DEV Community

Cover image for How I Learnt Logic Building in Programming! - 5 Tips To Build & Improve Programming Logic
Muhammad Maaz Shakeel
Muhammad Maaz Shakeel

Posted on

How I Learnt Logic Building in Programming! - 5 Tips To Build & Improve Programming Logic

Tip #1: Divide & Conquer

Always divide the problem into different parts.

For Example

This is a C code that just input for a number one and a number two and simply add them.

   #include <stdio.h>

   int main(void)
   {
       int num1;
       printf("Number One: ");
       scanf("%i", &num1);
       int num2;
       printf("Number One: ");
       scanf("%i", &num2);
       printf("The Addition ofjavascrip those numbers is: %i", num1 + num2);
   }
Enter fullscreen mode Exit fullscreen mode

This program just adding two numbers inputted from the user. So, we can just divide this code.

So, How are we going to divide this code, Well we can create a function called add that just returns the value of the addition of those numbers that inputted from the user?
Let's Divide the code

   #include <stdio.h>

   int add(int number1, int number2);   

   int main(void)
   {
       int num1;
       printf("Number One: ");
       scanf("%i", &num1);
       int num2;
       printf("Number One: ");
       scanf("%i", &num2);
       int addition = add(num1, num2);
       printf("The addition of those numbers is: %i", addition);
   }
   int add(int number1, int number2)
   {
       return number1 + number2;
   }
Enter fullscreen mode Exit fullscreen mode

So, We divided the code. This was an example of a simple program. Maybe you have a complex program, so you can divide them into functions or maybe you are making an app in react native.

React-Native lets you break the code into its own component. And you can create a file for that component and import that component.

So, You can break the code into its own components in your own application. This will be cleaner and more readable code.

Tip #2: Write a well-commented code

Write comments on your code, that explaining your code that what that block of code is actually doing.

So, Let's take the example of the code and write comments on that code we used in the last tip.

For Example

   #include <stdio.h>

   int add(int number1, int number2);   

   int main(void)
   {
       // Getting input from the user
       int num1;
       printf("Number One: ");
       scanf("%i", &num1);r
       int num2;
       printf("Number One: ");
       scanf("%i", &num2);
       // Adding the numbers
       int addition = add(num1, num2);
       // Printing the Output
       printf("The addition of those numbers is: %i", addition);
   }
   // This function add simply returns the sum of two numbers
   int add(int number1, int number2)
   {
       return number1 + number2;
   }
Enter fullscreen mode Exit fullscreen mode

So, write comments that explain your block of code.

Tip #3. Learn Data structures and algorithms

Programming is all about data structure and algorithms. Data structures are the fundamentals of all programming languages. It means that if you want to have a good command of any programming language, then you should start with the data structures of that programming language.

If you get a better command over the data structure of programming languages, then you can quickly improve your programming logic for that language. Now you know the best way of how to develop logical thinking in programming.

4. Learn about Programming paradigms

It would help if you also tried to learn the programming paradigms. There are many types of programming paradigms. One of the most popular programming paradigms is object-oriented programming. The programming paradigms also work as the blueprint.

With the help of this blueprint, you follow a predefined path to create the projects. It would help if you created plenty of projects using the same programming paradigms to have proper command over them. It will help you to improve your programming logic.

5. Start exploring code from experts

There are hundreds of ways to write a program to solve a particular problem. In programming, we have many ways to solve many problems. You may use different logic to solve the problem. And the other programming may use different logic to solve the same problem. That programmer might use the most optimal and straightforward way to solve the problem.

Therefore you should look at other peopleโ€™s code; it will help you to advance as a programmer. Github is one of the most popular programming platforms where you can see a lot of great projects and lots of programmers to solve the most complex problems with a short length of code.

So, I hope this will going to be helpful for you. Thanks for reading.

Top comments (12)

Collapse
 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ • Edited

Regarding tip #2 - comments should only be put in code if what the code is doing is not obvious, or some explanation is needed. Most well-written code is self explanatory and requires few, if any comments. The comments in your example are almost entirely superfluous

Also, a tip when sharing code samples on Dev - put the name of the language after the opening three backticks on the codeblock and you will get code highlighting

Collapse
 
codewithmaaz profile image
Muhammad Maaz Shakeel

Is there any improvements should change in my post?

Collapse
 
codewithmaaz profile image
Muhammad Maaz Shakeel

Thank you for the tip. โ˜บ๏ธ

Collapse
 
codewithmaaz profile image
Muhammad Maaz Shakeel

So, How are we going to improve Tip number 2?

Collapse
 
codewithmaaz profile image
Muhammad Maaz Shakeel

Thank you for telling me.๐Ÿ˜Š

Collapse
 
pazka profile image
Alexandre Weisser • Edited

Hey, he took the time to explain to you what he thought should be improved with a very detailed comment.

He doesn't have an obligation to answer you if you ask for more details.

If you read carefully its comment, you should see right away what you should improve in your post, a tip : Rewrite tip #2 with correct advices.

If you want more details, you can check the book "Clean Code". Especially the part about comments.

Also the series of increasingly aggressive answers make for a toxic communication, and you should cut that from you behaviour if you want to involve more deeply in collaborating with other. As a self taught developer, this should be your priority.

Have a nice day.

Thread Thread
 
codewithmaaz profile image
Muhammad Maaz Shakeel

Have a nice day too.

Thread Thread
 
codewithmaaz profile image
Muhammad Maaz Shakeel

Is it better now?

Thread Thread
 
pazka profile image
Alexandre Weisser

Yes

Thread Thread
 
codewithmaaz profile image
Muhammad Maaz Shakeel

Thank you ๐Ÿ˜Š

Collapse
 
mdcoder100 profile image
Moeez Bodla

Help Full! Thanks for sharing.

Collapse
 
codewithmaaz profile image
Muhammad Maaz Shakeel

Thank you ๐Ÿ˜Š