Jump to content

What's the best way of coding curly braces in a professional environment?


renatov

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.