4

This type of conditional statement is generally frowned upon

if(condition)
  statement;

because if you add another statement, like this

if(condition)
  statement;
  statement;

it gives the impression that the second statement is part of the if block when it isn't.

That said, my IDE does automatic indentation, which I think makes this problem moot. Furthermore, when I want to add another statement, it automatically does the bracket completion--which makes the issue about having to manually add brackets when you want to add another statement moot.

Is there any reason why this would still be bad style?

7
  • 1
    As an aside, if the language you're using supports ternary operators, why not consider using them in cases like this? IMHO someBoolVar = (cond)? true : false; is a little more cleaner than a fat if...else block with superfluous braces.
    – Bhargav
    Apr 25, 2012 at 3:13
  • 5
    Are you working on a team? Are you mandating that everybody on the team use the same editor? What about future maintainers are they expected to fix up code and add {} every time they expand a condition block. Apr 25, 2012 at 4:32
  • 2
    Almost duplicate of: programmers.stackexchange.com/questions/16528/…. Use a white-space sensitive language, that way you don't have to bother with braces at all :)
    – Benjol
    Apr 25, 2012 at 7:19
  • 2
    did you intend to set title like "assuming the bracketless if statement is frowned upon, but is it still..." and begin the question text with "If this type of conditional statement is generally frowned upon..."? The way it is stated now, without assuming and if, you'll probably will be getting a lot of irrelevant answers about whether bracketless is good or bad
    – gnat
    Apr 25, 2012 at 11:24
  • 1
    @BhargavBhat posters case wouldn't be if{line 1;} else {line 2;}... his case would be if {line 1; line 2;...}. Different needs. Ternary operators wouldn't be useful in this question.
    – WernerCD
    Apr 25, 2012 at 12:50

6 Answers 6

27

Yes, because not everyone on your team may be using that particular IDE. In general, it's still pretty dangerous since most IDEs/editors don't have this functionality so it can still let bugs creep in.

Also, many people prefer the brackets for readability purposes, since it makes it more clear that certain code is associated with an if statement.

5
  • 2
    Most IDEs don't do automatic indentation? I can't think of a single one that doesn't. Even text editors like gEdit and Notepad++ do this. Apr 25, 2012 at 12:43
  • 2
    @root45 What about VIM? Or notepad? Older version of gEdit/n++? Not every IDE is built equally. Not every VERSION of the same IDE is built equally.
    – WernerCD
    Apr 25, 2012 at 12:52
  • @WernerCD Notepad? Irrelevant. As are “older versions of X”. All modern editors support this. Including Vim, of course. Apr 25, 2012 at 13:15
  • 3
    @WernerCD Of course vim does. Use the autoindent option, which I believe is enabled by default. I'm not saying you can't find an editor that doesn't do it, just that "most IDEs/editors don't" is probably incorrect, and the opposite is probably true. Apr 25, 2012 at 13:39
  • 1
    All editors may support this, but it just takes one developer on your team with this feature turned off in his editor to cause a world of hurt. May 7, 2012 at 22:09
17

Alright, I'll be the one to state the unspoken. If omitting the brackets was such a serious offense, it would show up with -Wall, and none of the newer languages would let you do it at all. It is nowhere near a universal preference, and a lot of people even remove the brackets from existing code because it removes clutter and clarifies you only intended to include one statement. It's a rookie mistake because the code won't pass even basic testing, and in my experience the following mistake is more common among rookies:

if (condition);
{
    statement;
}

Probably because autoindent won't catch this bug, but will catch the one cited as the reason for including the superfluous brackets. Autoindent has been a feature of every single programmer's editor for decades, and if you don't use it, you deserve to get stymied by stupid syntax errors.

4
  • 3
    This. I hate brackets for what could be a single line statement. Unless you are being paid by LOC then there is no need for this clutter.
    – maple_shaft
    Apr 25, 2012 at 12:05
  • 4
    +1. It's definitely not universal preference, and it confuses me when people assume it is. I definitely don't buy that people would mistakenly add a second line to an if block without realizing it will always execute. Apr 25, 2012 at 14:59
  • It is not an offense in the same way that pointers are not an offense. It is just a known trap for errors, just as pointers are.
    – user1249
    May 8, 2012 at 9:16
  • Thanks for stating the minority view (or majority, maybe). I only use brackets if I have more than one line within the if or else. On a related note, when writing in English, you don't use bullets for a list that has only one item ;) Apr 23, 2013 at 17:37
5

Readability and maintainability are, in most circumstances, the two most important attributes of any piece of code. Using conditional logic without braces reduces readability, which is why it's considered bad style. Having a fancy IDE doesn't turn bad style into good style. Don't do it.

6
  • By your logic, languages like Python are by their inherent nature unreadable garbage.
    – maple_shaft
    Apr 25, 2012 at 12:08
  • 2
    I love Python, but to be honest, sometimes nested if, elif blocks ARE actually hard for me to read without any sort of braces separating the statements. Apr 25, 2012 at 12:11
  • 6
    Citation needed for logic without braces reduces readability. Is that opinion, or do studies show..? I used to have similar opinions, and then I read the code examples in Wirth's Programming in Oberon and got a lot less pedantic about mechanically-reproducible formatting. Apr 25, 2012 at 13:00
  • (Also: hello David, long time no see :-)) Apr 25, 2012 at 13:00
  • 1
    ... S.Robins' excellent answer, then please don't ever apply for a job on my team. Apr 26, 2012 at 6:43
5

Whether or not to use the brace is a personal (and/or team) preference. There are pros to cons about each. The reason for picking sides is to be consistent, and to produce code that is easiest for you to understand and maintain (and by "you" I mean "your team" if you're working in a team environment).

Your question seems to be not whether one is better than the other, but whether you should let your IDE tell you what is best. And to that question I say a resounding NO. Choose your coding conventions based on what you think is best, or your team if you're working on a team.

2

One of the nice things about using block delimiters (I.e.: Braces in C-Styled languages, or begin/end in Pascal-like languages) is that when used singly and on a separate line, they introduce additional white-space in your code. Now, without getting into a battle over hanging-brace vs inline-brace style choices, the point is simply that insofar as the compiler doesn't care either way, adding a little extra space helps to improve readability (particularly for those of us with eyes that aren't quite as young as they used to be!!). It's the same trick that you use when you create a resume. More white space makes things stand out better by themselves.

The other reasons to use block delimiters is to make it absolutely clear where something begins and ends. Sure, you can do the following:

DoTheFirstThing();
if (condition)
DoSomething();
DoSomethingElse();

Yet, when reading the code, the eyes can easily skip through the code, and you end up making assumptions about what has occurred as a part of the if statement. Whereas if you were to add a couple of braces:

DoTheFirstThing();
if (condition)
{
DoSomething();
}
DoSomethingElse();

you are making it clearer where the if block actually begins and ends. Of course, if you are pedantic about your indentation, you might argue that a simple indent is enough. Sure if you are used to using a language like Python, you may be used to paying particular attention to your indentation, but does it really make things much clearer? Let's look at a couple more examples. Decide which if statement stands out as the clearest when you take a really quick glance at the following:

DoTheFirstThing();
if (condition)
    DoSomething();
DoSomethingElse();
DoTheSecondThing();
if (condition)
{
    DoSomething();
}
DoSomethingElse();

Now do the same as the previous example, with this:

DoTheFirstThing();
if (condition)
    DoSomething();
DoSomethingElse();
DoTheFirstThing();

if (condition)
{
    DoSomething();
}

DoSomethingElse();

Out of the two examples, the very last if statement with the greater amount of whitespace stands out, and the code appears clearer, even though it is exactly the same.

So to really answer the OP's question, is it Bad to bunch things up and avoid brackets? My answer is no, and that I think calling it bad or poor or some other negative is laying it on a bit strongly. The compiler doesn't really care, and the code will work exactly the same. However, it is certainly more readable if you add the braces, and is shows the purpose of the If statement more clearly. More importantly perhaps, is that it shows that you are taking a little care to ensure the quality of your work stands out. It's like the difference between quickly painting a wall with a wide paint-brush once, or using a roller and carefully applying two coats of paint. Just as it is important to look at the finish of a paint job, so to the extra care taken in your code shows that you are willing to take the time to ensure others will not find it difficult to read through your work. This pays itself back when you need to revisit the code months or even years later, or when someone else needs to support you code after you're done.

Carefully written, clean, readable code inspires confidence in the code, while messy looking code will usually inspire an it's awful and too hard to work with attitude towards the code. Attention to detail shows a difference between the careful professional, and the less careful programmer. It inspires trust in your work, and is a pleasure to work with.

4
  • I'm not sure I agree, to me, the brackets just seem to add unecessary visual noise. If you need more whitespace separations, you can add an empty line afterwards. Apr 25, 2012 at 17:12
  • The point isn't to make DoSomething "stand out," it's to more clearly associate it with the condition. Also, putting blank lines before and after the bracketless version is a more apt comparison. Apr 25, 2012 at 17:56
  • @KarlBielefeldt I believe I made the point with regards to association in the paragraphs surrounding the first 2 examples of my answer, while the concept of adding space to make things stand out is shown in the last example of my answer. The point of my answer isn't to say that braces are only to make things stand out, but rather to show that this is an additional benefit.
    – S.Robins
    Apr 25, 2012 at 23:55
  • @PeterOlson In terms of visual noise, braces are quite discrete. I'd argue that excessive use of square/round brackets, or using braces inline tends to be more "noisy" visually than using braces to delineate code blocks. Braces are meant to define the scope of a block of executable code, and for this purpose I would consider them not noisy, however if you were to argue that having nested code blocks can become disorienting, then I'd likely agree with you to that extent.
    – S.Robins
    Apr 26, 2012 at 0:01
1

Code quality is independent of the IDE used to generate it (past tense!). The IDE that will be used to change it might alleviate some code problems. But this is in the future. You should strive for quality now.

As you can see from the other answers, some people do not consider this to be bad style. But I suppose most would agree that mixing two styles is much worth than < whatever coding style you despise goes here >. So if you are on a team or work with an existing consistent code base, stick to the rules. IMHO that's the professional thing to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.