Why Is It Considered a Bad Practice to Omit Curly Braces

Why is it considered a bad practice to omit curly braces?

Actually, the only time that's ever really bit me was when I was debugging, and commented out bar():

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

Other than that, I tend to use:

if(foo) bar();

Which takes care of the above case.

EDIT Thanks for clarifying the question, I agree, we should not write code to the lowest common denominator.

Why is it considered a bad practice to omit curly braces?

Actually, the only time that's ever really bit me was when I was debugging, and commented out bar():

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

Other than that, I tend to use:

if(foo) bar();

Which takes care of the above case.

EDIT Thanks for clarifying the question, I agree, we should not write code to the lowest common denominator.

Why is it considered a bad practice to omit curly braces?

Actually, the only time that's ever really bit me was when I was debugging, and commented out bar():

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

Other than that, I tend to use:

if(foo) bar();

Which takes care of the above case.

EDIT Thanks for clarifying the question, I agree, we should not write code to the lowest common denominator.

Is it a bad practice to use an if-statement without curly braces?

The problem with the first version is that if you go back and add a second statement to the if or else clauses without remembering to add the curly braces, your code will break in unexpected and amusing ways.

Maintainability-wise, it's always smarter to use the second form.

EDIT: Ned points this out in the comments, but it's worth linking to here, too, I think. This is not just some ivory-tower hypothetical bullshit: https://www.imperialviolet.org/2014/02/22/applebug.html

Is it ok if I omit curly braces in Java?

It won't change anything at all apart from the maintainability of your code. I've seen code like this:

for (int i = 0; i < size; i++)
a += b;
System.out.println("foo");

which means this:

for (int i = 0; i < size; i++)
a += b;
System.out.println("foo");

... but which should have been this:

for (int i = 0; i < size; i++) {
a += b;
System.out.println("foo");
}

Personally I always include the brackets to reduce the possibility of confusion when reading or modifying the code.

The coding conventions at every company I've worked for have required this - which is not to say that some other companies don't have different conventions...

And just in case you think it would never make a difference: I had to fix a bug once which was pretty much equivalent to the code above. It was remarkably hard to spot... (admittedly this was years ago, before I'd started unit testing, which would no doubt have made it easier to diagnose).

JavaScript: When we can omit curly brackets?

You can omit curly braces anytime there is only one statement.

for (var x in obj) {
console.log(x);
}

can become

for (var x in obj)
console.log(x);

but if you had

for (var x in obj) {
console.log(x);
console.log(x);
}

it would need the curly braces.


This is somewhat inconsistent. For example, it works for for, while and if, but not for try or catch. For consistency, I always use braces.

Comments in if statement without curly braces

Comments have no effect on the code other than the fact that they help to understand and edit code later.

The code you have shown is valid.

If the if statement is followed by codes inside curly braces all the codes inside the brace will get executed if the condition for if is met.
If there is no curly braces to group the code, the statement immediately after the if statement gets executed. If there is comments before this statement it will not effect the code as the comments will be removed when the code is compiled.

What is happening in this C program with an 'empty' if without curly braces and return after new lines?

Then the very next line of code is considered to be its body.

So this:

if(0)

return 1;

has the same effect with this:

if (0) {
return 1;
}

Now as for your, method, because the condition of the if statement is hardcoded to 0, it will always evaluate to false, and the return statement will not execute.

return 3; won't execute either, since it's commented. So your method will terminate without executing any return statement, despite the fact that it's defined to return an int.

This code invokes Undefined Behavior (UB), because we cannot tell what value it will return for sure, because it lacks an effective return statement. To you it happened to be 1, today. In my laptop it may be garbage. This code is, therefore, wrong.

I strongly recommend that you enable your compiler warnings, e.g. by passing Wall and/or Wextra flags when compiling with GCC, in order to get warned for such logical errors.

Read more in Function returns value without return statement.



Related Topics



Leave a reply



Submit