I’m not a fan of holy (tech) wars, but I do appreciate clarity in representation. How your code looks is important not only because it adheres to standards hence making it easier for future generations (yea right) to decipher the madness but also from the standpoint of improved pattern recognition. Pattern recognition in terms of how you learn to recognize obvious problems looking through artifacts, for example while ‘glazing’ through log files. Similarly, when looking through code, you should be able ’see’ code smells and the like. Whatever makes code more amenable to such pursuits is not a needless war I think.
Obviously, there are many points to this, like naming conventions, style guidelines (camelcase, etc) but my pet peeve is intend guidelines (ironic, that I use ‘guideline’ yet harp on about the same in this tirade).
There’s a super wiki article on the topic that gives me comfort knowing I’m not the only one wasting my time. Anyways, I prefer to use the “Allman style” where your code looks like:
while (x == y)
{
something();
somethingelse();
}
finalthing();
Notice how the “The style puts the brace associated with a control statement on the next line, indented to the same level as the control statement. Statements within the braces are indented to the next level.”
Among the advantages the wiki lists, an interesting one is “Commenting out the control statement, removing the control statement entirely, refactoring, or removing of the block of code is less apt to introduce syntax errors because of dangling or missing brackets.”
//while (x == y)
{
something();
somethingelse();
}
The reason I like it however is because it flows less jarringly than a:
while (x == y) {
something();
somethingelse();
}
which is often the intend standard in most Java shops. However, I can’t stand control statements and the body being so snugly fit, so to reach a compromise between adhering to the norm and my obstinate tendencies, I’m often tempted to restructure the above as:
while (x == y) {
something();
somethingelse();
}
See how futile and stupid this is?
Let’s see a more practical example for better impact:
private void iHaveAnUnreasonablyLongMethodName(Stuff iAmPassedIn) throws IMightThrowThisException,
OrThisException, OrMaybeThisException {
if ( (iAmPassedIn != null) && (iAmPassedIn.getStuff() != null) ) {
doStuffOnStuff(iAmPassedIn.getStuff());
}
}
Yech! See what I’m saying?…