curly braces/no curly braces?

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 :)!


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.


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.


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.

链接地址: http://www.djcxy.com/p/1866.html

上一篇: PHP中字符串中的大括号

下一篇: 大括号/没有大括号?