<p>In the case of languages that support single decision and action without brackets, such as the following example:</p>

<pre><code>if (var == true)
    doSomething();
</code></pre>

<p>What is the preferred way of writing this? Should brackets always be used, or should their usage be left as a preference of the individual developer? Additionally, does this practice depend on the size of the code block, such as in the following example:</p>

<pre><code>if (var == 1)
    doSomething(1);
else if (var &gt; 1 &amp;&amp; var &lt; 10)
    doSomething(2);
else
{
    validate(var);
    doSomething(var);
}
</code></pre>

[{'answer_id': 11102, 'author': 'Stu', 'author_id': 414, 'author_profile': 'https://Stackoverflow.com/users/414', 'pm_score': 2, 'selected': False, 'text': "<p>It doesn't really matter, as long as you're consistent with it.</p>\n\n<p>There does seem to be a tendency to demand sameness within a single statement, i.e. if there's brackets in one branch, there's brackets everywhere. The Linux kernel coding standards, for one, mandate that.</p>\n"}, {'answer_id': 11105, 'author': 'Chris Benard', 'author_id': 448, 'author_profile': 'https://Stackoverflow.com/users/448', 'pm_score': 0, 'selected': False, 'text': "<p>Our boss makes us put { } after a decision statement no matter what, even if it's a single statement.  It's really annoying to add two extra lines.  The only exception is ternary operators.</p>\n\n<p>I guess it's a good thing I have my code monitor in portrait orientation at 1200x1600.</p>\n"}, {'answer_id': 11106, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 2, 'selected': False, 'text': "<p>My preference is to be consistent, e.g., if you use brackets on one block, use brackets all throughout even with just one statement:</p>\n\n<pre><code>if (cond1)\n{\n   SomeOperation();\n   Another();\n}\nelseif (cond2)\n{\n   DoSomething();\n}\nelse\n{\n   DoNothing();\n   DoAnother();\n}\n</code></pre>\n\n<p>But if you have just a bunch of one liners:</p>\n\n<pre><code>if (cond1)\n    DoFirst();\nelseif (cond2)\n    DoSecond();\nelse\n    DoElse();\n</code></pre>\n\n<p>Looks cleaner (if you don't mind the dummy method names ;) that way, but that's just me.</p>\n\n<p>This also applies to loop constructs and the like:</p>\n\n<pre><code>foreach (var s as Something)\n    if (s == someCondition)\n        yield return SomeMethod(s);\n</code></pre>\n\n<p>You should also consider that this is a convention that might be more suited to .NET (notice that Java peepz like to have their first curly brace in the same line as the if).</p>\n"}, {'answer_id': 11107, 'author': 'stimms', 'author_id': 361, 'author_profile': 'https://Stackoverflow.com/users/361', 'pm_score': 4, 'selected': True, 'text': "<p>There isn't really a right answer.  This is what coding standards within the company are for.  If you can keep it consistent across the whole company then it will be easy to read.  I personally like</p>\n\n<pre><code>if ( a == b)    {\n    doSomething();\n}\nelse {\n    doSomething();\n}\n</code></pre>\n\n<p>but this is a holy war.  </p>\n"}, {'answer_id': 11110, 'author': 'Ryan Ahearn', 'author_id': 75, 'author_profile': 'https://Stackoverflow.com/users/75', 'pm_score': 1, 'selected': False, 'text': "<p>I've always used brackets at all times except for the case where I'm checking a variable for NULL before freeing it, like is necessary in C</p>\n\n<p>In that case, I make sure it's clear that it's a single statement by keeping everything on one line, like this:</p>\n\n<pre><code>if (aString) free(aString);\n</code></pre>\n"}, {'answer_id': 11116, 'author': 'ZombieSheep', 'author_id': 377, 'author_profile': 'https://Stackoverflow.com/users/377', 'pm_score': 3, 'selected': False, 'text': '<p>I recommend</p>\n\n<pre><code>if(a==b)\n{\n    doSomething();\n}\n</code></pre>\n\n<p>because I find it far easier to do it up-front than to try to remember to add the braces when I add a second statement to to success condition...</p>\n\n<pre><code>if(a==b)\n    doSomething();\n    doSomethingElse();\n</code></pre>\n\n<p>is very different to </p>\n\n<pre><code>if(a==b)\n{\n    doSomething();\n    doSomethingElse();\n}\n</code></pre>\n\n<p>see <a href="http://www.joelonsoftware.com/articles/Wrong.html" rel="nofollow noreferrer">Joel\'s article</a> for further details</p>\n'}, {'answer_id': 11117, 'author': 'Kev', 'author_id': 419, 'author_profile': 'https://Stackoverflow.com/users/419', 'pm_score': 3, 'selected': False, 'text': "<p>I tend to use braces at all times. You can get some subtle bugs where you started off with something like:</p>\n\n<pre><code>if(something)\n DoOneThing();\nelse\n  DoItDifferently();\n</code></pre>\n\n<p>and then decide to add another operation to the <code>else</code> clause and forget to wrap it in braces:</p>\n\n<pre><code>if(something)\n DoOneThing();\nelse\n  DoItDifferently();\n  AlwaysGetsCalled(); \n</code></pre>\n\n<p><code>AlwaysGetsCalled()</code> will always get called, and if you're sitting there at 3am wondering why your code is behaving all strange, something like that could elude you for quite some time. For this reason alone, I always use braces.</p>\n"}, {'answer_id': 11118, 'author': 'Eldila', 'author_id': 889, 'author_profile': 'https://Stackoverflow.com/users/889', 'pm_score': 1, 'selected': False, 'text': '<p>There is no right or wrong way to write the above statement. There are plenty of accepted coding <a href="http://en.wikipedia.org/wiki/Indent_style" rel="nofollow noreferrer">styles</a>. However, for me, I prefer keeping the coding style consist throughout the entire project. ie. If the project is using K&amp;R style, you should use K&amp;R.</p>\n'}, {'answer_id': 11119, 'author': 'Nick', 'author_id': 1236, 'author_profile': 'https://Stackoverflow.com/users/1236', 'pm_score': 2, 'selected': False, 'text': "<p>I would strongly advocate <em>always</em> using braces, even when they're optional. Why? Take this chunk of C++ code:</p>\n\n<pre><code>if (var == 1)\n  doSomething();\ndoSomethingElse();\n</code></pre>\n\n<p>Now, someone comes along who isn't really paying enough attention and decides that something extra needs to happen if (var == 1), so they do this:</p>\n\n<pre><code>if (var == 1)\n  doSomething();\n  doSomethingExtra();\ndoSomethingElse();\n</code></pre>\n\n<p>It's all still beautifully indented but it won't do what was intended.</p>\n\n<p>By always using braces, you're more likely to avoid this sort of bug.</p>\n"}, {'answer_id': 11120, 'author': 'Pascal Paradis', 'author_id': 1291, 'author_profile': 'https://Stackoverflow.com/users/1291', 'pm_score': 0, 'selected': False, 'text': '<p>I tend to agree with Joel Spolsky on that one with that article (<a href="http://www.joelonsoftware.com/articles/Wrong.html" rel="nofollow noreferrer">Making Wrong Code Look Wrong</a>) with the following code example : </p>\n\n<pre><code>if (i != 0)\nbar(i);\nfoo(i);\n</code></pre>\n\n<p>Foo is now unconditionnal. Wich is real bad!</p>\n\n<p>I always use brackets for decision statements. It helps code maintainability and it makes the code less bug prone.</p>\n'}, {'answer_id': 11134, 'author': 'Baltimark', 'author_id': 1179, 'author_profile': 'https://Stackoverflow.com/users/1179', 'pm_score': 0, 'selected': False, 'text': '<p>I prefer </p>\n\n<pre><code>if (cond)\n   {\n   //statement\n   }\n</code></pre>\n\n<p>even with only a single statement. If you were going to write something once, had no doubts that it worked, and never planned on another coder ever looking at that code, go ahead and use whatever format you want. But, what does the extra bracketing really cost you? Less time in the course of a year than it takes to type up this post. </p>\n\n<p>Yes, I like to indent my brackets to the level of the block, too. </p>\n\n<p>Python is nice in that the indentation defines the block. The question is moot in a language like that. </p>\n'}, {'answer_id': 11167, 'author': 't3rse', 'author_id': 64, 'author_profile': 'https://Stackoverflow.com/users/64', 'pm_score': 0, 'selected': False, 'text': '<p>I used to follow the "use curly braces always" line like an apparatchik. However, I\'ve modified my style to allow for omitting them on single line conditional expressions: </p>\n\n<pre><code>if(!ok)return;\n</code></pre>\n\n<p>For any multistatement scenario though I\'m still of the opinion that braces should be mandatory: </p>\n\n<pre><code>if(!ok){\n\n    do();\n\n    that();\n\n    thing();\n}\n</code></pre>\n'}, {'answer_id': 11283, 'author': 'James A. Rosen', 'author_id': 1190, 'author_profile': 'https://Stackoverflow.com/users/1190', 'pm_score': 1, 'selected': False, 'text': '<p>Ruby nicely obviates one issue in the discussion.  The standard for a one-liner is:</p>\n\n<pre><code>do_something if (a == b)\n</code></pre>\n\n<p>and for a multi-line:</p>\n\n<pre><code>if (a == b)\n  do_something\n  do_something_else\nend\n</code></pre>\n\n<p>This allows concise one-line statements, but it forces you to reorganize the statement if you go from single- to multi-line.</p>\n\n<p>This is not (yet) available in Java, nor in many other languages, AFAIK.</p>\n'}, {'answer_id': 11542, 'author': 'Adam Lassek', 'author_id': 1249, 'author_profile': 'https://Stackoverflow.com/users/1249', 'pm_score': 1, 'selected': False, 'text': "<p>As others have mentioned, doing an if statement in two lines without braces can lead to confusion:</p>\n\n<pre><code>if (a == b)\n    DoSomething();\n    DoSomethingElse(); &lt;-- outside if statement\n</code></pre>\n\n<p>so I place it on a single line if I can do so without hurting readability:</p>\n\n<pre><code>if (a == b) DoSomething();\n</code></pre>\n\n<p>and at all other times I use braces.</p>\n\n<p>Ternary operators are a little different. Most of the time I do them on one line:</p>\n\n<pre><code>var c = (a == b) ? DoSomething() : DoSomethingElse();\n</code></pre>\n\n<p>but sometimes the statements have nested function calls, or lambda expressions which \nmake a one-line statement difficult to parse visually, so I prefer something like this:</p>\n\n<pre><code>var c = (a == b)\n    ? AReallyReallyLongFunctionName()\n    : AnotherReallyReallyLongFunctionOrStatement();\n</code></pre>\n\n<p>Still more concise than an if/else block but easy to see what's going on.</p>\n"}, {'answer_id': 11557, 'author': 'Rytmis', 'author_id': 266, 'author_profile': 'https://Stackoverflow.com/users/266', 'pm_score': 2, 'selected': False, 'text': '<p>Chalk this one to lack of experience, but during my seven-year stint as a code monkey I\'ve <em>never</em> actually seen anyone make the mistake of not adding braces when adding code to a block that doesn\'t have braces. That\'s precisely <em>zero</em> times. </p>\n\n<p>And before the wisecrackers get to it, no, the reason wasn\'t "everyone always uses braces".</p>\n\n<p>So, an honest question -- I really would like to get actual replies instead of just downvotes: does that ever actually happen?</p>\n\n<p>(Edit: I\'ve heard enough outsourcing horror stories to clarify a bit: does it ever actually happen to <strong>competent programmers</strong>?)</p>\n'}, {'answer_id': 39418, 'author': 'Dewm Solo', 'author_id': 4225, 'author_profile': 'https://Stackoverflow.com/users/4225', 'pm_score': 2, 'selected': False, 'text': '<p>I personnally side with McConnell\'s explanation from Code Complete.</p>\n\n<p>Use them whenever you can. They enhance your code\'s readability and remove the few and scarce confusions that might occur.</p>\n\n<p>There is one thing that\'s more important though....Consistency. Which ever style you use,make sure you always do it the same way.</p>\n\n<p>Start writing stuff like:</p>\n\n<pre>\n<code>\nIf A == true\n   FunctA();\n\nIf B == "Test"\n{\n   FunctB();\n}\n</code>\n</pre>\n\n<p>You are bound to end up looking for an odd bug where the compiler won\'t understand what you were trying to do and that will be hard to find.</p>\n\n<p>Basically find the one you are comfortable writing everytime and stick to it. I do believe in using the block delimeters(\'{\', \'}\') as much as possible is the way to go.</p>\n\n<p>I don\'t want to start a question inside another, but there is something related to this that I want to mention to get your mental juices going. One the decision of using the brackets has been made. Where do you put the opening bracket? On the same line as the statement or underneath. Indented brackets or not?</p>\n\n<pre>\n<code>\nIf A == false {\n  //calls and whatnot\n}\n//or\nIf B == "BlaBla"\n{\n  //calls and whatnot\n}\n//or\nIf C == B\n  {\n  //calls and whatnot\n  }\n</code>\n</pre>\n\n<p>Please don\'t answer to this since this would be a new question. If I see an interest in this I will open a new question your input.</p>\n'}, {'answer_id': 77547, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>I use curly braces around every statement if and only if at least one of them requires it.</p>\n'}, {'answer_id': 78060, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>Sun\'s <a href="http://java.sun.com/docs/codeconv/" rel="nofollow noreferrer">Code Conventions for the Java programming Language</a> has <a href="http://java.sun.com/docs/codeconv/html/CodeConventions.doc6.html#449" rel="nofollow noreferrer">this</a> to say:</p>\n\n<blockquote>\n  <p>The if-else class of statements should\n  have the following form:</p>\n\n<pre><code>if (condition) {\n    statements;\n}\n\nif (condition) {\n    statements;\n} else {\n    statements;\n}\n\nif (condition) {\n    statements;\n} else if (condition) {\n    statements;\n} else {\n    statements;\n}\n</code></pre>\n</blockquote>\n'}, {'answer_id': 85924, 'author': 'Brad Gilbert', 'author_id': 1337, 'author_profile': 'https://Stackoverflow.com/users/1337', 'pm_score': 0, 'selected': False, 'text': '<p>In Perl if you are doing a simple test, sometime you will write it in this form:</p>\n\n<pre><code>do_something if condition;\n\ndo_something unless condition;\n</code></pre>\n\n<p>Which can be really useful to check the arguments at the start of a subroutine.</p>\n\n<pre><code>sub test{\n  my($self,@args) = @_;\n\n  return undef unless defined $self;\n\n  # rest of code goes here\n\n}\n</code></pre>\n'}, {'answer_id': 639794, 'author': 'David Thornley', 'author_id': 14148, 'author_profile': 'https://Stackoverflow.com/users/14148', 'pm_score': 0, 'selected': False, 'text': "<p>The golden rule is that, when working in an existing project, follow those coding standards.</p>\n\n<p>When I'm at home, I have two forms.</p>\n\n<p>The first is the single line:</p>\n\n<pre><code>if (condition) doThis();\n</code></pre>\n\n<p>and the second is for multiple lines:</p>\n\n<pre><code>if (condition) {\n   doThis();\n}\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/11099', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1185/']