Jump to content

Where do you put opening curly braces when defining a method?


NotionCommotion

Recommended Posts

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()
{
   //
}
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 ) {
    }
}

Link to comment
Share on other sites

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);
        }
    }
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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