Page 1 of 2

Your code format

Posted: Tue Sep 24, 2002 4:38 pm
by Takuma
How do you format your code, I use to do it like this

Code: Select all

<?php
<space><space>//hello
<space><space>echo hi;
<space><space>if($hi = true) &#123;
<space><space><space><space>echo hi;
<space><space>&#125;
?>
<space> representing a space. Then I recently changed it to using tabs

Code: Select all

<?php
<tab><tab>//hello
<tab><tab>echo hi;
<tab><tab>if($hi = true) &#123;
<tab><tab><tab><tab>echo hi;
<tab><tab>&#125;
?>
And also how would you place your braces

Code: Select all

<?php
  if(SOMETHING) &#123;
    SOMETHING
  &#125;
?>
or

Code: Select all

<?php
  if(SOMETHING)
  &#123;
    SOMETHING
  &#125;
?>

Posted: Tue Sep 24, 2002 4:50 pm
by JPlush76

Code: Select all

&lt;?php

if(SOMETHING) 
{ 
     DO SOMETHING COMPLETELY AMAZING THAT BOGGLES THE MIND
} 

?&gt;

Posted: Tue Sep 24, 2002 5:12 pm
by nielsene
I tend to user four space per indentation level, which braces on a line be themselves, splitting the identation ie

Code: Select all

&lt;?php
echo "Normal, no identation at top level";
if ($foo)
  { // indented two characters
    echo "Indented two chracters";
  }
echo "Back to normal";
?&gt;
This is the current default configuration for PHP-mode, I found, for Emacs.


I used to use four spaces per level, braces on lines by themselves, lined up with their starting block

Code: Select all

&lt;?php
echo "Normal, no identation at top level";
if ($foo)
{ 
    echo "Indented four chracters";
}
echo "Back to normal";
?&gt;
This was the default configuration for Perl-mode in Emacs which I used before I found PHP-mode. I actually like this style a little better, but PHP-mode does a much better job context coloring and I'm too lazy to hack the mode file.

I'm not 100% sure, but I believe these modes automatically convert the tabs to spaces.

Posted: Tue Sep 24, 2002 5:38 pm
by volka
.

Code: Select all

<html_tag_abc><?php
if ($a==$b)
<tab>singleStatment;
else
&#123;
<tab>some;
<tab>statements;
<tab>if ( (cond1) || (($c=function())!=$d) )
<tab><tab>whatEver();
&#125;
?><html_tag_xyz>
sometimes also $a != $b
but that's chiselled in stone nowhere ;)

Posted: Tue Sep 24, 2002 6:04 pm
by Coco
i only indent using tabs....
i indent one tab per level of nesting... ie:

Code: Select all

if(blah)&#123;
          do this
          if(blah2)&#123;
                    do this
          &#125;
&#125;

i *always* indent my closing brace to match the indent of the originating function (the } matches the indent of the 'if)

Posted: Tue Sep 24, 2002 6:08 pm
by JPlush76
if I have alot of conditional statements (ifs, whiles, fors) I'll comment the end brackets so I can always find my way

Code: Select all

&lt;?php

if(something)
{
     if(something else)
     {
        do this
     } // end if something else
} // end if something

?&gt;
not really needed for small code but 1000 line program with <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> all over the place makes it REAL handy to just code the end brackets when you want to change something

Posted: Tue Sep 24, 2002 6:15 pm
by nielsene
::) [Ignore this line]
JPlush76 wrote:if I have alot of conditional statements (ifs, whiles, fors) I'll comment the end brackets so I can always find my way
[snip]
not really needed for small code but 1000 line program with s**t all over the place makes it REAL handy to just code the end brackets when you want to change something
When I get in this situation, I desperately try to rethink the code to avoid that level of nesting, expecially over multiple screens of code. If I can't split it up into seperate functions to reduce the size, I'll comment the end brace, but its an absolute last resort for me....

Functions _should_ fit on a single screen. When I'm feeling blocked, I tend to go back and work on those functions I wrote that don't.

Posted: Tue Sep 24, 2002 6:17 pm
by JPlush76
the most I'll go is 3 or 4 levels but its coming from an as/400 background I guess 100 levels of nesting :)

now thats a mess, hehe

Posted: Tue Sep 24, 2002 6:26 pm
by Coco
ya i comment closing brackets too... (some of them are a hundred lines or so and i really dont wanna go hunting for them) but thats prolly just because i have some scripts that cant be functioned....

Posted: Tue Sep 24, 2002 6:31 pm
by nielsene
...everything can be functioned! (Course that doesn't always mean it should be, but.....)

Posted: Tue Sep 24, 2002 6:43 pm
by Coco
well yeah but if the function is different according to what the username is and there are only 2 versions there isnt much point may as well keep the 2 in the same script

Posted: Wed Sep 25, 2002 8:00 am
by volka
.

Code: Select all

...
	PGM 
	DCL &name *CHAR 12 VALUE('JPlush76')
	CALL PGMH PARAM(&name)
	RETURN
	ENDPGM

PGMH:
	PGM PARAM(&name)
	DCL &name *CHAR 12
	SNDPGMMSG ('hello '*CAT &name)
	RETURN
	ENDPGM
;)

Posted: Wed Sep 25, 2002 9:10 am
by mydimension
my style:

Code: Select all

if (condition) &#123;
    the resulting statements;
&#125; else &#123;
    some other statements;
&#125;
i use tabs which span four columns (for use in editors that ask for tab span)

Posted: Wed Sep 25, 2002 10:36 am
by BDKR
For me, it's,...

Code: Select all

if(($loaded==true) and ($pointed=="aimed"))
    {
    say("Allright Punk! Hand over the Swingline!", "with anger");
    if(swingline_acquire()==true)
        { run(); }
      else
        { 
        shoot();
        run();
        }
    }
else
    { walk_on_by(); }
Mine is rather similar to JPlush76's.

It's funny too. This is the kind of thing that has led to many a religous war across the net over the years, but the crowd here is pretty cool and mellow so I like playing along.

Cheers,
BDKR

Posted: Wed Sep 25, 2002 10:41 am
by nielsene
I, too, was afraid this would be a religious war, but this crowd is remarkably nice about such things...

Perhaps it's because the original poster asked about our style and not the flame-generating best style, which doesn't exist. No one here is trying to get anyone to change, only to show what they use. I hope that if anyone starts asking "why" someone uses a paticular style that we can all stay as civil and just accept their answer and not try to "educate" them into changing.

That's what I find makes this forum such a wonderful place, and the only forum I've stayed at for more than about 2 days....

Three Cheers for all of You! PHP! PHP! PHP! :)