Page 1 of 2

Do you use braces?

Posted: Mon Aug 10, 2009 4:50 am
by juma929
Hi :),

Not sure if this is the place for this kind of post but nevermind! Just wondering what you guys prefer in terms of the following:

Code: Select all

 
 
if($something)
{
    echo 'random!';
} else {
    echo 'not random!';
}
 
 
Or, do you prefer to use the following:

Code: Select all

 
 
if($something):
    echo 'random!';
else:
    echo 'not random!';
endif;
 
 
This really isnt an important point, im just curious as to how my peers work. I personally go for the braces option 9 times out of 10. I can follow the blocks of code easier and in some IDE's it allows for highlighting to occur so I can quickly find the start and end of a block.

I think its just personal preference but im interested anyway :p

Thanks :)

Re: Do you use braces?

Posted: Mon Aug 10, 2009 5:05 am
by onion2k
I always use braces. They make your code more obvious. There's no way to misunderstand how things are related if there's braces; there's plenty of scope for missing things if there aren't.

Re: Do you use braces?

Posted: Mon Aug 10, 2009 6:01 am
by Eran
@onion
notice that the braces aren't missing, they are replaced by keywords


I use braces inside pure PHP code, and the alternative syntax inside HTML templates since it is easier to spot:

Code: Select all

<div class="something">
    <?php if(some_condition) : ?>
    <p>lorem ipsum
    <?php endif; ?>
</div>

Re: Do you use braces?

Posted: Mon Aug 10, 2009 6:02 am
by Weirdan
I use braces because our code standard dictates to do so.

Re: Do you use braces?

Posted: Mon Aug 10, 2009 6:32 am
by onion2k
pytrin wrote:notice that the braces aren't missing, they are replaced by keywords
Well, true I suppose, but my IDE (textpad) can't do "brace matching" on keywords. It can on braces.

Re: Do you use braces?

Posted: Mon Aug 10, 2009 6:46 am
by juma929
Hello,

I too use alternative for HTML templates etc and regular braces for 99% of my code.

Just thought it would be interesting to find out what others do and its a nice simple topic that can take you away from whatever confusion and difficulties your busy dealing with!

Re: Do you use braces?

Posted: Mon Aug 10, 2009 11:07 am
by pickle
If it's just simple like that, I tend to do this:

Code: Select all

if($random)
  echo 'something';
else
  echo 'something else';
Or even:

Code: Select all

echo ($random) ? 'something' : 'something else';

Re: Do you use braces?

Posted: Mon Aug 10, 2009 11:15 am
by jackpf
Yeah. The ternary operator is f*cking sick.


:D

Re: Do you use braces?

Posted: Sun Aug 16, 2009 2:22 pm
by Darhazer
I would write it:

Code: Select all

 
if ( $something ) {
     echo 'random!';
} else {
     echo 'not random!';
}
(Although I'm trying to avoid elses)

Our coding convension require if there is only 1 line of code, as in the case, to write:

Code: Select all

 
if($something)
     echo 'random!';
else
     echo 'not random!';
 
I'm against this, because sometimes I need to put a second line, for debugging, and I have to add the braces and then remove them, and sometimes the code just grows and someone can forget to add the braces when he is adding the second line of code.

Re: Do you use braces?

Posted: Sun Aug 16, 2009 2:27 pm
by Christopher
Darhazer wrote:I'm against this, because sometimes I need to put a second line, for debugging, and I have to add the braces and then remove them, and sometimes the code just grows and someone can forget to add the braces when he is adding the second line of code.
That is the reason why most programmer always use braces -- because one line does not always stay one line.

Re: Do you use braces?

Posted: Mon Aug 17, 2009 5:58 am
by juma929
Hello,

One of the reasons I asked was that one of my clients insists on the use of alternative syntax instead of braces so I was just wondering what most other developers do as I tend to find the enforcement of alternative a bit strange but obviously has to be done if thats what they want.

:)

Re: Do you use braces?

Posted: Mon Aug 17, 2009 7:16 am
by jackpf
:crazy:

Re: Do you use braces?

Posted: Mon Aug 17, 2009 12:18 pm
by Ollie Saunders
One of the reasons I asked was that one of my clients insists on the use of alternative syntax instead of braces
By wary of clients that dictate to their developers in this way.

I consider the closing-block-syntax, be it } or endif, a nuisance. The endif style is worse because it is longer.

Compare:

Code: Select all

foreach ($a as $b): if ($a % 2 == 0): echo $a; endif endforeach
to:

Code: Select all

foreach ($a as $b) { if ($a % 2 == 0) { echo $a; } }
As closing-block-syntax imparts no additional information than that which indentation does already, indentation is visually more obvious than braces alone, programmers agree unanimously that indentation is essential, and programmers aspire to concision, I believe that indentation is, or should be, the dominant factor that determines nesting, where possible. As such, if PHP were to suddenly become whitespace sensitive and closing-block-syntax optional, I would eagerly omit closing-block-syntax in pursuit of concision despite never having programmed in a language, such as Python, where syntax is whitespace sensitive.

As PHP programmers, we have to make that conscious effort to look at our braces because is a requirement of the language (OK, fine) but I think it's a mistake to give closing-block-syntax any more visual significance than necessary; indentation is sufficient and essential in a way that closing-block syntax is not.

In light of all, that I brace like this:

Code: Select all

if (1) { if (2) { /* sth */ } }
and when lines exceed 100 columns, I ident:

Code: Select all

if (big long condition would go here foo bar blah blah) {
    if (2) { /* sth */ } }
If you wish, you may view what a lot of code braced in that way looks like here. At first you might find it unusual, as I did, but once you learn to read by the indentation alone reading speed will increase.

Occasionally you will see:

Code: Select all

} } } }
but I think this is preferable to:

Code: Select all

            }
        }
    }
}
N.B. In a professional situation, where there is a coding standard to adhere to, I would usually complain little before conforming.

Re: Do you use braces?

Posted: Mon Aug 17, 2009 4:21 pm
by VirtuosiMedia
I always use braces, except for one liners using the ternary operator. The thing that drives me nuts, however, is an opening brace on a separate line.

Re: Do you use braces?

Posted: Mon Aug 17, 2009 5:07 pm
by Christopher
Ollie Saunders wrote:If you wish, you may view what a lot of code braced in that way looks like here. At first you might find it unusual, as I did, but once you learn to read by the indentation alone reading speed will increase.
Like anything nonstandard, it is brilliant to a the individual who uses it. If all programmers learned your indention style the it would be the standard. But the thing missing is why there is a standard. I get the sense that there is a belief that some evil overload imposed some arbitrary standard upon us, crushing our wills, and forcing us to use these unnatural braces.

That ignores the years and years that thousands upon thousands of programmers experimented with different brace styles and decided on K&R style braces. It balances the likes and dislikes of most programmers, trading off density, whitespace, etc. It is not as if every other combination of brace style was not tried. So when Ollie or some client promote some wacky style, just remember why we have the standards we have.