3raser Posted March 28, 2012 Share Posted March 28, 2012 I'm curious how people prefer to organize their code. What do you prefer to do? <?php function blah() { } ?> or function blah(){ } Quote Link to comment Share on other sites More sharing options...
3raser Posted March 28, 2012 Author Share Posted March 28, 2012 EDIT: This may be more suitable for Application Design Quote Link to comment Share on other sites More sharing options...
kicken Posted March 28, 2012 Share Posted March 28, 2012 It pretty much all comes down to personal style and what you find to be the most readable. I personally prefer the second way, with the brace on the same line as the function (or loop or condition, etc). Lots of people prefer the first way. If it's just your own project and code then use whatever method you like and find easiest to use. If your developing code for an open source project or employer you should check if they have any established code style guidelines/rules. Some places will have a document that dictates things like this (as well as spaces vs tab, indent length, argument spacing, etc). Quote Link to comment Share on other sites More sharing options...
Philip Posted March 28, 2012 Share Posted March 28, 2012 This is pretty much what I do... braces on the next line unless it is a 1-line if statement. <?php function foo($bar = false) { if($blah = 5) { switch($do) { case 1: bar(); break; case 2: default: bar2(); break; } } if($blah = 1) { $oneLineIfStatement = true; } foreach($x as $key => $value) { // do something } return false; } class fooBar extends bar { public $doodad; public static function something( ) { return false; } } Quote Link to comment Share on other sites More sharing options...
thehippy Posted March 28, 2012 Share Posted March 28, 2012 Most of my code tends to follow the Zend coding standard, which is to say the PEAR coding standard with a few minor differences. I even have a pre-commit check with phpcs to ensure I'm not being lazy. Quote Link to comment Share on other sites More sharing options...
scootstah Posted March 28, 2012 Share Posted March 28, 2012 I put all my brackets on the next line except for if/else/else-if. Quote Link to comment Share on other sites More sharing options...
titan21 Posted March 28, 2012 Share Posted March 28, 2012 I prefer the second way. I always find it a little easier to match up the braces than the first way. (Not PHP I know, but jQuery has really upset me with the way it seems to do things!!) Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 28, 2012 Share Posted March 28, 2012 I also prefer the second way, and I find it easier to match. I think it just depends on what you first saw when learning. Quote Link to comment Share on other sites More sharing options...
Philip Posted March 28, 2012 Share Posted March 28, 2012 I think it just depends on what you first saw when learning. For me, I started off with the braces on the same line and evolved to the second because of standards with projects I worked with. However I do find it cleaner now. PS - Long time no see Quote Link to comment Share on other sites More sharing options...
scootstah Posted March 28, 2012 Share Posted March 28, 2012 I think it just depends on what you first saw when learning. For me, I started off with the braces on the same line and evolved to the second because of standards with projects I worked with. However I do find it cleaner now. Same here. Although I still don't like my conditional brackets being on the next line. I don't know why, it just doesn't look good to me. Quote Link to comment Share on other sites More sharing options...
titan21 Posted March 28, 2012 Share Posted March 28, 2012 For me, I started off with the braces on the same line and evolved to the second because of standards with projects I worked with. However I do find it cleaner now. Thank God! I thought that most people went with the same line brace! Glad to know that some projects do use the second style. Which projects were they if I may ask? Quote Link to comment Share on other sites More sharing options...
Philip Posted March 28, 2012 Share Posted March 28, 2012 Thank God! I thought that most people went with the same line brace! Glad to know that some projects do use the second style. Which projects were they if I may ask? A few for work (which I am not allowed to disclose) and Flingbits, a similar site to this that a lot of people from here helped out on... but sadly the drive behind it has died. Quote Link to comment Share on other sites More sharing options...
Stooney Posted March 28, 2012 Share Posted March 28, 2012 <?php class someClass{ private $var; public function foo(){ //Stuff switch($var){ case 1: //stuff break; } if($var){ //Stuff } else{ //Stuff } } } Quote Link to comment Share on other sites More sharing options...
Philip Posted March 28, 2012 Share Posted March 28, 2012 I can't stand the break being indented in a case like that. It makes it so much harder to see when the case ends. It's actually one code standard I will break. <?php switch($foo) { case 'blah': $do->this(); break; case 'something': $do->that(); // and this.. no break! case 'else': $do->both(); break; default: $do->die(); break; } vs. <?php switch($foo) { case 'blah': $do->this(); break; case 'something': $do->that(); // and this.. no break! case 'else': $do->both(); break; default: $do->die(); break; } Personal opinion, but I think the second is so much easier to read with where the cases start/end. Quote Link to comment Share on other sites More sharing options...
scootstah Posted March 28, 2012 Share Posted March 28, 2012 Yup, I don't understand why the break should be indented. It is basically the same as brackets... everything inside the case is of course indented but the break should not be. Quote Link to comment Share on other sites More sharing options...
3raser Posted March 28, 2012 Author Share Posted March 28, 2012 I can't stand the break being indented in a case like that. It makes it so much harder to see when the case ends. It's actually one code standard I will break. <?php switch($foo) { case 'blah': $do->this(); break; case 'something': $do->that(); // and this.. no break! case 'else': $do->both(); break; default: $do->die(); break; } vs. <?php switch($foo) { case 'blah': $do->this(); break; case 'something': $do->that(); // and this.. no break! case 'else': $do->both(); break; default: $do->die(); break; } Personal opinion, but I think the second is so much easier to read with where the cases start/end. Weird. I find the first one MUCH easier to read and see where each case breaks. Quote Link to comment Share on other sites More sharing options...
Philip Posted March 28, 2012 Share Posted March 28, 2012 So you recognize that case 'something' doesn't end until the default case starts? Quote Link to comment Share on other sites More sharing options...
scootstah Posted March 28, 2012 Share Posted March 28, 2012 Weird. I find the first one MUCH easier to read and see where each case breaks. That doesn't make sense. You have to specifically look for the break. If you only indent the code in the case but not the break, you know that as soon as the indentation stops there is a break. It is a lot faster to just look at a column of indented text than to find a specific keyword. Quote Link to comment Share on other sites More sharing options...
Proletarian Posted March 29, 2012 Share Posted March 29, 2012 I prefer code that is commented. That way, whatever style used, I know how to understand what is written. Side note, I never thought to use break statements as "closing brackets" to the case "open bracket". I'll be trying it out to see whether it improves my ability to organize my switch statements. Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 29, 2012 Share Posted March 29, 2012 Weird. I find the first one MUCH easier to read and see where each case breaks. That doesn't make sense. You have to specifically look for the break. If you only indent the code in the case but not the break, you know that as soon as the indentation stops there is a break. No you don't, it could be the next case. You are looking for two non-indented lines. It's kind of silly to tell someone they don't know what's easier for their own self to read. Studies show all-caps is hardest to read but some people insist it's easier for them to read, are you going to say they are wrong and don't know their own comfort? 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.