views:

959

answers:

21

As part of a code standards document I wrote awhile back, I enforce "you must always use braces for loops and/or conditional code blocks, even (especially) if they're only one line."

Example:

// this is wrong
if (foo) 
    //bar
else 
    //baz
while (stuff)
    //things

// This is right.
if (foo) {
    // bar
} else {
    // baz
}
while (things) {
    // stuff
}

When you don't brace a single-line, and then someone comments it out, you're in trouble. If you don't brace a single-line, and the indentation doesn't display the same on someone else's machine... you're in trouble.

So, question: are there good reasons why this would be a mistaken or otherwise unreasonable standard? There's been some discussion on it, but no one can offer me a better counterargument than "it feels ugly".

+20  A: 

I enforce this to a point, with minor exceptions for if statements which evaluate to either return or to continue a loop.

So, this is correct by my standard:

if(true) continue;

As is this

if(true) return;

But the rule is that it is either a return or continue, and it is all on the same line. Otherwise, braces for everything.

The reasoning is both for the sake of having a standard way of doing it, and to avoid the commenting problem you mentioned.

Gus
if (arg == null) throw ArgumentNullException("arg"); Totally agree on the braces required for multiple lines through.
Jarrett Meyer
I *always* put the continue or return statement on the next line, but maybe that's just me.
Robert Harvey
I could certainly see an exception for "on the same line"; I may change the standard over to that. (Thanks!)
Dean J
I follow the same standard; it's a good exception to make. Having a single return statement in braces looks silly.
xcut
I don't think having a single return statement in braces looks silly.
Joe Philllips
nice idea - I am a stickler for braces, but this is a rational and useful exception
Ray
Do you like having multiple returns in a single function/method?
Partial
if(true) { continue; }
Joe Philllips
And, presumably, `if (true) break;` to terminate a loop.
Jonathan Leffler
@Jonathan, yes...but I don't encourage breaking loops that way unless there is not other way to do it. Usually, there are ways that make more logical sense. That said, I'm not going to force someone to find them if they use this, unless it is glaringly obvious. I've had someone implement a loop that said while(1=1) with a if(x=0) break; statement in it. That kinds of programming is embarrassing.
Gus
+8  A: 

I have yet to have anyone come up with a good reason not to always use curly braces.

The benefits far exceed any "it feels ugly" reason I've heard.

Coding standard exist to make code easier to read and reduce errors.

This is one standard that truly pays off.

Jeffrey Hines
What are the benefits?
Robert Harvey
Robert; benefits would include avoiding introduction of accidental errors, and making readability of the code easier.
Dean J
Those are opinions not facts on the benefits
Matthew Whited
I suspect that if one took the time one could produce a metric about reduction of errors during maintenance of the code
Murph
One reason against it is that it takes up lines, and reduces what you can get on one screen. That may or may not be important to you, but aside from it being a little more typing that's all I can come up with.
David Thornley
@Matthew: and "it's ugly" isn't an opinion?
Laurence Gonsalves
Where did I say it was ugly or not? I think this entire question should be a wiki.
Matthew Whited
In over ten years I've only ever run into one bug caused by leaving out braces, which is why you should require braces and a comment when the body of a conditional statement is *empty*. I've entered many a brace in those same ten years, and left out many more. It's hard to say "truly pays off" to add all those braces back when it's never caused a bug. Focus on rules that actually pay off, like avoiding inconsistent double- or triple-negatives in conditionals (the `!!` that is sometimes useful in C++ is considered consistent).
280Z28
Just because it didn't truly pay off for you doesn't mean that it doesn't for other people.
Jeffrey Hines
Downside is noise. Extra code that doesn't mean anything just makes it harder to read code that does mean something. Its not a big deal in a contrived example, but it adds up
Matt Briggs
@Matt - The braces do mean something... They clearly define what will be executed in the then or else clause. In dense code it can be difficult to see the structure of the code.
Jeffrey Hines
For those saying that they have never made mistakes as a result of not using braces... One of the main reasons for Coding Standards is to put all of the code base and all of the programmers on even footing regardless of experience levels. You may be a super wizard coder, but just because you understand your code and you don't make mistakes it doesn't mean that the junior programmer that takes over your code when you leave for greener pastures will understand your masterpiece. Following Coding Standards shows basic respect for the people who inherit your code.
Jeffrey Hines
Depending on the language they craces also allow you to create your own blocks of scope. These blocks of scope could induce bugs by allowing you to reuse vaiable names and have them be declared at the wrong level... which, in turn, would be a bug causes by including the braces. There could be bugs either way. This is just a question that is begging for opinions on and as I have said several times should be a wiki.
Matthew Whited
A: 

I prefer adding braces to single-line conditionals for maintainability, but I can see how doing it without braces looks cleaner. It doesn't bother me, but some people could be turned off by the extra visual noise.

I can't offer a better counterargument either. Sorry! ;)

John at CashCommons
A: 

Depending on the language, having braces for a single lined conditional statement or loop statement is not mandatory. In fact, I would remove them to have fewer lines of code.

C++:

Version 1:

class InvalidOperation{};

//...
Divide(10, 0);
//...
Divide(int a, in b)
{
   if(b == 0 ) throw InvalidOperation();
   return a/b;
}

Version 2:

class InvalidOperation{};

//...
Divide(10, 0);
//...
Divide(int a, in b)
{
   if(b == 0 )
   { 
      throw InvalidOperation();
   }
   return a/b;
}

C#:

Version 1:

foreach(string s in myList)
   Console.WriteLine(s);

Version2:

foreach(string s in myList)
{
   Console.WriteLine(s);
}

Depending on your perspective, version 1 or version 2 will be more readable. The answer is rather subjective.

Partial
How small is your code repository's hard drive?!
jball
fewerLinesOfCode != betterCode
Gus
It has nothing to do with hard drive space...
Partial
For that matter, you can put your entire C++ file on a single line. Better readability doesn't slow down your code.
Will Eddins
I know they're not mandatory, that's why it can be standardized one way or the other. :-)
Dean J
@Gus: I never said it was better code. More lines of code is not necessarily better either. I do not understand your logic in saying such a thing.
Partial
@jball: that's not the motivation. vertical code density is way more important.
just somebody
@Will Eddins: Code readability is subjective. For me the version 1 is more readable than version 2.
Partial
@just somebody - what is the importance of "vertical code density"? This code is all compiled in the end. Even if it's javascript, write it for readability and mantainability, and run it through a minifier/obfuscator when you deploy to a production environment if "code density" is a concern.
jball
@Will Eddins: By the way, if(b == 0 ) throw InvalidOperation(); is not my entire code.
Partial
@jball: What you are saying is subjective.
Partial
Partial, my point is that having fewer lines of code is only worth it if there is a better-than-even trade off for it. I don't think that either is LESS readable, that's for certain...but I do think that having the braces there can prevent some maintenance bugs from popping up, and having the braces costs essentially nothing. Also, there needs to be a standard way of doing it. You cannot have "no braces" as an absolute standard, no one would argue otherwise. But you CAN have braces as a standard.
Gus
@jball: I believe @just somebody's point about vertical density is for viewing/editing the code, not compiling it. You can fit a lot more code on the screen at once if it's denser vertically (since most screens are wide format, line length is rarely a problem nowadays). I agree that having more code on the screen (i.e. denser) is better, and I also believe that putting in the extra braces is bad in part for this reason.
rmeador
@rmeador, I can understand that point somewhat, but I think even modest resolution screens can display at least 50 lines at once. Proper indentation and variable/method/class naming should make most code readable, even across 1000+ lines of code.
jball
Having a 1000 line conditional statement would be problematic.
Partial
@Partial I was of course referring to 1000 loc that also contained some conditionals wherein your desire for vertical density would be antagonised by braces for single statement conditionals.
jball
@jball: I know it was a joke.
Partial
foreach(string s in myList) { Console.WriteLine(s); }
Joe Philllips
@d03boy: What about that?
Partial
+5  A: 

I find it hard to argue with coding standards that reduce errors and make the code more readable. It may feel ugly to some people at first, but I think it's a perfectly valid rule to implement.

BryanD
+4  A: 

The one big advantage I see is that it's easier to add more statements to conditionals and loops that are braced, and it doesn't take many additional keystrokes to create the braces at the start.

jball
+5  A: 

I stand on the ground that braces should match according to indentation.

// This is right.
if (foo) 
{
    // bar
}
else 
{
    // baz
}

while (things) 
{
    // stuff
}

As far as your two examples, I'd consider yours slightly less readable since finding the matching closing parentheses can be hard, but more readable in cases where indentation is incorrect, while allowing logic to be inserted easier. It's not a huge difference.

Even if indentation is incorrect, the if statement will execute the next command, regardless of whether it's on the next line or not. The only reason for not putting both commands on the same line is for debugger support.

Will Eddins
I'm fine with either style of braces; I tend to standardize on whichever was already more common in the project.
Dean J
+1 for the readability!
David Glass
Yes. I loathe braces that don't match the indenting.
Loren Pechtel
A: 

For things like this, I would recommend just coming up with a configuration template for your IDE's autoformatter. Then, whenever your users hit alt-shift-F (or whatever the keystroke is in your IDE of choice), the braces will be automatically added. Then just say to everyone: "go ahead and change your font coloring, PMD settings or whatever. Please don't change the indenting or auto-brace rules, though."

This takes advantage of the tools available to us to avoid arguing about something that really isn't worth the oxygen that's normally spent on it.

Bob Cross
+4  A: 

My personal rule is if it's a very short 'if', then put it all on one line:

if(!something) doSomethingElse();

Generally I use this only when there are a bunch of ifs like this in succession.

if(something == a) doSomething(a);
if(something == b) doSomething(b);
if(something == b) doSomething(c);

That situation doesn't arise very often though, so otherwise, I always use braces.

mgroves
if (!something) { doSomethingElse(); } //There you go
Joe Philllips
@d03boy, How is the version with braces any better than the version without? The "accidentally comment it out" argument doesn't apply when it's one line. All your version does is add 4 characters.
tster
Its easier to read consistent code (code that always uses braces)
Joe Philllips
Is 4 extra characters really so bad?
Joe Philllips
reading a line which includes opening and closing braces is not at all consistent with normal coding styles. In fact, normally a closing brace would be on a line all to itself.
tster
Most boring argument ever.
mgroves
+2  A: 

Many languanges have a syntax for one liners like this (I'm thinking of perl in particular) to deal with such "ugliness". So something like:

if (foo)     
//bar
else     
//baz

can be written as a ternary using the ternary operator:

foo ? bar : baz

and

while (something is true)
{
blah 
}

can be written as:

blah while(something is true)

However in languages that don't have this "sugar" I would definitely include the braces. Like you said it prevents needless bugs from creeping in and makes the intention of the programmer clearer.

ennuikiller
+3  A: 

I am not saying it is unreasonable, but in 15+ years of coding with C-like languages, I have not had a single problem with omitting the braces. Commenting out a branch sounds like a real problem in theory - I've just never seen it happening in practice.

Nemanja Trifunovic
How often do you read other people's code?
Joe Philllips
All the time. Why?
Nemanja Trifunovic
+19  A: 

The best counter argument I can offer is that the extra line(s) taken up by the space reduce the amount of code you can see at one time, and the amount of code you can see at one time is a big factor in how easy it is to spot errors. I agree with the reasons you've given for including braces, but in many years of C++ I can only think of one occasion when I made a mistake as a result and it was in a place where I had no good reason for skipping the braces anyway. Unfortunately I couldn't tell you if seeing those extra lines of code ever helped in practice or not.

I'm perhaps more biased because I like the symmetry of matching braces at the same indentation level (and the implied grouping of the contained statements as one block of execution) - which means that adding braces all the time adds a lot of lines to the project.

Adam Bowen
+1. I'd rather see more lines of code on a page, and in 10 yrs I have never seen an error caused by this.
David
Consider if you put a debug print after the `if`. If you don't have braces, the `if` now affects only the debug print, and not the statement under it. I've been burned by this more than once when debugging code in which the brace rule wasn't enforced.
Nathan Fellman
I hate the braces on the same line with other code--they're hard to follow. If you put them on their own line that means less code on the screen. Thus I never use them in this sort of situation, I always use indenting to indicate control levels.
Loren Pechtel
+1 Braces for single lines just add noise without any real benefit. As long as the rule is use braces around any code that is more than one line, or if the construct (the if/while part above the braces) is more than a single line. This includes comments etc. i.e. you can omit the braces only in the simplest case where the code is easy to read and clear, so the lack of braces does not introduce any danger. If you add a comment, or split the if over 2 lines, then brace it up!
Jason Williams
@Nathan It's really not hard to notice the braces are missing. And sure it's a few more clicks and keystrokes to add them back in in order to add in that debug print - but not that many mark. I've been burned by this a few times too when I was tired and out of it. But I always took that as a sign to put down the keyboard and walk away - not a sign that I should change my code style standard.
Daniel Bingham
David, generally "being annoyed and confused" isn't an error. Its just a waste of time.
Joe Philllips
Perhaps ironically given my statements on "code you can see" I place the opening brace on the following line, which clearly distinguishes between the two cases; so I have no difficulty seeing whether or not braces are required. I'll grant that it's irritating to have to add them for debug statements. OTOH, if the company I worked for had a guideline that required them then I would follow it (I wrote the guidelines in my current job :)). As this relates to the op's post though, I think it's useful to understand both sides of the debate when it comes to choosing guidelines.
Adam Bowen
+1  A: 

Here are the unwritten (until now I suppose) rules I go by. I believe it provides readability without sacrificing correctness. It's based on a belief that the short form is in quite a few cases more readable than the long form.

  • Always use braces if any block of the if/else if/else statement has more than one line. Comments count, which means a comment anywhere in the conditional means all sections of the conditional get braces.
  • Optionally use braces when all blocks of the statement are exactly one line.
  • Never place the conditional statement on the same line as the condition. The line after the if statement is always conditionally executed.
  • If the conditional statement itself performs the necessary action, the form will be:

    for (init; term; totalCount++)
    {
        // Intentionally left blank
    }
    

No need to standardize this in a verbose manner, when you can just say the following:

Never leave braces out at the expense of readability. When in doubt, choose to use braces.

280Z28
+1  A: 

I think the important thing about braces is that they very definitely express the intent of the programmer. You should not have to infer intent from indentation.

That said, I like the single-line returns and continues suggested by Gus. The intent is obvious, and it is cleaner and easier to read.

Ray
Indentation shows my intent. Braces are just to tell the idiot language what I mean.
Tom Hawtin - tackline
+8  A: 

I see this rule as overkill. Draconian standards don't make good programmers, they just decrease the odds that a slob is going to make a mess.

The examples you give are valid, but they have better solutions than forcing braces:

When you don't brace a single-line, and then someone comments it out, you're in trouble.

Two practices solve this better, pick one:

1) Comment out the if, while, etc. before the one-liner with the one-liner. I.e. treat

if(foo)
    bar();

like any other multi-line statement (e.g. an assignment with multiple lines, or a multiple-line function call):

//if(foo)
//    bar();

2) Prefix the // with a ;:

if(foo)
;//    bar();

If you don't brace a single-line, and the indentation doesn't display the same on someone else's machine... you're in trouble.

No, you're not; the code works the same but it's harder to read. Fix your indentation. Pick tabs or spaces and stick with them. Do not mix tabs and spaces for indentation. Many text editors will automatically fix this for you.

Write some Python code. That will fix at least some bad indentation habits.

Also, structures like } else { look like a nethack version of a TIE fighter to me.

are there good reasons why this would be a mistaken or otherwise unreasonable standard? There's been some discussion on it, but no one can offer me a better counterargument than "it feels ugly".

Redundant braces (and parentheses) are visual clutter. Visual clutter makes code harder to read. The harder code is to read, the easier it is to hide bugs.

int x = 0;
while(x < 10);
{
    printf("Count: %d\n", ++x);
}

Forcing braces doesn't help find the bug in the above code.

P.S. I'm a subscriber to the "every rule should say why" school, or as the Dalai Lama put it, "Know the rules so that you may properly break them."

Mike DeSimone
Forcing braces prevents bugs due to ambiguity. See the dangling else problem for reference.
Pestilence
I don't work alone; that's the whole reason for standards. I can't enforce that folks remember to do something specific when commenting things out. I can enforce things that are easily checked for, though.
Dean J
+1 "visual clutter" is a true counter-argument for not using unnecessary braces. I would also agree that being able to see more code at once is better.
DanM
@Pestilence: Forcing consistence in if/else/elsif chains eliminates the dangling else problem too. Forcing braces everywhere is overkill
Matt Briggs
+1 for visual clutter. I wish people would stop making already verbose languages as verbose as they possibly can be in the name of good practice.
Matt Briggs
I fail to see how implementing a barely-used-in-the-industry practice like a semi before a comment is better than implementing an industry wide practice like bracing to solve concerns which you admit are valid.
Gus
@Pestilence: The dangling else problem is its own problem, and I agree with Matt on this. You could either require braces *in this case*, or allow a programmer to put in a `else ; // Do nothing` to match the `if()`. There's no need to go fully over to the other extreme and mandate braces in the other 90% of cases. @Dean: If you really, *really* want the Draconian rule, then find or write a lint-like tool that enforces it. A standard that isn't consistently enforced is wallpaper. @Gus: Agreed, that's why I listed it second. I wanted to point out that it is an alternative.
Mike DeSimone
@Mike: I guess the way I see it is that you NEED standards for this type of thing, and the divide over whether or not they should be required is proof of that. Imagine if everyone who answered this question was on a team...we would come up with all sorts of different ways of expressing conditional statements and the poor schmuck who gets to maintain it would have to sort through it all. Out of all of the ideas to prevent the valid concerns, braces really are the easiest to implement.
Gus
+1 for visual clutter and rules must have a reason. I have been programming without braces on single-line statements for 28 years, and I have never ever written a bug that related to the lack of optional braces. Nor have I known a colleague to do so. The "always brace" rule is a solution to a problem that does not actually exist as long as one is (a) a programmer and (b) has a tidy code/indentation style.
Jason Williams
@Gus: The best way to enforce coding rules is to have as *few* rules as possible, so that programmers have a chance to follow them. I can cite at least several hundred rules that are of more benefit than this one, so it simply does not make the cut.
Jason Williams
@Jason - So you are saying that as long as you follow coding standards, you don't need this coding standard?
Jeffrey Hines
@Gus: It depends on your goals as a development team. Is it more important to make code legible, i.e. make it look closer to a set of English instructions, or is it more important to make the coding standards short and simple through frequent use of "always" or "never"? The exact answer depends on the team; some work best in a rigid, structured environment, and some work bets in an environment where they know that rules can have exceptions, as long as the exceptions are justified. "The poor schmuck who gets to maintain it" should be *all* the developers (dog food rule); we use a Wiki.
Mike DeSimone
@Mike, I definitely see where you're coming from. Most of what I'm going for with implementing standards has to do with consistency in coding styles. As I said in my answer, I don't think that having a single "return" or "continue" statement in braces makes a lot of sense. @Jason, if a rule has ANY benefit, it is at least worth considering. And this one does. As I said, it promotes consistency, even if it does nothing else.
Gus
It's worth considering, but for it to be used, its benefits should exceed its costs. It seems Jason is saying that they have other coding practices in place that already deal with the problems that requiring braces is supposed to fix, so this rule doesn't pay for itself. It seems to depend on the development environment in question: At our research lab, we hire a lot of experienced developers who have habits in place to deal with these problems. It would be counterproductive to force them to conform to, essentially, alien rules.
Mike DeSimone
On the other extreme, a shop which churns out code like a factory, hires mostly college grads, and has a lot of turnover *needs* Draconian rules. For example, see Pestilence's answer below regarding dangling-else. For some reason, their engineers are allowed to copy and paste his code into theirs, and then shirk the responsibility for the results. Any engineer here who tried to pull that on me would get either simply mocked or systematically proven wrong in writing, as needed. This is why, after decades, there is still not One Code Style To Rule Them All.
Mike DeSimone
@all: read http://www.joelonsoftware.com/articles/fog0000000024.html
RCIX
It's good to decrease the chances that a slob will make a mess. A mitzvah, even.
Norman Ramsey
Norman: That's covered in the link RCIX supplied. Seriously, though... the easiest and most reliable way to keep a slob from making a mess is firing the slob. Barring that... we get this whole discussion. But it's important to remember that you are still just working around the potential damage a slob can cause.
Mike DeSimone
I like the nethack tie fighter. I mean, I use it.
egarcia
A: 

Wow. NO ONE is aware of the dangling else problem? This is essentially THE reason for always using braces.

In a nutshell, you can have nasty ambiguous logic with else statements, especially when they're nested. Different compilers resolve the ambiguity in their own ways. It can be a huge problem to leave off braces if you don't know what you're doing.

Aesthetics and readability has nothing to do it.

Pestilence
It C, C++, C#, and Java, the else unambiguously goes with the last unmatched if. The dangling else problem is a syntactic ambiguity that was trivially resolved ages age. For an example of non-trivial syntactic ambiguities, see my answer to this question: http://stackoverflow.com/questions/1172939/is-any-part-of-c-syntax-context-sensitive/1173038#1173038
280Z28
Why is this a reason for **always** using braces? Seems to me that this is an argument for using braces when you have a dangling else statement. Anyways, I think most of the people here are saying it's OK to omit the braces when it is a very simple if statement with no elses or anything.
tster
Pestilence
+3  A: 

At present, I work with a team that lives by this standard, and, while I'm resistant to it, I comply for uniformity's sake.

I object for the same reason I object to teams that forbid use of exceptions or templates or macros: If you choose to use a language, use the whole language. If the braces are optional in C and C++ and Java, mandating them by convention shows some fear of the language itself.

I understand the hazards described in other answers here, and I understand the yearning for uniformity, but I'm not sympathetic to language subsetting barring some strict technical reason, such as the only compiler for some environment not accommodating templates, or interaction with C code precluding broad use of exceptions.

Much of my day consists of reviewing changes submitted by junior programmers, and the common problems that arise have nothing to do with brace placement or statements winding up in the wrong place. The hazard is overstated. I'd rather spend my time focusing on more material problems than looking for violations of what the compiler would happily accept.

seh
The point of a standard for braces is you don't have to consider it any more, and can spend more time on more important issues.
anon
+2  A: 

Another advantage of always using braces is that it makes search-and-replace and similar automated operations easier.

For example: Suppose I notice that functionB is usually called immediately after functionA, with a similar pattern of arguments, and so I want to refactor that duplicated code into a new combined_function. A regex could easily handle this refactoring if you don't have a powerful enough refactoring tool (^\s+functionA.*?;\n\s+functionB.*?;) but, without braces, a simple regex approach could fail:

if (x)
  functionA(x);
else
  functionA(y);
functionB();

would become

if (x)
  functionA(x);
else
  combined_function(y);

More complicated regexes would work in this particular case, but I've found it very handy to be able to use regex-based search-and-replace, one-off Perl scripts, and similar automated code maintenance, so I prefer a coding style that doesn't make that needlessly complicated.

Josh Kelley
+3  A: 

I don't buy into your argument. Personally, I don't know anyone who's ever "accidentally" added a second line under an if. I would understand saying that nested if statements should have braces to avoid a dangling else, but as I see it you're enforcing a style due to a fear that, IMO, is misplaced.

rlbond
+1  A: 

If you have the time to read through all of this, then you have the time to add extra braces.

vrv
+2  A: 

The only way coding standards can be followed well by a group of programmers is to keep the number of rules to a minimum.

Balance the benefit against the cost (every extra rule confounds and confuses programmers, and after a certain threshold, actually reduces the chance that programmers will follow any of the rules)

So, to make a coding standard:

  • Make sure you can justify every rule with clear evidence that it is better than the alternatives.

  • Look at alternatives to the rule - is it really needed? If all your programmers use whitespace (blank lines and indentation) well, an if statement is very easy to read, and there is no way that even a novice programmer can mistake a statement inside an "if" for a statement that is standalone. If you are getting lots of bugs relating to if-scoping, the root cause is probably that you have a poor whitepsace/indentation style that makes code unnecessarily difficult to read.

  • Prioritise your rules by their measurable effect on code quality. How many bugs can you avoid by enforcing a rule (e.g. "always check for null", "always validate parameters with an assert", "always write a unit test" versus "always add some braces even if they aren't needed"). The former rules will save you thousands of bugs a year. The brace rule might save you one. Maybe.

  • Keep the most effective rules, and discard the chaff. Chaff is, at a minimum, any rule that will cost you more to implement than any bugs that might occur by ignoring the rule. But probably if you have more than about 30 key rules, your programmers will ignore many of them, and your good intentions will be as dust.

  • Fire any programmer who comments out random bits of code without reading it :-)

P.S. My stance on bracing is: If the "if" statement or the contents of it are both a single line, then you may omit the braces. That means that if you have an if containing a one-line comment and a single line of code, the contents take two lines, and therefore braces are required. If the if condition spans two lines (even if the contents are a single line), then you need braces. This means you only omit braces in trivial, simple, easily read cases where mistakes are never made in practice. (When a statement is empty, I don't use braces, but I always put a comment clearly stating that it is empty, and intentionally so. But that's bordering on a different topic: being explicit in code so that readers know that you meant a scope to be empty rather than the phone rang and you forgot to finish the code)

Jason Williams