|
Boost : |
From: Douglas Gregor (gregod_at_[hidden])
Date: 2001-07-07 11:12:32
5.1 states "Use a separate line for opening and closing braces". In "Code
Complete", Steve McConnell considers the following three styles:
// 1
if (cond) {
stmt1;
stmt2;
}
// 2
if (cond)
{
stmt1;
stmt2;
}
// 3
if (cond)
{
stmt1;
stmt2;
}
5.1 suggests style #2, which is the only one of the three that McConnell says
_not_ to use, because it violates his "Fundamental Theorem of Formatting."
Essentially, the formatting doesn't fit the logic: without whitespace both
above an below the entire code snippet, one can't tell that the block is part
of the if statement. Code like this:
// A
foo = wibble + bar;
{
stmt1;
stmt2;
}
looks very similar to #2, but there is no relation between the block and the
line before it. Throw in an identifier that looks like a statement (for_each,
perhaps?) and it becomes more confusing. While I've not seen code like "A"
often in C++ (it's more common in C when the programmer needs a temporary
variable), it would be used more often when using the
resource-acquisition-is-initialization methodology.
McConnell also cites a study that found no statistically significant
difference between #1 and #3 regarding readability and understandability of
code. Between these two it is a matter of personal preference, but I think
that there is good reason not to accept the style proposed by 5.1.
I personally would opt for #1 because
a) doesn't waste vertical space
b) more common historically
Doug
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk