Thursday 23 October 2008

One of life little problems - solved!

How long have I been programming? Since I was 8... that's 23 years, and hence I'm a little annoyed that I've only just come up with something that seems so obvious in retrospect.

It is a solution to the classic program formatting dilemma.

First a couple of examples;

// Java Standard
try {
XMLDocument response = agent.request(xmlRequest, logger);
} catch( FailureReport failureReport ) {
throw new GSSystemException(failureReport);
}

// C/C++ Standard
try
{
XMLDocument response = agent.request(xmlRequest, logger);
}
catch( FailureReport failureReport )
{
throw new GSSystemException(failureReport);
}
The above illustrates the difference between the 2 notations. The first, the IBM Standard Java format, is something I've never really liked since everything looks so bunched up - bunched up code is much harder to read - I end up putting random whitespace in anyway, but with no level of consistancy.

The C/C++ notion is much better in terms of readability, but doesn't have the symemtry of the Java Standard and suffers from a fatal flaw; it's not Java Standard!! :)

But we have the solution! How to write *almost* standard Java code with out any of the issues;
// Colin's much better method

try {

XMLDocument response = agent.request(xmlRequest, logger);
}
catch( FailureReport failureReport ) {

throw new GSSystemException(failureReport);
}
As you can see, it's exactly the same as the C/C++ notion, but with the brackets at the end, a la Java Standard but with the whitespace preserved... see what I mean about it being obvious?

The great thing is the only bit that isn't really standard is the fact that the "catch" clause is dropped to the next line - but I'm not sure that is specified anyway, since I've since lots of code with the dropped line... and no - I can't be bothered to check!

No comments: