B0b Posted December 31, 2009 Share Posted December 31, 2009 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! Quote Link to comment Share on other sites More sharing options...
PugJr Posted December 31, 2009 Share Posted December 31, 2009 I'm lacking to see the difference between the two methods besides that the first one has the "{" at the first line whereas the second as it on the second line? Quote Link to comment Share on other sites More sharing options...
Garethp Posted December 31, 2009 Share Posted December 31, 2009 Actually, I like mine as such if(condition) { //Code } else { //Code } There's little difference, it just seems to make code blocks easier for me to read Quote Link to comment Share on other sites More sharing options...
corbin Posted December 31, 2009 Share Posted December 31, 2009 I like method 1. if() { } Just looks disconnected to me. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted December 31, 2009 Share Posted December 31, 2009 for me method 2 is annoying, there is too much open space for my liking. I also agree with corbin. Quote Link to comment Share on other sites More sharing options...
cags Posted December 31, 2009 Share Posted December 31, 2009 I generally conform to the PEAR Standards (with regards to this issue at least). So language constructs such as if's and loops get the opening bracket on the same line and functions/classes get them on the line after. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted December 31, 2009 Share Posted December 31, 2009 I tend to use the K&R style. Edit: I'm really curious to know how PHP freaks are placing their curly brackets/braces! I'm not sure what you mean with this, but all the code that is custom written for PHP Freaks uses K&R as well. Quote Link to comment Share on other sites More sharing options...
zq29 Posted December 31, 2009 Share Posted December 31, 2009 I prefer a slight variation on the first method you posted, just with less white space. Like so... if($bob == $jack) { $joe++; // More code.. } Quote Link to comment Share on other sites More sharing options...
Adam Posted December 31, 2009 Share Posted December 31, 2009 Since starting for a company I've had to follow their coding standards (most like method 2) and it's kind of stuck with me since. Before that though I always used to follow PEAR's standards. Quote Link to comment Share on other sites More sharing options...
salathe Posted December 31, 2009 Share Posted December 31, 2009 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] Quote Link to comment Share on other sites More sharing options...
.josh Posted December 31, 2009 Share Posted December 31, 2009 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. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted December 31, 2009 Share Posted December 31, 2009 Except when you meet one of those morons who think that "zero whitespace" is the best formatting. Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted December 31, 2009 Share Posted December 31, 2009 For the most part I use the second style. Just looks better to me Quote Link to comment Share on other sites More sharing options...
premiso Posted January 1, 2010 Share Posted January 1, 2010 I love the first style, I just think it is a waste of a line to put it on a separate line. Thats me Quote Link to comment Share on other sites More sharing options...
sw45acp Posted January 1, 2010 Share Posted January 1, 2010 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 Quote Link to comment Share on other sites More sharing options...
oni-kun Posted January 1, 2010 Share Posted January 1, 2010 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* Quote Link to comment Share on other sites More sharing options...
Andy-H Posted January 1, 2010 Share Posted January 1, 2010 Personally I like the second way, just looks more readable to me. I also drop the else braces to a newline: if ( $something == $somethingEls ) { //Do some stuff... } else { //Do some other stuff... } Quote Link to comment Share on other sites More sharing options...
Garethp Posted January 1, 2010 Share Posted January 1, 2010 Am I really the only one in the forum who thinks if($somthing == $else) { //code } else { //code } Looks the neatest? Quote Link to comment Share on other sites More sharing options...
cags Posted January 1, 2010 Share Posted January 1, 2010 Quite likely so. Quote Link to comment Share on other sites More sharing options...
Alex Posted January 1, 2010 Share Posted January 1, 2010 For me it's the second method all the way. I just think it's much easier to read and nicer to look at, especially when things get complicated and involved. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted January 2, 2010 Share Posted January 2, 2010 Am I really the only one in the forum who thinks if($somthing == $else) { //code } else { //code } Looks the neatest? Yeah, probably. My preference: if ($something == $else) { // code } else { // code } Quote Link to comment Share on other sites More sharing options...
trq Posted January 2, 2010 Share Posted January 2, 2010 I use php-codesniifer to validate my code against Zend's Coding Standard, which is pretty similar to that of PEAR. Quote Link to comment Share on other sites More sharing options...
Alex Posted January 2, 2010 Share Posted January 2, 2010 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. Quote Link to comment Share on other sites More sharing options...
oni-kun Posted January 2, 2010 Share Posted January 2, 2010 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. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted January 2, 2010 Share Posted January 2, 2010 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.