Support forums : The Future

Coding guidelines

The future of this project but not in a "I want a pony" sort of way. This is all about everything meta about Quam Plures. The general direction, the support tools, stuff like that.

Moderator: Dracones

Coding guidelines

Postby Tblue » Mon May 10, 2010 7:12 pm

Edit [by Tblue, 2010-05-29]
Note: This has to be discussed a little bit more and a new, concise version should be written.


Our coding style is similar to K&R's (braces on a new line), but additionally, we use the following style:
  • Use brackets when possible. No statements without brackets; this makes the code more readable (at least in our opinion).
  • Spaces after opening and before closing parentheses. This contributes to code clarity.
  • Use two tabs for each indention level. Pad with spaces if necessary.
  • Use single quoted strings when possible.
  • One statement per line.
  • more?

Examples:
Code: Select all
if( ( do_something( 'Foo', 'bar' ) && $is_valid ) ||
    $blah )
{    // Comment. What is done here?
    do_it();
}
Tblue
Dracone
 
Posts: 340
Joined: Sat Nov 21, 2009 1:35 pm
Location: Berlin, Germany

Re: Coding guidelines

Postby EdB » Tue May 11, 2010 1:53 am

2 tabs per indent level and pad with spaces? One tab per level is where it's all at now, and works just fine. I see no need to ever pad with spaces. Oh and tabs should NEVER be used inline. Start a line with the right number of tabs, then use characters and spaces. (pet peeve: I hate finding a tab where a space is perfectly suitable ... and spaces pretending to be a tab)

One statement per line I'll go with even though I don't like it. I mean, I get it and all but isn't "if this and that or that and the_other or something_else" technically one statement? In a weird sort of way? Anyway I don't like it but will go with it.

No statements without brackets is AWESOME! I friggin hate "if( $foo ) echo $bar;" 2 more lines makes that so much better on the eyes and mind IMHO.

Single-quoted strings are almost always possible. In fact there is a 'test' using double quotes that I want to rip out by it's short hairs one day. Now I can say "Tblue made me do it" :)

Correct me if I'm wrong, but generally "a space after each ( and before each )" is appropriate? In other words, the spaces flank the thing inside the brackets. Currently almost all of our translatable strings do not do this - by the way.
Code: Select all
echo T_('there are no spaces outside these single quotes')
EdB
Dracone
User avatar
 
Posts: 2072
Joined: Sun Nov 22, 2009 7:20 am
Location: Maricopa Arizona

Re: Coding guidelines

Postby EdB » Fri May 28, 2010 9:20 pm

Tblue wrote:[*] Use two tabs for each indention level. Pad with spaces if necessary.

I think I got part of this somehow figured out. Don't like two tabs and since everything is already one tab that's much easier to stay with. Tabs in my editor take up the same as 2 spaces. I guess that is configurable?

Anyway the "pad with spaces" part I'm using for stuff like this:
Code: Select all
   function content_teaser( $params )
   {
      // Make sure we are not missing any param:
      $params = array_merge( array(
            'before'      => '',
            'after'       => '',
            'disppage'    => '#',
            'stripteaser' => '#',
            'format'      => 'htmlbody',
         ), $params );

Tabs connect the left edge to the first character, spaces make the => bits line up with each other. Dunno if that will look right here in phpbb but it looks good in my editor.

Oh and that seems to be a case of double-tabbing but I kinda felt that was due to "one tab per open (", so the params are double-tabbed in from the $params line

Anyway trying to apply these guidelines as I do branches :)
EdB
Dracone
User avatar
 
Posts: 2072
Joined: Sun Nov 22, 2009 7:20 am
Location: Maricopa Arizona

Re: Coding guidelines

Postby Yabs » Fri May 28, 2010 10:22 pm

Ohhh, I just use :
[tab]$params = array(
[tab][tab]'foo'[tab][tab]=>[space]'bar',
[tab][tab]'hello'[tab]=>[space]'world',

tabs set as 2 spaces here as well

¥
I may have opened the door but you entered of your own free will

Image Techno Babble II
Image Tacky Pad 3
Yabs
Dracone
User avatar
 
Posts: 896
Joined: Sat Nov 21, 2009 9:59 am


Return to The Future

Who is online

Users browsing this forum: No registered users and 1 guest

cron