February 24, 2006

For, While Loops & Birthday Wishes

February 23 was an eventful day for one of my close mates. She turned 30 and I "think" she has accepted the fact that she is about to start the third decade of her life.

Another ex-colleague of mine happened to share the same b'day as well. She never revealed her age but she's a SYT and that doesn't matter much. If you're a UAT, then, you've got to hide the fact that you are turning 1 year older. ** hee hee **

NOTE: By the way, SYT = Sweet Young Thing and UAT = Ugly Aunty Thing...

Anyway, this ex-colleague was online and we decided to do our customary geeky thing -- announce her b'day wishes on our chat client's status message. Some were nice, like "Happy Birthday XXXXX!" but some decided to write some archaic program like:

Human object = new Human();
object.happy(..);

I told this friend that his code didn't follow proper Java coding convention and he replied that he was using C#. -_-

Oh well, that got me thinking about putting up some wishes and I went:

for (int i=1; i=10; i++) {
System.out.println("Happy Birthday XXXX!");
}

A lot of people came up to me and said that I should have changed to using a while loop, which loops infinitely. :)

I threw back a challenge to ask them to create an infinite loop using a "for" statement.

One came up with this:


for (; ;) { System.out.println("Happy Birthday XXXX!"); }

Seems valid. Not sure if he had compiled and run it on his system. I threw back another challenge to use a boolean variable as a counter, something with mimics the "while" loop. I came up with:

for (boolean infinite=true; infinite == true; ) {
System.out.println("Happy Birthday XXXX!");
}


I think its valid. :)

The point is, even when writing small pieces of code like this, we should follow the coding conventions properly. Recently, I had to explain some bit of code to someone and we came across something like this:

for (Iterator iter = abc.getSomeList().iterator(); iter.next()) {
SomeObject object = (SomeObject) iter.next();
object.do(someVar);
}


Anyway, to the coder, this seems like OK code but to me, because I like to use coding conventions like "_varName" for local variables and stuff like "doXXX()" as method names, this really irked me a little bit. I think sometimes, its out of habit that we code in a certain manner and when someone tells you that you've got to conform to some standards, I can see why there would be some hostility at first. But once you "sell" the idea of more readable code and easier maintenance, people would get used to it.

I've always felt that one of the tools that a development QA team should have is a code convention verifier/formatter to go through tonnes of code, generate some report to show which pieces of code do not conform to convention. Some would argue that the code convention in use may not be efficient especially when it comes to variable declaration. This is a big issue as in Java, performance gurus always state that we have to be prudent with our object creation/deletion. Some Java developers prefer to use the good old C-style programming that we declare all variables at the top of the file and then, use it in all our methods below these variables declaration. However, others prefer to declare and create the objects as and when they need it so that it lives within a certain scope.

Whatever the preference and how that affects performance, the least the developer should do is follow some naming convention so that code looks more readable. Who knows that a variable called "car" is a local or class variable in the code unless someone traces through the entire file for it?

Oh yes, one final note. I hate it when certain people like to use the "x", "y" and "z" as class variable names. -_-... It doesn't describe what the variable is used for. I don't mind having this in use in for or while loops where I know that the variable's scope is short-lived.

Anyway, best to check out this site for details... ;)

No comments:

Post a Comment

Please feel free to add your comments. However, take note that your comments may be edited or deleted as seen fit by the author of this blog.

Take note that the author of this blog may not be held responsible for the comments which may be insensitive, vulgar or controversial.

Related Posts with Thumbnails