Tuesday 12 April 2016

Habits That Are Non-Negotiable For Coding




Dear Readers,

Most of the college going student have an assumption that they will have to work alone. We even get a project work and are assigned to it as a team. Yet, in college, we fail to understand the importance of teamwork. Either we do most of the work or none of it. Due to this, we never understand what is coding. Is it the end result? Certainly not. It is the piece of art. An art which can express its motive. For me, a software never dies if you keep rectifying it. I worked on a game for 3-4 months. Yet, I always think how can I make it better. It's certain that one day I will get bored of it. But if my code is in a shape that anyone can understand and that also just by reading it, I will assume my assignment is complete.  

Most of the programming books have listed "Re-usability" as a key-value for a good code. For me, I don't think a code that can't be reused is qualified as the code itself. If you go with books on "Software Engineering" you will understand what I mean to say. Code that just works is useless if it can't be maintained or reused. I want to list two basic habit that makes your code more readable :


1 . Variable names/Function Names/Any Name


Look at the two codes given below
=====================================================
function f(n){                                                
      var f = 1;                                                            
      for(var i=1;i<=n;i++)                              
            f = f*i;
      return f;
}
=====================================================
function calculateFactorialOf(number){
       var factorial = 1;
       for(var counter = 1;counter <= number;counter++)
               factorial = factorial * counter;
       return factorial;
}
=====================================================

The first program looks like the test question we used to solve in the exam. The question was "what is the output of the program". But the code for software is not meant to test someone's IQ. It is meant to explain what the code does. You won't name your children as '100101001' just to complicate yours as well as other people's life. So why you want to complicate your code. Programming languages give you freedom to refer a memory location with a name so use it efficiently.

2. Indentation

Let us again look at two codes to compare :
=====================================================
function calculateFactorialOf(number){
var factorial = 1;
for(var counter = 1;counter <= number;counter++)
factorial = factorial * counter;
return factorial;
}
=====================================================
function calculateFactorialOf(number){
       var factorial = 1;
       for(var counter = 1;counter <= number;counter++)
               factorial = factorial * counter;
       return factorial;
}
=====================================================
It is simple to see what is the effect of indentation in the code. We get a better view of what piece code is contained in for loop and where a function starts and ends. The second code just with the indentation is much more easier to read and understand. 

I understand these are very basic steps. But nobody talked about it when I was in college. Nobody spent a second about it. But now when I developed these habits, it takes out the pain of explaining my code to a new person. Let me talk about my real life incident. I was on leave from my office. I went to my friends. One of the guy from CS branch asked me to show what I learnt. It was amazing for me that he was able to understand each line of my code as soon as I coded. I didn't need to explain him verbally any of the lines. It was a great experience for me. I hope this will motivate you to develop these habits.

Thank you.