coding style: curly brackets - why like this?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

coding style: curly brackets - why like this?

Post by patrikG »

Just had another look at the control-structure bit in the PEAR-manual and stumbled across this:

Code: Select all

<?php
if ((condition1) || (condition2)) {
    action1;
     blabla;
} elseif ((condition3) && (condition4)) {
    action2;
    blablabla;
} else {
    defaultaction;
    blablablabla;
}
?>
I've often seen other people code curly brackets like this - but why is this recommended?

I would do the code above like this:

Code: Select all

<?php
if ((condition1) || (condition2)) 
    {
    action1;
    blabla;
    } 
elseif ((condition3) && (condition4)) 
    {
    action2;
    blablabla;
    }
else
    {
    defaultaction;
    blablablabla;
    }
?>
I find it much easier to read - and can't find any advantage in the PEAR-recommended style apart from being more space-efficient (which is redundant). But when it comes to debugging and you forgot some curly bracket somewhere, I find the PEAR-recommended style a pain.

Could anyone enlighten me, please?

P.S.: and yes, the code is just an example, I know PHP's switch etc. - it's just there make my point clear.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Its the classic K&R style from C. Some people think the blanks lines with only a brace is ugly, most people agree that an entire function should fit on the screen at the time, so the K&R style allows more per screen :)

I like the more whitespace friendly version, but its not a big deal to adapt to any paticular style guide.
Last edited by nielsene on Wed Jun 18, 2003 8:03 am, edited 1 time in total.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

It really comes down to what you like. Personally I prefer the PEAR way of doing things (although I use tabs not spaces) and find the second way has me scrolling continuously to find anything.

Readability is very much in the eye of the beholder - what one person finds perfectly clear is code soup to someone else. The really important thing is keeping it consistent, things like this really irritate me:

Code: Select all

if ($something == 'something') {
    // some code
}
elseif ($foo == 'bar')
    {
     // some code
} else {
    // all done
}
Mac
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

<--- is a spacious programmer then :)

Thanks!
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post by cactus »

K & R:

http://info.astrian.net/jargon/terms/k/K_R.html
http://info.astrian.net/jargon/terms/i/ ... style.html

To Add:

BTW, I'm a K&R man, can't help it, makes the code easier to read (IMO) :)

Regards,
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Excellent. :)

P&G-style

I am right inbetween GNU & K&R-style then :p
User avatar
releasedj
Forum Contributor
Posts: 105
Joined: Tue Jun 17, 2003 6:35 am

Post by releasedj »

I prefer

Code: Select all

<?php
if ((condition1) || (condition2))
{
    action1;
    blabla;
}
elseif ((condition3) && (condition4))
{
    action2;
    blablabla;
}
else
{
    defaultaction;
    blablablabla;
}
?>
but then who cares. I think it's only relative to a particular application or project.. then I think it just needs to be consistent.
User avatar
discobean
Forum Commoner
Posts: 49
Joined: Sun May 18, 2003 9:06 pm
Location: Sydney, Australia
Contact:

Post by discobean »

I'm an Allman style...

I used to write the K&R Style when writing in C, then moved to Java and started using Allman, just seemed much cleaner to me.

Edit: And I'm a 3 space man for tabs.. although, still using tabs, just changing spacing in VIM
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post by pootergeist »

Whitesmith's style here (same as PatrikG) and generally 4 spaces to the tab. I find it easier to read for extensive nested code blocks

Code: Select all

if($condition)
	{
	if($condition2)
		{
		// condition plus condition2
		}
	else
		{
		// condition without condition2
		}
	}
with all the clauses on the same vertical line as the braces it is a doddle to walk through. well imo anyway ;)
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

Just for the fun of it, I'm an Allman style coder myself.
And I indent five spaces.
But I agree with those who say that consistency is more important than which style you use.
Moonspell
Forum Newbie
Posts: 21
Joined: Tue May 13, 2003 4:54 pm

Post by Moonspell »

I tend to prefer

Code: Select all

<?php
if ((condition1) || (condition2))
{
    action1;
    blabla;
}
elseif ((condition3) && (condition4))
{
    action2;
    blablabla;
}
else
{
    defaultaction;
    blablablabla;
}
?>
A lot of my errors come from loops inside of loops. For some reason I have a hard time ending complex looping sequences correctly. I usually leave off a bracket somewhere. Using it this style makes it a lot easier to see at a glance, especially if you use horizontal spacing well.

However, I do find myself scrolling too much inside of loops or functions.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Re: coding style: curly brackets - why like this?

Post by McGruff »

patrikG wrote:

Code: Select all

<?php
if ((condition1) || (condition2)) {
    action1;
     blabla;
} elseif ((condition3) && (condition4)) {
    action2;
    blablabla;
} else {
    defaultaction;
    blablablabla;
}
?>
This I find too hard to read but..

Code: Select all

<?php
if ((condition1) || (condition2)) {

    action1;
     blabla;

} elseif ((condition3) && (condition4)) {

    action2;
    blablabla;

} else {

    defaultaction;
    blablablabla;
}
?>
.. let's the code breathe.
Last edited by McGruff on Thu Aug 11, 2005 5:10 am, edited 1 time in total.
lcidw
Forum Commoner
Posts: 58
Joined: Mon Apr 28, 2003 8:55 am
Location: Netherlands

Post by lcidw »

To continue people's preferences, i'm a K&R style coder.

Nice thing of it is under every if(...) or while(...) is a closing bracket and i don't need to look for an open bracket. It's like a hand which carries a package.

Code: Select all

if($you_like_KnR) {
    echo("Me too!");
}
Only thing i'm fscking with, is about spaces, the next becomes very unreable.. does anyone have interesting recommendations about that?

Code: Select all

if(((empty(substr($_POST['bold'],4,2))) && (empty($_GET['italian']))) || (isset(addslashes($error)))) { .. }
Oh, and then there's the thing like PEAR recommending to leave a space between `if` and `()`, a function seperated by space from it's input, though in an own function defenition you should `function()` without space.. Weird.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

if is a control structure - not a function - so that's why it gets a space between it and it's conditions.

I agree that lots of if conditions with ands and ors can become very difficult to read - if I find one's getting too complicated I'll break it down into more if...else statements to give me more control and make it easier to maintain.

Mac
User avatar
releasedj
Forum Contributor
Posts: 105
Joined: Tue Jun 17, 2003 6:35 am

Post by releasedj »

I like the way PEAR defines how spaces should be used.

With regard to spaces in an if clause I would suggest the following:
  1. If an argument to a function is itself a function, leave a spaces at either side.
  2. If you have multiple clauses grouped together using brackets, leave a space after the first bracket and before the last
  3. Avoid having more than 2 conditions on the same line, unless the conditions are very simple.
  4. New lines should be indented slightly underneath the top line and should start with the clause operator
i.e.:

Code: Select all

if( ( empty( substr($_POST['bold'],4,2) ) && empty($_GET['italian']) )
        || isset(addslashes($error))
{
    ...;
}
Post Reply