Page 1 of 2
Brace Yourself
Posted: Wed Oct 15, 2003 6:44 pm
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.
Posted: Wed Oct 15, 2003 7:23 pm
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.
Posted: Wed Oct 15, 2003 7:24 pm
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...
Posted: Thu Oct 16, 2003 2:13 am
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
Posted: Thu Oct 16, 2003 3:16 am
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
Posted: Thu Oct 16, 2003 7:58 am
by Stoneguard
Opps, sorry. I tend to only scan the front page for subjects.
Posted: Thu Oct 16, 2003 8:31 am
by volka
don't worry, there's always someone who remembers

Posted: Fri Oct 17, 2003 9:42 pm
by Kriek
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";
}
?>
See ->
Coding Style
Posted: Fri Oct 17, 2003 9:58 pm
by Stoneguard
Yes, but is PEAR the only reason you code that way? Would you code another way if not for PEAR?
Posted: Sat Oct 18, 2003 12:30 am
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
Posted: Sat Oct 18, 2003 6:57 am
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
Posted: Sat Oct 18, 2003 7:01 am
by qads
Code: Select all
<?php
if($bla != $alb)
{
do_this($something);
}
?>

look neat thats all.
Posted: Sat Oct 18, 2003 12:37 pm
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.
Posted: Sat Oct 18, 2003 5:55 pm
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!";
}
?>
Posted: Tue Oct 21, 2003 10:30 pm
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