renatov Posted February 9, 2014 Share Posted February 9, 2014 I know there is no official suggestion about how curly braces should be coded, but in your oppinion what style is a better coding practice in a professional environment? code #1 <?php function hello_world(){ echo 'Hello nurse!'; } ?> code #2 <?php function hello_world() { echo 'Hello nurse!'; } ?> Link to comment https://forums.phpfreaks.com/topic/286048-whats-the-best-way-of-coding-curly-braces-in-a-professional-environment/ Share on other sites More sharing options...
jazzman1 Posted February 9, 2014 Share Posted February 9, 2014 First example. You know what happens in javascript, right? Link to comment https://forums.phpfreaks.com/topic/286048-whats-the-best-way-of-coding-curly-braces-in-a-professional-environment/#findComment-1468237 Share on other sites More sharing options...
requinix Posted February 9, 2014 Share Posted February 9, 2014 I know there is no official suggestion about how curly braces should be coded, but in your oppinion what style is a better coding practice in a professional environment?The best style (for PHP) is "consistently". I don't care if you put them on new lines or not, just make sure you do it like that everywhere. Link to comment https://forums.phpfreaks.com/topic/286048-whats-the-best-way-of-coding-curly-braces-in-a-professional-environment/#findComment-1468239 Share on other sites More sharing options...
renatov Posted February 9, 2014 Author Share Posted February 9, 2014 First example. You know what happens in javascript, right? Sorry, I can't program in javascript yet, I'm still learning. Could you tell me what happens, please? Link to comment https://forums.phpfreaks.com/topic/286048-whats-the-best-way-of-coding-curly-braces-in-a-professional-environment/#findComment-1468249 Share on other sites More sharing options...
renatov Posted February 9, 2014 Author Share Posted February 9, 2014 The best style (for PHP) is "consistently". I don't care if you put them on new lines or not, just make sure you do it like that everywhere. Yes, that is a great advice, thanks! PHP manual itself isn't consistent about this and it makes me confuse... Link to comment https://forums.phpfreaks.com/topic/286048-whats-the-best-way-of-coding-curly-braces-in-a-professional-environment/#findComment-1468251 Share on other sites More sharing options...
ignace Posted February 9, 2014 Share Posted February 9, 2014 Pretty much everyone follows the PSR standards: http://www.php-fig.org/ So #2 would be the way to go. Link to comment https://forums.phpfreaks.com/topic/286048-whats-the-best-way-of-coding-curly-braces-in-a-professional-environment/#findComment-1468259 Share on other sites More sharing options...
jazzman1 Posted February 9, 2014 Share Posted February 9, 2014 Sorry, I can't program in javascript yet, I'm still learning. Could you tell me what happens, please? First of all, I'm agree with gurus just to jump into some good coding practice using a PSR standard or to follow a consistently style everywhere in your php application. As a web developer you should start to learn Javascript as well at some point and you will see that most often js and php are going together in your practice, for instance using Ajax or..... some web developers mix them up, which I'm considering as a very bad practice. Here is a simple example that uses javascript's return statement in right and wrong way. function square(x) { return { /* right way, good coding style in JS */ "return": x *x } } square(2) function square(x) { return { /* wrong way, the js parcer returns a silent error */ "return": x *x } } square(2) This is the first reason to prefer this coding style and the second is that my IDE (NetBeans) formats by default the code in this way and it's too late to change my habits Link to comment https://forums.phpfreaks.com/topic/286048-whats-the-best-way-of-coding-curly-braces-in-a-professional-environment/#findComment-1468293 Share on other sites More sharing options...
.josh Posted February 9, 2014 Share Posted February 9, 2014 I agree that the "standard" ("standard" meaning de facto standard that most people use) is #2. However, I personally favor #1. IMO it's easier to read. But overall, I agree that the only true "right" way is to consistently use one style or the other. If you're on a project that has code using #1, use that. If you're on a project that uses style #2, use that. Link to comment https://forums.phpfreaks.com/topic/286048-whats-the-best-way-of-coding-curly-braces-in-a-professional-environment/#findComment-1468294 Share on other sites More sharing options...
renatov Posted February 9, 2014 Author Share Posted February 9, 2014 Thanks, guys! I wasn't aware of PSR standards, I'll read it carefully. And yes, consistency is the best standard above all, I'll keep that in mind Link to comment https://forums.phpfreaks.com/topic/286048-whats-the-best-way-of-coding-curly-braces-in-a-professional-environment/#findComment-1468310 Share on other sites More sharing options...
renatov Posted February 9, 2014 Author Share Posted February 9, 2014 PSR says that PHP should use a mixed coding style with braces. The example one (no line break) should be used with control structures and the example two (line break) should be used with function, method and class definitions. PSR offers this example: <?php namespace Vendor\Package; use FooInterface; use BarClass as Bar; use OtherVendor\OtherPackage\BazClass; class Foo extends Bar implements FooInterface { public function sampleFunction($a, $b = null) { if ($a === $b) { bar(); } elseif ($a > $b) { $foo->bar($arg1); } else { BazClass::bar($arg2, $arg3); } } final public static function bar() { // method body } } Link to comment https://forums.phpfreaks.com/topic/286048-whats-the-best-way-of-coding-curly-braces-in-a-professional-environment/#findComment-1468317 Share on other sites More sharing options...
jazzman1 Posted February 9, 2014 Share Posted February 9, 2014 Make sure you have a copy of some good php IDE. Link to comment https://forums.phpfreaks.com/topic/286048-whats-the-best-way-of-coding-curly-braces-in-a-professional-environment/#findComment-1468327 Share on other sites More sharing options...
sKunKbad Posted February 12, 2014 Share Posted February 12, 2014 I prefer Allman style (#2) for position of comments, and the fact that if you've got longer lines of code you can't see your opening bracket when an indent style is used that leaves opening brackets on the same line. So, I say pick an indent style and stick with it, as long as it is Allman style! Link to comment https://forums.phpfreaks.com/topic/286048-whats-the-best-way-of-coding-curly-braces-in-a-professional-environment/#findComment-1468622 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.