Jump to content

Curly brackets/braces: how you like them?


How are you placing your curly brackets/braces?  

29 members have voted

  1. 1. How are you placing your curly brackets/braces?

    • Method 1 (first bracket at first line)
      17
    • Method 2 (first bracket at second line)
      9
    • Other? Tell us!
      3


Recommended Posts

Hey!

I'm really curious to know how PHP freaks are placing their curly brackets/braces! Or if there's some sort of convention about this?

 

Method one:

if ( $bob == $jack ) {
    $joe++;
    // More code..
}

 

Method two:

if ( $bob == $jack )
{
    $joe++;
    // More code..
}

 

Or something else? Vote!

Link to comment
https://forums.phpfreaks.com/topic/186770-curly-bracketsbraces-how-you-like-them/
Share on other sites

Depending on who the code is written for, I use different conventions.  For example, in the PHP manual we must adhere to PEAR's coding standard (which uses K&R style braces); other work/projects require the use of Allman style braces/indentation or sometimes a K&R variant (aka. One True Brace). As for which I like best, probably one of those mentioned above (there are lots of other bracing/indenting/layout styles around). 

 

[ot]@PugJr that's entirely the point.[/ot]

Depending on who the code is written for, I use different conventions.  For example, in the PHP manual we must adhere to PEAR's coding standard (which uses K&R style braces); other work/projects require the use of Allman style braces/indentation or sometimes a K&R variant (aka. One True Brace). As for which I like best, probably one of those mentioned above (there are lots of other bracing/indenting/layout styles around). 

 

That's pretty much how it is with me.  I personally code the way SA does, but depending on who I'm coding for, I try to adhere to their standards.  You'd be surprised how many people out there actually believe that it somehow makes a difference in a "best practice" or "this somehow makes the code run (better)" way, and it's not worth the time or effort arguing with them about it.

i do mine like this to save time

 

if (2==2){
    //code
}else{
    //code
}

 

but if i were ever going to publish code, i would probably do it the second way

 

Yeah, I just prefer it this way, It is just easier to see the beginning of the bracket (especially in IDE) and just looks cleaner, on second line looks like one of those 'looks bad, but must do it' kinda things to me..

 

Atleast I don't do

function foobar($foo, $bar, $baz){return $foo+$bar+$baz; DoSomething(); echo "done"; }

.. *hides my sig*

Am I really the only one in the forum who thinks

 

if($somthing == $else)
{
//code
}
else
{
//code
}

 

Looks the neatest?

 

Yeah, probably.

 

My preference:  :sarcastic:

if
    ($something
==
$else)
         { // code
    } else
{
    // code
                     }

I'd like to follow the Zend standard, but I'm really stubborn about a few things, in particular, this. I wouldn't have a problem changing the way I do some things to adhere to the standard, like the space before the parenthesis after if. But I figure there's no point in following only some of the standard.

 

I guess the reason I'm so stubborn about this is because when I program in lower level programming languages like C++ I find this use of (the second method) bracket formatting especially helpful in terms of readability. I guess there's 2 main reasons: 1) I find that it just keeps things less crowded, easier to read in general. 2) It makes matching opening an closing brackets much easier. I find it's easier to visually match the opening and closing brackets when they're both at the same horizontal position.

 

I've seen, and worked on a few projects for some larger companies that actually required small things like this. For example they might have something like (Just an example in PHP, because the projects that I'm actually referencing happened to be in C++):

 

if ( $variable == $anothervar )
{
     $this->something ( $variable );
}

 

I just don't happen to see how those spaces really improve readability, but that's just my opinion.

 

 

http://framework.zend.com/manual/en/coding-standard.coding-style.html#coding-standard.coding-style.control-statements.if-else-elseif

 

That should support my like of the brackets being on top:

The opening brace is written on the same line as the conditional statement. The closing brace is always written on its own line. Any content within the braces must be indented using four spaces

 

if ($a != 2) {

    $a = 2;

}

 

If Zend coding standards are like PEAR's, than I say you should just stick to that.

I just don't happen to see how those spaces really improve readability, but that's just my opinion.

 

Whether you choose Allman, K&R or something different isn't so important, but consistency is. Pick one and stick with it throughout your entire project.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.