Your code format

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

User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Your code format

Post 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;
?>
Last edited by Takuma on Wed Sep 25, 2002 1:27 am, edited 1 time in total.
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

Code: Select all

&lt;?php

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

?&gt;
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

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

Post 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 ;)
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post 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)
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post 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
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post 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
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post 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....
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

...everything can be functioned! (Course that doesn't always mean it should be, but.....)
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

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

Post 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
;)
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

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

Post 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
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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! :)
Post Reply