While helping a colleague with a defect regarding things that should not happen but were and caused the whole tower of sticks to come tumbling down, I noticed a block of code that follows more or less the gist of the pseudo-sample below:
if(lots of conditions)
{
doSomeStringCopies();
if(a certain condition)
doSomething();
doSomethingElse();
doFinalStringCopies();
}
Bear in mind that the consistency of indentation of the actual code was nowhere as nice as above.
Notice the problem? doSomethingElse() will be called irrespective of the secondary condition, resulting in bad things happening because a certain field does not contain a valid value for the doing of something else, but is entirely valid in terms of the business rule.
This is why you always surround your compound code structures with braces: because you never know when someone’s going to slide a “fix” into your code and neglects to add the braces. Several people (who have obviously never been burnt by this problem, and if they have, they should have their keyboard license removed from them) might think that bracing a compound structure which contains only one statement makes the code look uglier or more unreadable, but it definitely makes it a lot more maintainable.
(As an aside, I have a feeling that this is one of my more poorly written entries. Shame on me, I know.)



