Search This Blog

Tuesday, October 19, 2010

Coding Standards

Is there anything that is more religiously charged in a normally atheist world as Coding standards in the Software industry?

So I was in this discussion today about modernizing our style or at least make it more available to all the engineers working on the product. The modernizing, steams from the fact that the coding standard was developed 15 years ago, when Java was still in its cradle. No IDEs existed yet and no one even considered a Java coding standard.

I guess historically most Java developers came from C++, hence it became natural to use the same habits. Therefore our coding standard contains remains of these habits. Like fields are prepended with 'm_'. Fields/local and arguments use some pseudo-hungarian notation to indicate the use and type of variables and constants. Some examples being:
  • c for counters like 'int Foo'
  • i for integer like 'int iFoo'
  • a for arrays like 'int[] aFoo'
The list is long and for a developer that left C and went into the Java world it seems strange that this denotation is needed in a highly typed object oriented language like Java. Personally I can only remember one good reason for employ hungarian notation.

For those who remember the hassle of dealing with strings in C, I would like you to recollect BSTR (Binary strings) and char*. One is zero terminated by a single char '/0' the other includes the length in the first 4 bytes and is zero terminated by two characters '/0/0'.

Since any real world program would contain both types of strings it made perfect sense to use some notation of indicate what type of string was dealt with:
  • bstrMyString for BSTR*
  • pszMyString for char*
However Java, in a world were the IDE provides amazing mind boggling re-factoring functions and incredible code navigation. Something I never thought possible when I ran my first IDE (Seka assembler for those who are nerdy enough to know). I'm going to generalize a bit here so hang on. There is no need since all Java types are equal, all are references to something on the heap. Everything derives from Object. I hold my mouse over the field and can see, comments and declaration. Furthermore if I would try to pass a field or local into something that wouldn't accept that particular type I see it immediately.

Another interesting argument is that we want to be able to see what type a field is directly from its name. Since we are using the normal Java Bean pattern, it doesn't really work though:

public int getFoo()
{
return m_iFoo;
}

int iFoo = m_iFoo; // fine and dandy
int iFoo = getFoo(); // how can we be sure what getFoo() returns an int


The argument to keep hungarian notation in Java is and will always be about religion and out of sentimental reasons. One factor could be that cosmetically the code looks more advanced more like C code? I cannot explain it in any rational terms though.

Anyways, the commute went fine today, the first couple of miles took 40 min though because of some accident at 128/rte 3. which meant that I missed my sons baseball game tonight.

No comments: