NotionCommotion Posted March 23, 2017 Share Posted March 23, 2017 I am not asking whether one approach is better than the other, or what you do for more obscure cases such as closure. Only for defining normal methods (and I assume normal functions as well). Assuming you were starting the code off from scratch, what would you do? Thank you public function myFunction() { // } public function myFunction() { // } Quote Link to comment https://forums.phpfreaks.com/topic/303522-where-do-you-put-opening-curly-braces-when-defining-a-method/ Share on other sites More sharing options...
ginerjm Posted March 23, 2017 Share Posted March 23, 2017 Whenever I set up a pair of braces it is as you did in sample 2. Function headers, ifs, while, foreach, anything using {} will always go to the next line to begin. I also always indent one tab stop once the braces are setup. So my statement is at column one say and my braces will also be in column one, but starting on the next line. Then the statements inside the braces will be in the next tab stop. I find that this makes it much easier to recognize the code and to follow it when I'm breezing thru it looking for something. It also helps to see the flow since everything is not all in column one. This works well if your IDE automatically recognizes where the previous line is indented to and matches it when you hit the enter key for the next line of code. If it doesn't this style may be more of a pia than a help. Quote Link to comment https://forums.phpfreaks.com/topic/303522-where-do-you-put-opening-curly-braces-when-defining-a-method/#findComment-1544489 Share on other sites More sharing options...
requinix Posted March 23, 2017 Share Posted March 23, 2017 I think it's completely and utterly irrelevant. Quote Link to comment https://forums.phpfreaks.com/topic/303522-where-do-you-put-opening-curly-braces-when-defining-a-method/#findComment-1544490 Share on other sites More sharing options...
ginerjm Posted March 23, 2017 Share Posted March 23, 2017 Compared to the rest of my life, this style is about the tidiest thing I have ever done. 1 Quote Link to comment https://forums.phpfreaks.com/topic/303522-where-do-you-put-opening-curly-braces-when-defining-a-method/#findComment-1544491 Share on other sites More sharing options...
NotionCommotion Posted March 23, 2017 Author Share Posted March 23, 2017 I think it's completely and utterly irrelevant. I believe it is relevant to me. I've gone back and forth on what approach is "best", and it has been a futile endeavor. That is why I specifically did not ask what approach is best. I do believe, however, it is important to be consistent, and to my determinant, I have not been in this regard. I've even changed code because on that given day, I felt my style should be one way or the other. I've witnessed very good programmers on this forum using one style or the other, but unlike me, they consistently use the same style. My desire is to identify the style of the majority of programmers, and adapt that style as my own. Quote Link to comment https://forums.phpfreaks.com/topic/303522-where-do-you-put-opening-curly-braces-when-defining-a-method/#findComment-1544493 Share on other sites More sharing options...
requinix Posted March 23, 2017 Share Posted March 23, 2017 Consistency is good, sure. But you're spending time on a matter which is not even a matter. The name you give to a temporary variable that lasts less than a dozen lines of code is more important than this. Don't know which to choose? Flip a coin. Programming is not about copying what other people are doing. ...what coin you flip is probably more important than this... Quote Link to comment https://forums.phpfreaks.com/topic/303522-where-do-you-put-opening-curly-braces-when-defining-a-method/#findComment-1544494 Share on other sites More sharing options...
requinix Posted March 23, 2017 Share Posted March 23, 2017 Actually, forget the coin. Find a code style guide from a project you like and adopt that. Quote Link to comment https://forums.phpfreaks.com/topic/303522-where-do-you-put-opening-curly-braces-when-defining-a-method/#findComment-1544495 Share on other sites More sharing options...
NotionCommotion Posted March 23, 2017 Author Share Posted March 23, 2017 Actually, forget the coin. Find a code style guide from a project you like and adopt that. So, not going to get your answer? Quote Link to comment https://forums.phpfreaks.com/topic/303522-where-do-you-put-opening-curly-braces-when-defining-a-method/#findComment-1544496 Share on other sites More sharing options...
benanamen Posted March 23, 2017 Share Posted March 23, 2017 There are actually several common styles that have names. I personally prefer Whitesmith. Pick one and stick with it. http://www.terminally-incoherent.com/blog/2009/04/10/the-only-correct-indent-style/ 1 Quote Link to comment https://forums.phpfreaks.com/topic/303522-where-do-you-put-opening-curly-braces-when-defining-a-method/#findComment-1544497 Share on other sites More sharing options...
NigelRel3 Posted March 23, 2017 Share Posted March 23, 2017 Same here - find a style and just stick to it. IMHO there are issues with all of them, but the main thing is consistency makes it easier to read code. Personally I've always done function someName() { } Which is consistent with other things like if ( someDodgyComparison == true ) { while ( thereIsMoreToDo == true ) { } } Quote Link to comment https://forums.phpfreaks.com/topic/303522-where-do-you-put-opening-curly-braces-when-defining-a-method/#findComment-1544500 Share on other sites More sharing options...
ginerjm Posted March 23, 2017 Share Posted March 23, 2017 NigelRel3: It is only consistent with your other examples if THAT is the style you choose to use. Quote Link to comment https://forums.phpfreaks.com/topic/303522-where-do-you-put-opening-curly-braces-when-defining-a-method/#findComment-1544507 Share on other sites More sharing options...
ginerjm Posted March 23, 2017 Share Posted March 23, 2017 PS - I love the link Benanaman provided. Made my vote and found that I was part of the majority!! Allman all the way! Quote Link to comment https://forums.phpfreaks.com/topic/303522-where-do-you-put-opening-curly-braces-when-defining-a-method/#findComment-1544508 Share on other sites More sharing options...
NigelRel3 Posted March 23, 2017 Share Posted March 23, 2017 As I started writing C - not a surprise that I use K&R. The reason I was talking about consistency with brace placing is that the PHP-FIG PSR-2 style (http://www.php-fig.org/psr/psr-2/) uses what I see as inconsistent brace placement... public function sampleMethod($a, $b = null) { if ($a === $b) { bar(); } elseif ($a > $b) { $foo->bar($arg1); } else { BazClass::bar($arg2, $arg3); } } Quote Link to comment https://forums.phpfreaks.com/topic/303522-where-do-you-put-opening-curly-braces-when-defining-a-method/#findComment-1544509 Share on other sites More sharing options...
Psycho Posted March 23, 2017 Share Posted March 23, 2017 Someone asked a similar question a long time back and someone pointed them toward the PSR coding standards. So, that's another option: http://www.php-fig.org/psr/psr-2/ Quote Link to comment https://forums.phpfreaks.com/topic/303522-where-do-you-put-opening-curly-braces-when-defining-a-method/#findComment-1544513 Share on other sites More sharing options...
zamight Posted March 24, 2017 Share Posted March 24, 2017 I use PSR-2. So I would break a new line for a function curly bracket. Quote Link to comment https://forums.phpfreaks.com/topic/303522-where-do-you-put-opening-curly-braces-when-defining-a-method/#findComment-1544545 Share on other sites More sharing options...
bbmak Posted March 24, 2017 Share Posted March 24, 2017 I think it is personal perference... I used #2, and I like the { } vertical allign Quote Link to comment https://forums.phpfreaks.com/topic/303522-where-do-you-put-opening-curly-braces-when-defining-a-method/#findComment-1544547 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.