Lately I've been seeing a lot of discussion on various sites, blogs, wikis, and what have you about fully bracing all braching instructions (with references to C-style languages that have bracing, or analogous optional-bracing schemes in other languages). Generally, this is the classic example given in the argument for full bracing:
if(some_condition)
do_some_action();
Of course this appears in various forms for different branching instructions (i.e. all loops and conditionals). The argument goes that some programmer might come along in the future and need to do two things when the condition is true:
if(some_condition)
do_some_action();
do_some_more_actions();
He then "forgets" to add full bracing to the conditional. Oops! do_some_more_actions() is now unconditional, and will always execute. This is obviously a problem - right? Personally, I don't think so. I think this argument is a straw-man and frankly rather laughable.
First of all, if you have any decent indentation standards whatsoever, it should be immediately obvious to anyone reading the code that there needs to be full bracing around a multi-line conditional block. Someone with any appreciable experience in C-style languages will
immediately see that something is "wrong" with the code, simply because of the indentation and lack of bracing.
Secondly, I highly doubt anyone actually forgets to do this, unless they are extremely sleep deprived or very new to C-style syntax. Following the first point, anyone adding do_some_more_actions() should immediately notice that the conditional is not fully braced, and add bracing to make it work as intended. I know I do this all the time, and it's second nature to me to look at branching instructions for full bracing when I modify them. It's sort of like making sure you put a semicolon at the end of every statement - this shouldn't be something we panic about people forgetting to do. Granted, the compiler won't gripe at me for not fully bracing, but IMO that makes it even more important that programmers be sure to take care of it without having to be reminded.
Third, to my mind, if someone
does forget to add full bracing, it's frankly their fault for not being careful when modifying code. In a lot of the discussions I've read, people seem to take the view that any bugs introduced by this sitation are the fault of the original coder, and the forgetful modifying programmer is absolved of all responsibility for their oversight. Perhaps this is just my own extrapolation, and perhaps it isn't what people are really thinking - but either way it seems pretty odd to me. If a maintenance programmer doesn't understand my singleton pattern and directly instantiates a copy of the singleton class, is it my fault for using a confusing pattern? Or is it his "fault" (although
fault seems a pretty harsh word to me) for not being familiar with the pattern and not properly learning about it before modifying it?
Anyways, as a final (and probably totally irrelevant) observation, I've noticed that a lot of full-bracing advocates are also proponents of the vertically-compacted brace style. The argument for that brace style almost universally seems to be that it saves vertical space; yet these same people end up wasting an entire line on a trailing brace for small conditionals just so they aren't guilty of someone else's (hypothetical) accidental omission of bracing in the future. Maybe this correlation is totally invented in my head, and it's certainly not really meaningful, but I found it rather ironic.
Discuss.