4 Simple Steps to Write a Perfect Code

4 Simple Steps to Write a Perfect Code


Those who start to learn programming either starts at home or at school. The time they think they aren't learning properly, they switch to coaching classes to learn to programming in a better way. It is our mindset that we think programming can be learned easily in coaching classes and not at Home or School. 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 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.

4 Simple Steps to Write a Perfect Code

Coding isn't just writing the syntax which comes in mind by looking at the problem and organizing the lines of code. You have to be a little organize with the way you code. Below are four mandatory steps following which you can follow and get the result you expect from the code you write:

Analyse the problem and requirements

The first step is to analyse the problem and requirement which almost nobody does properly. Let's take a very simple code problem to understand the steps more quickly.

The problem statement is - "Find whether the given input number is prime or not and print it"?

Just ignore the toughness of code, if it seems horrific and let's find out

How to analyse the problem?

A perfect code does not require perfect analysis. Just 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.

Here the question is "Find whether the given input number is prime or not and print it".

The keywords we should focus on are -

  1. Find: we have to find something
  2. given input number: we have to take a number as input
  3. prime: we need to write logic to find whether the input number is prime or not
  4. print: At last we need to print whether the given number is prime or not

With those keywords, now 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.

Create an algorithm/pseudo code/written flow

Now, you will ask what is an 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 Pseudocode 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.

So to make a perfect code, let us make our own flow logic here. The steps that we follow could be -

  1. Declaring a variable which will contain the number
  2. Taking input in that number
  3. Opening loop (Now, we will think about the variable which will control the loop, for this we better figure out the logic of prime number and starts with step 4 at first)
  4. Take all numbers from 2 to N (Where N is input number) in a loop and check whether N is completely divisible by the interative number (Say i) or not -
    loop from i = 2 to i = N
        if N%i equals 0
             exit the loop
             increment i by 1 and run the loop again
    End loop
  5. After the loop ends, check if i equals N, which means if there is no number between 2 to N-1 which is completely divisible by N. Then print the input number N is prime, else print the number is not prime.

Dry Run the Code

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.

What's Dry Run thing now? 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 the 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.

So, take an input number (Say 13) and use it in the flow we made above. It should work if you understand it properly.


Even if the code works with one input (test case), never believe that your code is perfect. 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 the compiler and see how it works!

For your convenience, refer the code below for the flow logic -

using namespace std;  
int main()  
  int n, i, flag=0;  
  cout << "Enter the Number to check Prime: ";  
  cin >> n;  
  for(i = 2; i <= m; i++)  
      if(n % i == 0)  
          cout<<"Number is not Prime."<<endl;  
  if (flag==0)  
      cout << "Number is Prime."<<endl;  
  return 0;  

My Experience when I followed these steps

I used to start coding by reading the problem statement for 1 minute, then I start coding the syntax on compiler without analyzing 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 on 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 the 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 analyzing, making pseudo code before, dry running and testing it. Hence, most of my codes are perfect code for me. I suggest you to try it once and see the outcome.

Thanks for reading :)