5
votes

When I started developing, I followed tutorials that always used {} (curly braces) to enclose blocks. However, when I began looking at other peoples code (classes on GitHub for example, or just more code in general than what a basic tutorial would show), however I've also seen block statements without being enclosed in {}, for example;

if($var < 15)
     $string = 'Hello, Jimmy!';
elseif($var >= 15)
     $string = 'Hello, Anne!';

Is the same as

if($var < 15) { 
    $string = 'Hello, Jimmy!';
} elseif($var >= 15) {
    $string = 'Hello, Anne!';
}

I've never used blocks not enclosed in {}, however I used them today and I'm starting to see the efficiency of doing so (it looks a lot cleaner too, as I'll often find my functions riddled with {} from loops, conditionals etc.

What I'm asking is;

a) are there any limitations on blocks without curly braces (; I noticed my IDE dropped back from an indent after I enter a single line and returned after an if() conditional?

b) are there any best practices to be had, when not using {}?

Any answers, specifically those inc. background/docs on the convention of curly brace usage for blocks vs. not using them would be greatly appreciated, as I'd really like to understand the usage of curly braces :)!

5
It's a matter of preference. I really don't like omitting them, others do. If you're consistent, you're probably ok.Jared Farrish
With this pretty major bug in SSL, I'd say this question is VERY pertinent. Note that ALWAYS using curly braces would have ensured this problem didn't occur. imperialviolet.org/2014/02/22/applebug.htmlAlan

5 Answers

4
votes

You can omit the {} for one single line:

if(something)
  do something else

however you cannot omit it and have it keep going like so:

if(something)
   do one thing
   do another
   do some more

The example above would only have 1 conditional element (the 'do one thing'). The rest would just run unconditionally.

And yes I've seen the sloppy method before without the {}, I myself prefer using {} to separate the logic, and its easier to read.

So stick to using {} in your code.

8
votes

Best practice is to always use them. It's far too easy for another developer to come along and add a line of code or delete a line of code and completely break the conditional unintentionally.

1
votes

It mostly depends on the style you are used to. http://en.wikipedia.org/wiki/Indent_style

if (some_error)
    something(); //Will be executed only when some_error==true
somethingelse(); //Will always be executed.

If you don't use braces only the next line is part of the if statement.

if (some_error)  err1 = "bad1"; errorno=3;

Of course, in the above example, errno will always be 3 whether or not some_error is true or not.

It is the same with for loops

for (i = 0; i < 10; i++)
    doSomething (i);

So the braces marks where it starts and where it ends. Not using them means that only the next line will be affected by the if, for, while, ... statement.

Also, braces can be used to mark block of code. When i code in C++ i always mark the start and end of a GL call with braces.

glBegin(GL_TRIANGLES); // Drawing Using Triangles
{
    glVertex3f( 0.0f, 1.0f, 0.0f); // Top
    glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
    glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
}
glEnd();

This way it is easier to read and to spot missing glEnd calls.

0
votes

Curly braces are optional if the statement to be executed after the conditional or loop is only a single line:

//This outputs "hello"
$test = 1;
if($test == 1)
    echo "hello";

//This outputs "howdy"
$test = 1;
if($test == 2)
    echo "hello";
    echo "howdy";

Whether or not you surround single-line statements with braces is a matter of personal preference and also may be dictated by a coding style document if you're working on a collaborative project. Personally I never use them for single statements and allow indentation of my code to show how the code is organized but if you see my second example above this is a case I've seen before where bugs creep in. The coder creates a conditional with a single line to be executed based on that condition. Then another coder goes in and adds a second line but forgets to add the braces. So from that perspective you could say to always use them. It depends much on your individual circumstances.

0
votes

You only have to use them if there is more than one line of code following a curly brace.

if($var) return true;

same as

if($var) {
  return true; 
}

However, if you have more than one line you must enclose them in curly braces.

if($var) {
 $var += 1;
 $var2 = $var;
 return $var2
}

Personally I stopped using them when one line of code followed because you are right, it looks cleaner especially if its all on one line like the top statement. Also too, your code will be more manageable if you cut down on the else if statements and just use return to stop the function if the if fails and just keep going to the next if on true. It can get really messy real quick with a lot of else if statements` and your not using a switch. Other than personal preference, if it's just one line of code following, lose the braces.

AlienWebguy is right when it comes to professional development, use them in that case.