Page 1 of 2

coding style: curly brackets - why like this?

Posted: Wed Jun 18, 2003 7:46 am
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.

Posted: Wed Jun 18, 2003 8:00 am
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.

Posted: Wed Jun 18, 2003 8:01 am
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

Posted: Wed Jun 18, 2003 8:03 am
by patrikG
<--- is a spacious programmer then :)

Thanks!

Posted: Wed Jun 18, 2003 8:14 am
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,

Posted: Wed Jun 18, 2003 8:22 am
by patrikG
Excellent. :)

P&G-style

I am right inbetween GNU & K&R-style then :p

Posted: Wed Jun 18, 2003 8:44 am
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.

Posted: Wed Jun 18, 2003 8:49 am
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

Posted: Wed Jun 18, 2003 9:06 am
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 ;)

Posted: Wed Jun 18, 2003 11:14 pm
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.

Posted: Wed Jun 18, 2003 11:30 pm
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.

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

Posted: Thu Jun 19, 2003 2:26 pm
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.

Posted: Tue Jun 24, 2003 4:20 am
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.

Posted: Tue Jun 24, 2003 4:31 am
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

Posted: Tue Jun 24, 2003 5:33 am
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))
{
    ...;
}