Page 1 of 2

Coding Style

Posted: Tue Apr 01, 2003 9:24 am
by Kriek
I've been meaning to poll you guys concerning coding style for awhile, but recently I've found yet another style to choose from so I feel now is the time. This mainly deals with control structures.

C-Style

Code: Select all

<?php
    if ($a > $b)
    {
        print "a is bigger than b";
    }
    elseif ($a == $b)
    {
        print "a is equal to b";
    }
    else
    {
        print "a is smaller than b";
    }
?>
Java-Style (PEAR Coding Standard)

Code: Select all

<?php
    if ($a > $b) {
        print "a is bigger than b";
    }
    elseif ($a == $b) {
        print "a is equal to b";
    } else {
        print "a is smaller than b";
    }
?>
VB-Style (PHP Alternative syntax)

Code: Select all

<?php
    if ($a > $b): print "a is bigger than b";
    elseif ($a == $b): print "a is equal to b";
    else: print "a is smaller than b";
    endif;
?>

Posted: Tue Apr 01, 2003 9:30 am
by Jim
I'm all about C-style. I guess that's a good thing, since I'm going to college to learn C, C++ and Java and any other language I can pick up along the way.

Posted: Tue Apr 01, 2003 9:42 am
by Stoker
C vs PEAR style is pretty much the same to me, whichever looks better in the chunk of code I'm working on..

The VB-like syntax (Alternative) shouldn't be allowed... :roll:

Posted: Tue Apr 01, 2003 9:48 am
by twigletmac
I agree with Stoker - I really dislike the alternative syntax. It's much easier to read and see the logic flow in code using braces rather than colons and end this that and the others.

Mac

Posted: Tue Apr 01, 2003 10:06 am
by daven
I'm with Stoker & Twigletmac. PEAR & C style are interchangable.

Posted: Tue Apr 01, 2003 11:26 am
by ILoveJackDaniels
Ummmm, I do it like this:

Code: Select all

<?php 
    if ($a > $b) &#123; 
        print "a is bigger than b"; 
    &#125; elseif ($a == $b) &#123; 
        print "a is equal to b"; 
    &#125; else &#123; 
        print "a is smaller than b"; 
    &#125; 
?>
So I guess that makes me a touch strange? :)

Posted: Tue Apr 01, 2003 6:00 pm
by m3mn0n
lol, nah your not strange, my newbie book taught me it that way too and it's impossible to kick the habbit once you've done it a few thousand times.

I hate the vb syntax too, as twigletmac explained it's much harder read and see logic flow compared to parenthesis/quotes. But then again, that's what comments are for! :)

Posted: Tue Apr 01, 2003 6:38 pm
by hob_goblin
Java style all the way!

No need for extra spaces.

Posted: Tue Apr 01, 2003 7:20 pm
by phice
ILoveJackDaniels wrote:

Code: Select all

<?php 
    if ($a > $b) &#123; 
        print "a is bigger than b"; 
    &#125; elseif ($a == $b) &#123; 
        print "a is equal to b"; 
    &#125; else &#123; 
        print "a is smaller than b"; 
    &#125; 
?>
That's my style of programming. But, since I've been learning C in my class, I'm slowly going torwards that type of programming. But, then again, the VB style is rather sexy.

Posted: Tue Apr 01, 2003 7:43 pm
by volka
I prefer matching brackets at the same column. Mainly because I do not use them at all for only one statement

Code: Select all

if ($cond)
	doSomething();
and therefor sometimes heavily rely on the matching-brackets-highlighter ;)

Posted: Wed Apr 02, 2003 4:01 pm
by Kriek
Just for the record, there are several styles and probably more than I can find information about, but I just listed the two most popular being C-Style and Java-Style along with the disputed VB-Style that has stirred up so much commotion.

Allman-Style Named for Eric Allman, a Berkeley hacker who wrote a lot of the BSD utilities in it (it is sometimes called `BSD style'). Resembles normal indent style in Pascal and Algol. It is the only style other than K&R in widespread use among Java programmers. Basic indent per level shown here is eight spaces, but four (or sometimes three) spaces are generally preferred by C++ and Java programmers.

K&R Style Named after Kernighan & Ritchie, because the examples in K&R are formatted this way. Also called kernel style because the Unix kernel is written in it, and the One True Brace Style (abbrev. 1TBS) by its partisans. In C code, the body is typically indented by eight spaces (or one tab) per level, as shown here. Four spaces are occasionally seen in C, but in C++ and Java four tends to be the rule rather than the exception.

Whitesmiths Style popularized by the examples that came with Whitesmiths C, an early commercial C compiler. Basic indent per level shown here is eight spaces, but four spaces are occasionally seen.

GNU style Used throughout GNU EMACS and the Free Software Foundation code, and just about nowhere else. Indents are always four spaces per level, with { and } halfway between the outer and inner indent levels.

Posted: Wed Apr 02, 2003 4:17 pm
by pootergeist
I use braces and step to one tab (four spaces in a decent [non-notepad] editor) after the definition/condition

Code: Select all

<?php
if( $a == $b )
	{
	// do this
	echo 'a equals b';
	}
else
	{
	// do that
	echo 'a does''t equal b';
	}
?>
as I find it fastest to read grouped clauses

though for the above I'd use a ternary operant

Posted: Thu Apr 03, 2003 3:46 pm
by Shinmeiryu
I personally prefer C style the most when I'm working with nested ifs. I can easily adapt to the PEAR standard. However, that VB standard...no thanks.

Posted: Thu Nov 13, 2003 10:48 am
by RazorsEdge
[quote="pootergeist"]I use braces and step to one tab (four spaces in a decent [non-notepad] editor) after the definition/condition

[syntax=php]<?php
if( $a == $b )
{
// do this
echo 'a equals b';
}
else
{
// do that
echo 'a does\'t equal b';
}
?>[/syntax]

as I find it fastest to read grouped clauses

though for the above I'd use a ternary operant[/quote]

This is the style I use.

The braces are a LOT more visible than at the end of lines and always column match.

Add to this a code collapse function ...

Posted: Thu Nov 13, 2003 11:14 am
by BDKR
pootergeist wrote:I use braces and step to one tab (four spaces in a decent [non-notepad] editor) after the definition/condition

Code: Select all

<?php
if( $a == $b )
	{
	// do this
	echo 'a equals b';
	}
else
	{
	// do that
	echo 'a does''t equal b';
	}
?>
as I find it fastest to read grouped clauses

though for the above I'd use a ternary operant
I'm with the pootergeist on this one.

It also seems that there are two seperate schools on this. The curl brace languages ({) and those without. Those without include VB of course, but also Python and Ruby. There are others, but Python and Ruby are garnering tons of respect out there. Python even goes so far as require indentation (for lack of a better way of putting it).

Pascal is another language without braces if I'm not mistaken.

Ultimately, it prolly has most to do with the language you learned first. I first learned C (eventhough I am still less than proficient with it) and as a result have a bias, maybe even an affinity, for the curly brace.

Cheers,
BDKR