Sunday, September 26, 2010

CODE is not meant to be read!!

"Why call it code if it has to be read??!!??"
Someone jovially asked me this when I was lecturing about commenting the code he had written.

Oh!! I don't have an answer to this question! Honestly, that beat me TOTALLY! Having put forth the disclaimer, I am ranting why we should comment the code we write.

Many a times work of art is not understood by the common man unless explained. Take examples of the Oscars, how many times we complain, the movie that won the Oscars was totally unworthy of it! Let's step aside and take a moment, digest the truth; the reality is that more often than not WE did not understand the worth of the movie. ( Oh wait!!!! don't get back to me for that, may be that was a bad example. The idea was anyway not to rant about Oscar winning movies ;-) Read on!

Coding is an art, no doubt about it. The art of writing in a Language unknown to man, know to machines, forging lines of the story with the best possible approach to reach the result ( it is misnomered as algorithm and logic ). Thus, solving a problem -- only the machine way.

As goes with many forms of art, the code is not well understood by mere mortals. The artist forging code must enable mortals understand his code. How do we do this??

Simple, tag your code with comments!!

Its a given fact that, ones own code is not understood over a period of time. Remember, Thou art the lord, until thou finishes the line.Once you hit the carriage return, the burden of maintaining code is added on to the carriage hitched to your back.

When we get back to the code written "once upon a time" its inhuman on the self to expect to know what the code did in the first place. Worst still, if the realization hits upon just then, and we start writing comments to explain the code, I wouldn't be surprised to see a comment like this at that point of time :

/* When I wrote this code, God and I knew what it did, now only God knows what it does */
(found the above comment here)

I have heard of programmers' statements, "if some one doesn't understand what code is written here, he is not worth re-using/copying it". I bow to thee O programmer, but, what if its you who doesn't understand the code after a month?

Its a simple fact to realize, we are not here to prove our prowess as to how sharp a coder we are, we are here to solve a problem, often collectively. This is something we as programmers need to digest at the earliest.


For one single moment, we need to step into the shoes of the guy who sits in the same chair as ourselves and read the code we wrote. He would feel so miserable about the code, and again, I wouldn't be surprised to see a comment like this written by him :

/* Get me the guy who wrote this, it is punishment enough to make him read it. */

I guess I have put my point forth on why we should tag our code with comments. There are numerous formal articles on the aspect of commenting, if my ranting has inspired you to go look up for some such articles, i would suggest literate programming. If the name Don Knuth scares you ( like it scares me ;-) ) I would suggest something more simple to assimilate ( at www.cprogramming.com ). 
I also came across a nice article on tips to comment your code.

If at the end of all this, you feel like commenting out this blog post from your memory, feel free to do so.

I would leave you to enjoy some funny comments that I stumbled upon.

PS:
Links provided are copyrights of the respective authors.

Friday, September 24, 2010

jAvA JaVa

Learning a programming language in class really s*c*s.
I have always considered a programming language a tool to implement the logic and get the results out. Each of the languages have their own use and abuse ( well what ever it meant! ), choose the most optimal as per the need.

Oh!! this rant btw, refers to learning Java in the classroom.

I would any day prefer to learn the constructs of object oriented design and programming, implement the same using Java in a mini project.

Instead it feels so lifeless to be learning a language sitting in the class.

Yesterday's class covered the concept of interfaces in Java. The concept of interfaces is quite interesting to add abstraction to the code. Make code extensible etc., One can read about interfaces here.

Let me apply some new learning to express what i think :D
Code follows

/**
* Life is an abstract class;
* - will be explained in due course of time.
* The class Me defines what i am.
* Currently i implement Zombie!
* So i am a Zombie and lifeless :P
* Yes you got it. Sitting in a programming
* language class is being lifeless.
*/

public interface Zombie {
public Life lifeStatus();
}

public class Me implements Zombie{

public Life lifeStatus() {
return NoLife;
}
}