Brace Yourself

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

Stoneguard
Forum Contributor
Posts: 101
Joined: Wed Aug 13, 2003 9:02 pm
Location: USA

Brace Yourself

Post by Stoneguard »

Ok, I would like to know how people use braces and why they use that method. I know the 'accepted' way is as follows:

Code: Select all

if ($this != $that) {
   do_run ($theother);
   step_thru ($amess);
}
But I think that really does not make for smooth reading or easy comprehension. I use aligned braces:

Code: Select all

if ($this != $that) 
{
   do_run ($theother);
   step_thru ($amess);
}
And the reason I used aligned braces is many-fold, but primarily so I can a) quickly see that a block of code is being called instead of a single function and b) I can easily match up nested braces on the left instead of having a variable column set on the right to begin.

I have made aligned my standard during over 16 years of coding and doing it multiple ways, because what I see are the positives out-weighing the negatives. Now, I would like someone to explain to me why they use a different way.

I am just curious as to why people use the method they use, and not looking for a 'right' method, as there obviously is not one - just look at all the code out there and the different styles.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

I like the way you do it also. I keep most everything in a heirarchy and that includes the brace that starts off the function/if statement.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Interesting topic. I'm wondering also, as I currently seem to adapt to others.

I'm writing with alot of people using the former method, so today I write like that. A year ago I was apart of a group that wrote as the later, so at that time so did I.

Why I still keep to the old first example-type, I can't say. Today that feels natural. Tomorrow, it just might not.
I'm having the same thoughts about people using the heredoc style coding (that I get chestpains from personally), double quotes or single quotes...

But, i can't brace myself not to mention people that use the following way of coding:

Code: Select all

<?php
$i=1;$o="some long text string that just fills out the page...";$somevar=function($cow);
if($somevar==$moo){bla();}
// or worse...
?>
I don't read phpdn-users problems if I see this type of coding. It's an insult not to use indents or newlines just to save a code-height...
User avatar
cybaf
Forum Commoner
Posts: 89
Joined: Tue Oct 01, 2002 5:28 am
Location: Gothenburg Sweden

Post by cybaf »

I always use the first method... reason? well it's because I've had too many problems due to a lacking ";"... so if I see code the way you (and many others) do it I see the lines without an ending ";"... :)... so I think that all lines you end with either a ";" or a "{"

//cybaf
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

there already have been several threads on the same topic
e.g.
viewtopic.php?t=9864
viewtopic.php?t=7671
viewtopic.php?t=3173
Stoneguard
Forum Contributor
Posts: 101
Joined: Wed Aug 13, 2003 9:02 pm
Location: USA

Post by Stoneguard »

Opps, sorry. I tend to only scan the front page for subjects.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

don't worry, there's always someone who remembers ;)
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post by Kriek »

Java-Style (PEAR Coding Standard)

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;
?>
See -> Coding Style
Stoneguard
Forum Contributor
Posts: 101
Joined: Wed Aug 13, 2003 9:02 pm
Location: USA

Post by Stoneguard »

Yes, but is PEAR the only reason you code that way? Would you code another way if not for PEAR?
murph
Forum Commoner
Posts: 29
Joined: Fri Oct 03, 2003 1:28 pm
Location: washington

Post by murph »

I like to have everything looking nice and purdy! so i code like this

Code: Select all

for ( $i = 0; $i < $count; $i++ )
{
    $something;
    $another;
}
also i like to use spaces in my code like this function blah ( $what, $who )
It makes for way easier debuging and it looks alot nicer
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

I don't know, maybe it's just my preference thing again. I never use indents. I hate unneccessary tabs or spaces in my scripts. Surprisingly, I don't find it hard to read at all - really!

So I'm something like:

Code: Select all

<?php
$this = "that";
if($this=="that") {
echo "this is that";
} else {
echo "that is not this!";
}
?>
-Nay
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

Code: Select all

<?php
if($bla != $alb)
{
do_this($something);
}
?>
:D look neat thats all.
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post by Kriek »

Stoneguard wrote:Yes, but is PEAR the only reason you code that way? Would you code another way if not for PEAR?
No, it just happens to be my personal preference.
This really has nothing to do with PEAR or Java.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Nay wrote:I hate unneccessary tabs or spaces in my scripts. Surprisingly, I don't find it hard to read at all - really!

Code: Select all

<?php
$this = "that";
if($this=="that") {
echo "this is that";
} else {
echo "that is not this!";
}
?>
This is much easier to read, imho (as well as the indents have added some whitespace to make the if steps stand out):

Code: Select all

<?php
$this = "that";

if($this=="that") 
{
    echo "this is that";
    
    } else {

    echo "that is not this!";
}
?>
Last edited by McGruff on Wed Aug 10, 2005 2:23 pm, edited 1 time in total.
fractalvibes
Forum Contributor
Posts: 335
Joined: Thu Sep 26, 2002 6:14 pm
Location: Waco, Texas

Post by fractalvibes »

Whatever makes it easier when you or your compadres have to go into the code a year from now and figure out what the **** it is doing and adapt.

Personally I like a little whitespace, indention, and lining up things for readablility.

When it comes to enbedded sql - I am absolutely rabid about making it readable. 5 minutes looking for the typo instead of an hour!

fv
Post Reply