Tuesday, February 10, 2009

Substance Over Style

I find that too much effort and emphasis is put on coding style. We blindly follow the guidelines and styles that we've always done, without questioning why.

In an Agile process, one of the goals that we strive for is continual improvement, continual quality process review. With every line of code that we write, we should do the same. Question why we are writing that code, or why we are writing it that way.

A good example of this is the if statement in Java. One of the commonly followed guidelines is that you should alway enclose the block of an if statement in brackets-even if it only one line of code. For example:

if(toast.isBurnt()){
eat(somethingElse);
}


instead of:

if(toast.isBurnt()) eat(somethingElse);


A traditional argument for this is that a developer may come along and add more code into the if block, but forget to add the brackets. Thus following a coding style convention will avoid these problems.

What is wrong with this statement? In a proper Agile environment, where tests are written first, surely this 'problem' will be caught the very next time you run your unit tests? You know, testing the substance of your code?

For my money, I'd prefer the developer to be writing test first code. As long as the substance can be proven, I'm not too bothered with the style of the code (within reason, of course!).

So, put your effort into the substance, and worry about the style later.

No comments:

Post a Comment