Wednesday, December 28, 2016

Beautiful Code

Is coding an art or a science? This question is pretty similar to the question when you see a beautiful bridge or a building. Is it art or science? Think about this. Build a technically superior bridge but not tastefully done. It will not draw the same kind of emotion. 


Ever seen a classical dancer performing on stage. Most of us may not even understand the technicalities of the dance. I am for sure. However we can easily find it enjoyable, if the dancer engages us with his or her expressions. When they almost talk to us and not focussed on being technically correct. I am not saying that not being technically correct over communication is good thing. On the contrary the technically correctness is given and the dancer takes it to another level of engagement.

I find this quite parallel to writing code. Technical correctness should be given. What is required is the code which speaks to you, when you read it. The code talks about itself. It's not abut writing pedantic documents containing the obvious, but about explaining the core in such a way that the code tells about it automatically.

Let's see two snippets of code which calculates average

function average(int[] m){
int a= 0;
for (int i =0; i < m.size(); i ++){
   a += m[i];
}
return a/m.size();
}

It's a very simple code so quite obvious. Think if there are 10 more variables with all english alphabets. Now compare that with the following code

function calculateAverage(int[] dataArray)
{
      int sum = 0;
      int dataArraySize = dataArray.size();
      for(int i = 0; i < dataArraySize; i++){
      {
             sum += dataArray[i];
      }
      return sum/dataArraySize;
}

Very simple things to do but code becomes more natural. Couple of things to note:
  • Give intuitive names. The function name should be able to tell what it does and so the variable names. Same goes with variable names. Sometimes this leads to long variable and function names but in the longer run it helps in self documentation.
  • Format the code properly. Every IDE has shortcut for that. Just make it a habit to format or you can even enable auto formatting.
Some of the other things to take care of:
  • Don't write cryptic code unless it's bringing a tons of performance. Adopt KISS. You yourself will not understand it couple of months down the line. And if you understand an arcane feature of a language, code shared with others is not the right place to show your skill. It will hurt overall more than it will help. The corollary is it's very simple to write cryptic code if you understand it. So do complex things to write simple code. 
  • Write simple and modular code. Even the longest of novels are divided into logical chapters and sections. 
Consider writing code like writing poetry or a story. Build a natural flow. And write code only when you are absolutely clear of what you are going to write. The actual coding should be a typing job. The story is already jumping and kicking in your head in its full form.

  

No comments:

Post a Comment