How to write good codes or What are the good programming practices to follow !!





The following post highlights some good programming practices what beginner programmer can follow. Applying these increases the readability of codes, will make make codes more easier to update. 

Well, individual programmers should have their own programming styles to maintain, but still there are some practices that the beginners can follow. With times every programmers find out their own styles. Following is a list of things that beginners can do while coding or solving a problem.


01. Planning: This is a very important step from my view. Don’t care about how much difficult the program is or how big the solution will be, always make a plan before solving it on how you will solve the problem. Remember we are discussing about practices. Though Planning is required for large scale solutions, we should keep practicing for each and every problem to get practiced with it.

02. Decompose the Problem: It is a part of the planning step, however, to highlight it I brought it separately. Each problem will have 3 parts (Except some very minor practicing problems); Input, Processing and Output. Each part can be decomposed to many more parts. Think about them. The more you find the easier the coding will be.

03. Formatting: Formatting includes the Coding Styles.

     i) Using Indentation: When we read codes written by others it is almost always difficult to understand what the coder has done. I mean, we must have to care about the readability of our code. In this case, the first thing I can find is the Use of Indentation. Look, at the following code,

     if(i<2)
     {
     cout<<”Something”;
     if(i==0)
     {
     cout<<”i = ”<<i;
     }
     else if(i==1)
     {
     cout<<”Not Required”;
     }
     else
     {
     cout<<”Please increase”;
     cin>>i;
     }
     }
     else
     {
     cout<<”Where am I?”;
     }

     Now look at this version of the above code,
     if (i < 2)
          {
          cout<<”Something”;
          if (i == 0)
               {
               cout<<”i = ”<<i;
               }
          else if (i == 1)
               {
               cout<<”Not Required”;
               }
          else
               {
               cout<<”Please Increase”;
               cin>>i;
               }
          }
     else
          {
          cout<<”Where am I?”;
          }


     well I hope you have understood the use of indentation in coding. I can also show some more complex codes without indentation which can make anyone mad while trying to understand the code.

     ii) Naming Convention: Well, I found it from my very own experience that a piece of code seems like I have never seen before even when the code is written by me after only a few months. Its because I used variables like i,j,k,l,m,n,o,p etc. None of these describes why they have been used or what they are storing. We should avoid things like these. We should name our variable in a proper way. Like,

          Dim dob_sav_amount as Double
          Dim str_user_name as String

     iii) Commenting: Well, comments are necessary, very necessary. But sometimes just to describe the job of a loop in your code you may need a lot of lines. And this will turn your reader to more difficulty to understand because S/He will get jammed in Codes and Comments. This is not good. If your procedure requires a lot of descriptions then you should write a separate page to illustrate the method and procedure, then from the code just pull the reader to the appropriate location in the described page. And as you have written the descriptive page, suggest your reader to read that page before reading the code and while commenting, assume that the reader has already read the descriptive page.

     iv) Highlighting Section: This a method I use mostly while coding. This is something like following,

     /**********Initializing Variables and object*********/
     ………….
     /*codes here*/
     ………….
     /**********End of Initialization******************/


     /**********Taking input from user***************/
     ………….
     /*codes here*/
     ………….
     /**********End of taking input******************/

     /**********Pulling data from database***********/
     ……………………
     /*codes here*/
     …………………..
     /***********End of Pulling Data from Database****/

     /***********Creating and Publishing report*******************/
     …………….
     /*codes here*/
     …………….
     /***********End of Creating and publishing report*************/


04. Debugging: This is a very important and useful step for programmers. While debugging you can see how the program is getting executed line by line, you can also find the changes of values of the variable you used, thus you can find where the error is occurring or where you left a gap.

Well, that’s all for now. I wish, I will come up with some updates to this post. Thanks for reading. Let me mention, that all the steps described above are from my view and I also believe that every programmer should have their own programming styles. 

Good programming practices are practices that helps the reader of codes to better understand the procedure and also helps the coder while updating source code written previously or while writing a huge program. That’s what I think and can’t assure that this is what it is.

Thanks for reading. Leave comments if you want to share anything.

Recommended Recommends

Comments

Contact Us