Science, anyone? Anyone? -

Science, anyone? Anyone?


Last spring, my fellow columnist Michael Barr published the first column in his new Barr Code series, entitled “Bug-killing standards for firmware coding.” In it, he recommended ten “coding rules you can follow to reduce or eliminate certain types of firmware bugs.” Needless to say, the column elicited a torrent of comments. Some of these comments actually considered the merits of his recommendations. However, many comments wandered off onto the subject of brace placement, an issue Michael didn't address explicitly in his article. (He did recommend using braces even when they are optional; however, he didn't recommend a preferred style for placing them.)

The flow of the discussion was quite typical of what I've seen or heard in other such discussions. In this case, the opening salvo came when one commenter asserted that using the “Allman” brace placement style caused bugs that using the “One True” brace placement style cured. Another commenter replied that, despite the previous claim, the Allman style is easier to read. And so it went.

Let me first say that I enjoy talking about programming style–not just about brace placement, but about style in general. After more than 30 years in the computing business, I've heard many of the arguments already, but I still hear new ideas often enough to keep me coming back for more.

On the other hand, I'm rather dismayed that nearly all programmers who participate in such discussions apparently do so with the tacit assumption that there's no objective basis–no science–that we can use to discern a preferred style. The discussions traffic almost entirely in anecdotes, personal observations and largely unsubstantiated assertions. I can't recall the last time I heard or read anyone suggest that we ought to measure how style choices affect code quality, let alone how to conduct an appropriate experiment.

For example, advocates of the One True style claim that it's better than the other styles at revealing extraneous semicolons after conditionals. That is, it's easier to spot and avoid the spurious semicolon in:

while (condition); {    statement(s)}   

than it is in:

while (condition);{    statement(s)}   

This seems plausible, but I'd like to know how often this problem actually arises. The answer I'm looking for isn't “Lots.” It's a number indicating defects per unit of code. I'd like to see a number for each brace placement style so I can assess how effectively each style reveals the error.

Moreover, as another commenter indicated, using static analyzers might be a more effective strategy for detecting this error, so much so that it renders moot any advantage that one style has over another in this regard. A few stats might tell us if this is really so.

Stray semicolons are probably not the only coding gaffes whose presence or absence in code might suggest an advantage of one brace placement style over another. Wouldn't it be nice to see a fairly comprehensive catalog of such errors along with statistics indicating the relative effectiveness of each style at revealing each error?

Lest the One True style proponents think I'm picking only on them, I'll note that proponents of the Allman style claim that it's easier to read and is better at exposing mismatching braces. To them I ask, “By what measure?”

How do we find this evidence? Before we start thinking about how to produce it, we should begin by asking, “Does it already exist?” I've searched the web for studies of the relative effectiveness of brace placement styles, but found nothing. I've found articles on other aspects of coding style, such as indenting depth (two to four spaces seems to be best at improving comprehension), but nothing on brace (or begin-end pair) placement. But, just because I haven't found any research, that doesn't mean it doesn't exist. So I have this request:

If you know of quantitative research results on the relative effectiveness of brace placement styles at improving code quality, please write to me and tell me where to find them.

(Brace placement is certainly not the only style controversy that could benefit from more rigorous analysis, but I'd like to limit the scope of this social experiment to just brace placement for now.)

I'm skeptical that such findings exist, but I'd be happy to be wrong on this. If they exist, why do so few people cite them? If they don't, why doesn't anyone seem to care, or even notice?

Dan Saks is president of Saks & Associates, a C/C++ training and consulting company. For more information about Dan Saks, visit his website at Dan also welcomes your feedback: e-mail him at . For more information about Dan .

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.