Have you ever discovered that something you have been practising for a long time has a name associated with it? I did just now and I could not resist blogging about it :).

One of my favorite pastime is browsing http://www.reddit.com/r/programming/. It would not be wrong to say I am sort of addicted to programming subreddit. A few minutes back I came across this link http://ssdl-wiki.cs.technion.ac.il/wiki/index.php/Spartan_programming and I was like “wow..this is my style of coding. Man, it has a name associated with it.” Of course, I do not follow everything that is in there, but a majority I do.

While we are at it, let me point out some of the observations I have made regarding coding styles:

Earlier I used to cram everything into a single line but now I have sort of rationed that practice after a colleague of mine griped about it during a code review session. He was like “When using a debugger, it becomes a pain to inspect the value, if you follow that coding style”. Well, I would not say it becomes a pain but for sure I agree with him that it becomes easier to inspect values while debugging if you split assignments into multiple lines. Consider the below snippet:

String name = EscapeUtil.StripHTML(StringUtil.trim(foo.getName()));

While debugging the above, if you want to check the original name foo returns then, you have to select foo.getName() or say you are trying to inspect the trimmed value then, you have to select StringUtil.trim(foo.getName()) etc.

If the above is written in multiple lines as below

String name = foo.getName();

name = StringUtil.trim(name);

name =  EscapeUtil.StripHTML(name);

Now, if your are stepping through, you can track the variables in the debug window without getting into the hassle of selecting something or adding that expression to watch. As goes with the universal truth “There is a tradeoff everywhere”.

Also I prefer mutliple returns instead of using a big ass if else block. My colleagues code in the if else style. My code would look like:

if (not true) {

return;

}

…….

…….

return;

While what they do is

if (true) {

….

….

}

return;

I prefer multiple returns because I feel it makes code more readable especially if the blocks are huge. Well, everybody has their own style :).

Advertisements