[For beginners] The right approach to solving errors in your code.

There are so many times you will encounter an error and you may spend hours on it. So here are few things to consider to save your time.

The right approach to solving errors in your code.

One of the issues I had while going through my program was - I wrote the program myself, I implemented all of the logic myself, I solved all the errors myself but I could not recollect the next time I visited the program as to how the program worked at first glance. I had to “spend” time again revisiting to find why some variables were kept there.

To overcome this situation of spending time, though I know commenting is a bad option; I think writing down the logic separately or writing down the code entirely with comments somewhere else while you are a beginner is a good idea. So that whenever you visit the program, you see again what you did. This can help because let’s say you rediscover an input issue later, you know what part of the program you need to change quickly instead of trying to understand for a few minutes what your program variables and functions are doing or passing things to each other.

So here is what is recommended for beginners:

Write down the logical flow in plain English: Once you are done with the program code, you write down in your own words what you think worked. Write down the logic in plain English.

Write down the logical flow in code flow: Even though plain logic is good to go through the program, in some cases it is not. You may also need to write how the different functions are working together, how the different statements are executing and how the different variables are making it all work. Write down step by step what you think the statement is doing.

Write down the error in plain English: First describe what you think is the problem Don’t assume you know what the problem is by just seeing it. Write it as detailed as possible based on: what do you see? What part of code do you think is involved in it? What changes in that part of the code do you think is possibly affecting it? This is necessary because when you are searching for a solution, you will find solutions that will possibly try to tell that it is solving your problem when potentially it could make your error worse. For example, I was aligning labels. So when I implemented one of the solutions, it made the alignment totally unexpected for my use case.

Learn how to find a solution: So when you have described the problem and before you search for a solution. Here is what you can do. Observe what you are not seeing or intended to see but not visible Observe what you are seeing but it appears other than what you wanted it to be.

Once you notice something is not right, try to figure out what part of the code could be affecting the output. Then go to that piece of code and read it first for syntax errors, read it for variable names or function definitions. Then unit test it or test that piece of code and do a logical run or dry run to see how the function is performing. If it’s performing okay, try to see before and after how that function or variable or statement is linked and is that affecting it. Once you have done this and you still do not get it. You go for asking what is wrong.

Sometimes it is not right: Sometimes things do not work together and you won’t know because it is not documented or maybe you did not think that was the problem. Your understanding of how everything works individually and as a whole when linked to each other grows through practice. If you lack an in-depth understanding of the concept, you might run into “I don't see what is logically wrong and is not working out for me” The logical part is when you do not see an error and you do not see an output also. You cannot solve what you cannot see. So you have to dig through everything, do unit tests of what every variable and function is doing for all the cases you want it to run. That will give a clearer picture of what part you think is working logically wrong or probably stopped working at that point.

In terms of an example if you don't know it yet, I learnt about type coercion in Javascript where operators can perform on string if they are numbers. So imagine a number is stored as string and you try to perform functions; you will see the output and you will be elated but when it does not, remember type coercion is not happening. It is important to see how data is stored in different data types due to a definition it was given to execute via logic.

So summing up, read your errors in detail. Do not rush to solve the problem assuming the problem. Understand the problem, understand your code and then proceed to write out solutions. Test these solutions and go for the one that works optimally for your scenario.