How To Code: How To Improve Logical Thinking For Programming

how to improve logical thinking
Photo by tian kuan on Unsplash
There are multiple techniques shared on the internet on how to improve logical thinking or # logic building tips for programmers or how to think like a programmer. But those techniques does not work. I mean they work but... the hard way. It is like someone is asking you to taste a delicious food without opening your mouth. Can you do that? Probably you can come up with a super creative way but that's not the way it works. There is one most important process to follow before applying those techniques. You can say, it is a prerequisite to fully utilize those logic building technique. And it is, understanding the flow of data inside your code. The code that you haven't written yet. Wait... What?

I am not kidding. You can understand your code that does not exists yet by using your imagination power. The power that makes us human, human. If you want something done and you cannot imagine it, you will never get it done. Here, I will tell you how programmers in the beginning develop their imagination power in order to build strong logical thinking ability.

The technique discussed here data pipeline technique, similar to instruction pipeline but with a higher level of abstraction. The purpose of using this technique is to imagine what kind of data flow is required in order to convert input data to output data. Once you successfully develop your sense of imagination to foresee that what would happen to a given data after only one statement of code executes, then you can easily imagine a long flow of data transforming from input shape to output along the way. And it gains momentum as you keep developing your data flow imagination skills. We will call this technique logic pipeline in the rest of the post.

Why to use logic pipeline?

The most important thing in logic building is how well you understand data flow, a sequence of steps or stages where data transformation may occur at each step. In order to understand data flow, I have divided it into simple steps:
  1. identify input data
  2. identify source(s) of input data
  3. identify intended outcomes of the program
  4. apply the required transformations on input data
Steps from 1- 3 are quite easy and require less effort. However, step 4 is a tedious task. It is the core of logic building in programming. Each of the steps is explained below:

1. Identify input data

Before writing a single word of code, identify the input data. Input data is the raw data that is given to the program in order to process it and produce the intended results. Examples:
1. Images are the input data for picture editing software.
2. Financial data is input data for business analytics tools.
3. User input is input data for a game.

Study the problem you are writing code for and identify the input data for your program. Input data is the key to accurate program. You cannot move further unless you have the key. Unless you have an idea of what are you dealing with, you cannot make your next move.

2. Identify source(s) of input data:

Once you identify input data for your program, I think it would not be that much difficult to identify its source. By source I mean the place where the data is coming from. It is not necessary that you will always receive data only from one source. The data may come from many and heterogeneous sources. For example, sources of data in examples mentioned above could be:
1. Camera, Hard-drive, Flash drive or Internet.
2. User input, excel sheets or other docs stored on hard drive.
3. User or another code executing on the same or another computer.

3. Identify intended outcomes of the program:

The intended outcomes of a program are the values we want it to create for us. These are the achievements the user will receive after successful execution of the program. For example, indented outcomes of the above mentioned examples could be:
1. Visually improved images
2. Meaningful insights about the business
3. Game aesthetics and dynamics

So until this step, you know what kind of data you have, where is it coming from and where it needs to go. Logic building is just taking some sort of data you have in your hand to some other sort of data.

4. apply transformations from input data to output data
 
Once you identify what you have in your hand and what you want to turn it into, all you need to do is to say the magic spell. Logic building is simply a sequence of steps which gradually convert input data to output data. You know where to start and where to go. Take small baby steps and move ahead. By moving I mean the movement of data, convert data from one level to another level or from one form to another form. It is just an activity of path finding in a puzzle game where basic structures or statements of programming (Loop, Conditional statements, I/O, Math operators etc) are pieces of the puzzle. You keep connecting the dots and the magic happens by itself.

In order to become good at making logic, you must know what would happen to data after it passes through a function or what would happen to current data after a line of code executes. The more lines of code or functions you can forecast about, the quicker you can make logic. Let me explain with an example: If someone named A can tell you what will happen to data after 1 line of code executes, it will take him/her t time to make a logic L. If someone else named B can tell you what will happen to data after 2 lines of code execute, it could possibly take him/her t/2 time to make the same logic L.

The main point of this discussion is that at any point/line in your program or logic, you should know the following:
  1. input data you have at the moment
  2. desire output data
  3. transitions from input to output
  4. a list of basic operations on input data and their results without executing the operations.
In the next few days, this step will be explained further with the help of visuals and examples.

Comments