Those who start learning programming either starts at home or at school. When they think they aren't getting proper learning, they switch to coaching classes to learn programming in a better way. It is our mindset that we think programming can be easily learned in coaching classes. Exceptions are everywhere, so yes, there are classes where you learn programming in a proper way. But many of them don't teach the basic steps necessary to write a perfect code.
Don't worry if you feel you can't write a perfect code at once or twice or even thrice. I will guide you with 4 simple steps to success.
The first step is to analyse the problem and requirement which almost no body does properly.
How to analyse the problem?
Read the problem statement and pick up essential keywords and write what is required (such as loops, if-else statements, date-time functions etc) to solve the problem.
With those keywords, assume a flow of solution you can go with. Many of us stuck at this point, because when we assume the flow, we think about the syntax and not everyone remembers the syntax of each solution statement. Even I can't remember and search silly syntax on Google.
So, just assume informally how the problem is solved and get ready for the next step.
Now, you will say what is algorithm, pseudo code or written flow? And even if you know all of these terms, you don't know how to make one, do you?
Well it is not so hard.
An Algorithm is a step-by-step procedure that is followed to achieve a particular goal.
A Pseudo code is simple informal, wrong syntactical, human understanding program.
And lastly, a written flow is informal algorithm to let us understand what we have to do to make a perfect code.
Whichever of these you get into your mind will lead you to make the code properly. I started to write the flow first, then tried to create algorithms and nowadays I make pseudo codes to code.
Let me tell you, there is no rule to write a flow of a program. You can even write it in your local language. It's just what you think the solution goes like.
Alright, now that you have an algorithm or a flow written in your understandable language, we will now try to create pseudo code of it.
You might be wondering, why Pseudo code when we have an algorithm or written flow?
It is because if you have a pseudo code, it will be very easy for you to know what is wrong with your code, before you even write the actual syntactical code.
Yes, Dry Run is the method of executing the pseudo code (Might be informal) line by line on paper to know if the code will execute correctly with compiler or not?
Dry running the program is the most important step to know the compilation errors we might have when we develop the actual code in compiler.
It can also be called as a debugging stage where we can easily debug the bugs we might have during actual coding.
Even if the code works with one input (test case), never believe that your code is perfect. A perfect code is one which can handle any input i.e. exception/error handling capacity must be there.
Try at least 3 test cases with different data types and if that works for you, blindly write the syntactical code of your pseudo code in compiler and see how it works!
I used to start coding by reading the problem statement for 1 minute, then I start coding the syntax on compiler without analysing it properly. I stuck so many times during code, because I used to think about the solution after I include/import header files, open the class and main function and all. It took at least 4 hours for the problem to be solved which could be solved in 30-45 minutes.
Once I sat in a campus drive, there were three problem statements to code in two hours. It was on Hackerrank. For the first time of my life, I strictly followed all these steps to code the first problem. It took 35 minutes to write the proper pseudo code on paper and just 5 minutes to write the syntactical code on compiler. And believe me, it worked for all 2 public test cases and 4 private cases at once.
After that time, I never code a single line without analysing, making pseudo code before, dry running and testing it.