Coding Style

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Coding Style

C-Style
21
51%
Java-Style
19
46%
VB-Style
1
2%
 
Total votes: 41

User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Coding Style

Post 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;
?>
Last edited by Kriek on Tue Apr 01, 2003 9:48 am, edited 6 times in total.
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post 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.
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post 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:
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post by daven »

I'm with Stoker & Twigletmac. PEAR & C style are interchangable.
User avatar
ILoveJackDaniels
Forum Commoner
Posts: 43
Joined: Mon May 20, 2002 8:18 am
Location: Brighton, UK

Post 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? :)
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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! :)
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Java style all the way!

No need for extra spaces.
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post 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.
Image Image
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 ;)
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post 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.
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post 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
User avatar
Shinmeiryu
Forum Commoner
Posts: 29
Joined: Wed Nov 13, 2002 2:57 am

Post 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.
RazorsEdge
Forum Newbie
Posts: 1
Joined: Thu Nov 13, 2003 10:48 am
Location: Plymouth, Devon, UK
Contact:

Post 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 ...
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

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