if statement in for loop, brackets needed?

Started by
31 comments, last by Nicholas Kong 11 years, 2 months ago
Well, the only protection against idiotic programmers is better hiring and firing decisions. Bracketing conventions won't do. smile.png
Advertisement

For me brackets or not seems just like a matter of style. If there are none and you add a second line, surely you should know you also need to add brackets.

But putting the body on the same line as the if is annoying if you happen to want to put a breakpoint there or single step the if, as you cant easily see then if the body was actually run.

There is only one case where I feel its acceptable to omit braces.

It's for one line tests with simple expressions like this:

if(!some_object) some_object = new SomeClass();

If you want to add a second line, you have to start with adding a line break, and at that point, it's very obvious that you need to add brackets too.

Even with stress levels high.

For me brackets or not seems just like a matter of style. If there are none and you add a second line, surely you should know you also need to add brackets.

Well, the only protection against idiotic programmers is better hiring and firing decisions. Bracketing conventions won't do.

Easy to say until that day you sit there with the deadline looming over your head, with stress levels at max, trying to fix that last show stopper bug.

The "just going to add some print out" happens very easily... And you really dont want to spend even 5 minutes with something like that, totally breaks your flow.

Clean and consistent code = less bugs and frustration

always.

I have spent decades coding in C-like languages. I have seen a significantly disproportionate number of software error arising from not using braces in control statements than from (almost) any other construct. The 'other construct' is mistaking '=' for '==' in comparisons, but that's less easily avoided.

The problem doesn't come from writing new code. The problem comes from maintaining existing code. Remember that all code is existing code the moment you're finished typing it.

I have worked on projects in which the formalized and enforced coding style dictates that a single line in a control construct should never use braces, likely in an attempt to save on electrons. With one significant exception, every such project has had a higher bug rate and higher maintenance cost. The one exception has a very large automated test suite (which takes hours to run) but is under very restricted maintenance, since it's a standard library with strongly defined functionality and wide distribution.

There are, of course, strong opinions on the aesthetic of brace usage in coding style. Such opinions are significantly more important than practical issues such as minimizing maintenance costs or maximizing software reliability.

Stephen M. Webb
Professional Free Software Developer

I have programmed in C-like languages for aboud 20 years now, 13 of them professionally. I have never ever seen a problem with the use of braces. So I don't think it's that important.

It could also depend on what editor you use. Emacs understands the level of indentation you are at, so when you go to add a second line to the `then' clause, the cursor will go to a place where it's obvious that you need braces.

I agree with everyone that the most clear style should be used. So use whatever you think is more clear.

Ho boy, a bracketing convention debate.

My advice is to always use braces.

In practice, I add braces only when I need them, but don't remove them when the need disappears, such as if debug code is removed or the code is reformatted.

Also, I always use a newline after all if statements and non-empty loops. Empty loops get a pair of braces on the same line.

Empty loops get a pair of braces on the same line.


That's an interesting idea. I use empty braces on the same line for an empty function or class definition. My empty loops look like this:
while(*(src++) = *(dst++))

;

I guess I just feel like loop bodies should start on the next line, no matter what. I'm also strongly against semi-colon on the same line to prevent things like this:
for(;;);
{
//Why am I not executed as expected?
}

I have programmed in C-like languages for aboud 20 years now, 13 of them professionally. I have never ever seen a problem with the use of braces.

I find that hard to belive that in 13 years of proffesional coding you have never once found a bug written by either you or by someone else that involved incorrect use of braces.


I have programmed in C-like languages for aboud 20 years now, 13 of them professionally. I have never ever seen a problem with the use of braces.


I find that hard to belive that in 13 years of proffesional coding you have never once found a bug written by either you or by someone else that involved incorrect use of braces.


I'm telling you, I can't remember one. As I said, emacs makes it very obvious if you are writing at the wrong level of indentation. Also, perhaps our testing procedures are good at catching that type of mistake.

I have programmed in C-like languages for aboud 20 years now, 13 of them professionally. I have never ever seen a problem with the use of braces. So I don't think it's that important.

I have. Once. (though I've got much less experience than you). It was in the FFmpeg source code. Indentation levels indicated the else statement was part of a different if block, but actual control flow was otherwise. The actual logic ended up being correct, but anyone reading the code was likely to misread the code (like I did initially) because of incorrect indentation levels and a complete lack of braces. That was a fun day.

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

This topic is closed to new replies.

Advertisement