Do you use braces?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
juma929
Forum Commoner
Posts: 72
Joined: Wed Jun 17, 2009 9:41 am

Do you use braces?

Post 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 :)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Do you use braces?

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Do you use braces?

Post 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>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Do you use braces?

Post by Weirdan »

I use braces because our code standard dictates to do so.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Do you use braces?

Post 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.
User avatar
juma929
Forum Commoner
Posts: 72
Joined: Wed Jun 17, 2009 9:41 am

Re: Do you use braces?

Post 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!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Do you use braces?

Post 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';
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Do you use braces?

Post by jackpf »

Yeah. The ternary operator is f*cking sick.


:D
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Do you use braces?

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Do you use braces?

Post 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.
(#10850)
User avatar
juma929
Forum Commoner
Posts: 72
Joined: Wed Jun 17, 2009 9:41 am

Re: Do you use braces?

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

:)
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Do you use braces?

Post by jackpf »

:crazy:
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Do you use braces?

Post 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.
User avatar
VirtuosiMedia
Forum Contributor
Posts: 133
Joined: Thu Jun 12, 2008 6:16 pm

Re: Do you use braces?

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Do you use braces?

Post 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.
(#10850)
Post Reply