TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-10-2009, 01:13 PM   #1 (permalink)
The Contributor
 
Runar's Avatar
 
Join Date: Nov 2008
Location: Norway
Posts: 58
Thanks: 20
Runar is on a distinguished road
Default How do you write your code?

Greetings!

I recently came across this Wikipedia article on indent styles, and thought it could be fun to see how the member of this community writes their code.

I would like to point out that I did not pick the style I liked the most and started writing that way, after reading the article. I found the style most equal to the way I write my code. It turned out to be a mixture of Allman style and my own.

Example:

PHP Code:
if( == )
{
    print( 
);

So, how do you write your code?

Let me know if the thread title could be better.


Yours, Runar
Send a message via MSN to Runar
Runar is offline  
Reply With Quote
Old 06-10-2009, 02:21 PM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

I generally stick to Allman/BSD style, easiest to read IMO.
__________________

Village Idiot is offline  
Reply With Quote
Old 06-10-2009, 03:01 PM   #3 (permalink)
The Acquainted
 
Hightower's Avatar
 
Join Date: May 2009
Location: Durham, UK
Posts: 134
Thanks: 9
Hightower is on a distinguished road
Default

Based on the code you provided I would write it like this:

PHP Code:
if (== y) {
    print(
x);

I like spaces after 'if' and always put the '{' on the same line. Not sure why, but I don't like spaces after other functions like the shown print(x). Guess it's because it looks messy when you start nesting them in one another.
__________________
Hightower's Softpolio
Send a message via MSN to Hightower
Hightower is offline  
Reply With Quote
Old 06-10-2009, 05:42 PM   #4 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

One thing I've began to do that most do not is placing the constant to the left and the variable to the right. For example
PHP Code:
if(1==$variable)
{
//do this

This makes it harder to accidentally forget the second = and assign the variable. Since I program in VB as a profession (one = check in an IF statement) and do PHP and C on the side, it is very easy to accidentally put one = instead of two. With this approach, the program will generate a parse error because you are trying to assign a constant.

It looks awkward at first but becomes very useful once you get used to it.
__________________

Village Idiot is offline  
Reply With Quote
Old 06-10-2009, 09:27 PM   #5 (permalink)
The Contributor
 
Normo's Avatar
 
Join Date: Oct 2008
Location: UK
Posts: 30
Thanks: 0
Normo is on a distinguished road
Default

Something like this:
PHP Code:
if($a == 1){
  echo 
"Hello world!";

Normo is offline  
Reply With Quote
Old 06-10-2009, 09:46 PM   #6 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

I prefer to use a space after things like if, before the opening parenthesis, and to keep curly brace pairs at the same indent level (Allman style). E.g.:

PHP Code:
if ($a == 1)
{
    echo 
'Hello world!';

The main thing, in my opinion, is to choose (or arrive at) a style and to be consistent with it rather than believing one way is any more "correct" than any other.
Salathe is offline  
Reply With Quote
Old 06-10-2009, 10:41 PM   #7 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

Mine is like this
PHP Code:
if($a == 1){
    print(
'this are'' arguments');

tony is offline  
Reply With Quote
Old 06-11-2009, 01:44 AM   #8 (permalink)
The Addict
 
Join Date: May 2009
Posts: 287
Thanks: 5
adamdecaf is on a distinguished road
Default

PHP Code:
if ($n == 12 || $n == (11)) {
// Do something
     
lets_call_a_function($with$some$variables);

Ive tried a few of styles and this is a mix of spaces, single lines, ect...
__________________
My Site
adamdecaf is offline  
Reply With Quote
Old 06-11-2009, 09:18 AM   #9 (permalink)
The Contributor
 
Sakakuchi's Avatar
 
Join Date: Feb 2009
Posts: 64
Thanks: 1
Sakakuchi is on a distinguished road
Default

Just like Salathe
PHP Code:
if ($a == 1)
{
    echo 
'Hello world!';

Sakakuchi is offline  
Reply With Quote
Old 06-11-2009, 09:40 AM   #10 (permalink)
The Contributor
 
dschreck's Avatar
 
Join Date: Nov 2007
Location: California
Posts: 82
Thanks: 0
dschreck is on a distinguished road
Default

PHP Code:
$camelCase 42;

$arr['and_indent']         = 1;
$arr['like_variables']     = 2;
$arr['as_its_easier']    = 3;
$arr['to_read']            = 4;


if(
$a == 1)
{
    echo 
'I LIKE PIE';
}

define('ONLY_DEFINES_ARE_CAPS',true);

if(
defined('ONLY_DEFINES_ARE_CAPS') && ONLY_DEFINES_ARE_CAPS === true)
{
    echo 
'And for heavens sake, check the type and value of booleans';

__________________
Where I Ramble: http://www.iwilldomybest.com/
What I do: Zynga Game Network
Senior Software Engineer at CityVille
dschreck is offline  
Reply With Quote
Old 06-11-2009, 10:31 AM   #11 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Allman/BSD for me too.
I also like to line up things like so:
PHP Code:
$array['paths.controllers'] = $appPath DIRECTORY_SEPERATOR 'Controllers' DIRECTORY_SEPARATOR;
$array['paths.models']      = $appPath DIRECTORY_SEPERATOR 'Models'      DIRECTORY_SEPARATOR;
$array['paths.views']       = $appPath DIRECTORY_SEPERATOR 'Views'       DIRECTORY_SEPARATOR
Makes it a lot easier to scan read stuff.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 06-11-2009, 11:02 AM   #12 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

php Code:
if($a == $b)
{
<spacer>
     // Comments are indented aswell
     $c = $a;
<spacer>
}

javascript Code:
if(a == b) {
<spacer>
      // Comments are indented aswell
      var c = a;
<spacer>
}

css Code:
div#container {
     color: blue;
     background-color: red;
     margin: 3px 0 0 3px;
}
Or if it's short
css Code:
div#container { color: blue; }
__________________
Tanax is offline  
Reply With Quote
Old 06-11-2009, 08:56 PM   #13 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I am the same as Salathe and a few others in this thread. However, touching on Village Idiot's point, I much prefer my variables to the left of the invariables.

As we typically read left from right, I like to see the variables and/or constants that are involved in the conditional immediately, instead of seeing the invariable, and then seeing what it's being checked against.

As Zend Framework is built by many programmers, I've noticed they use both ways in their framework code. I never thought of it as being a way to prevent accidental typos. It's always nice to have a new perspective on things !
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 06-11-2009, 10:04 PM   #14 (permalink)
The Addict
 
Join Date: May 2009
Posts: 287
Thanks: 5
adamdecaf is on a distinguished road
Default

@Tanax I've seen this CSS style used by a few people and I really like it, what do you think?

css Code:
#content {
    color: #FFFFFF;
}

      #content a {
           color: #009900;
      }

            #content a:hover {
                  color: #00CC00;
            }

      #content p {
           clear: left;
      }

#footer {
    border: 1px solid #000000;
}
__________________
My Site
adamdecaf is offline  
Reply With Quote
Old 06-11-2009, 10:16 PM   #15 (permalink)
The Contributor
 
Runar's Avatar
 
Join Date: Nov 2008
Location: Norway
Posts: 58
Thanks: 20
Runar is on a distinguished road
Default

I actually do the same, adamdecaf. It has worked pretty good for me so far.
Send a message via MSN to Runar
Runar is offline  
Reply With Quote
Old 06-12-2009, 03:08 PM   #16 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
As we typically read left from right, I like to see the variables and/or constants that are involved in the conditional immediately, instead of seeing the invariable, and then seeing what it's being checked against.
Which is why it takes some time to get used to. The error is more common to me than most people since I program for eight hours a day in a language that takes one equal sign to compare. Then I come home and start coding C, that bug was tripping me up very often, but using that method it can't even compile if I did it.
__________________

Village Idiot is offline  
Reply With Quote
Old 06-12-2009, 03:32 PM   #17 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Quote:
Originally Posted by adamdecaf View Post
@Tanax I've seen this CSS style used by a few people and I really like it, what do you think?

css Code:
#content {
    color: #FFFFFF;
}

      #content a {
           color: #009900;
      }

            #content a:hover {
                  color: #00CC00;
            }

      #content p {
           clear: left;
      }

#footer {
    border: 1px solid #000000;
}
Well, I must say that it looks pretty logical. Though I think it looks ugly. But perhaps I'll try it! Whatever is easier to read and understand is the way you should use in the end
__________________
Tanax is offline  
Reply With Quote
Old 06-12-2009, 08:06 PM   #18 (permalink)
The Contributor
 
Sakakuchi's Avatar
 
Join Date: Feb 2009
Posts: 64
Thanks: 1
Sakakuchi is on a distinguished road
Default

that css-code-style is new to me to. Might give it a try...
Sakakuchi is offline  
Reply With Quote
Old 06-13-2009, 11:31 PM   #19 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

@adamdecaf

I do something similar. instead of:

PHP Code:
#content {
    
color#FFFFFF;
}

      
#content a {
           
color#009900;
      
}

            
#content a:hover {
                  
color#00CC00;
            
}

      
#content p {
           
clearleft;
      }

#footer {
    
border1px solid #000000;

I would do

PHP Code:
#content { color:#FFF; }
  #content a { color:#090; }
  #content a:hover { color:#0C0; }
  #content p { clear:left; }
#footer { border:1px solid #000; } 
I always keep the css to 1 line. When you start spacing them out the files get so damn long. We widescreen monitors for a reason. Use them. Also if you put the attributes in alpha order its even easier to find them. (ie border, color, float, width..)

Same thing when I am programming I would do it in the format of

PHP Code:
class blah 
{
  function 
hello
  
{
    if( 
== ) { 
      echo 
"Question"
      for(
010x++) {
        
x++;
        print 
x;
      }
    } 
  }

CoryMathews is offline  
Reply With Quote
Old 06-14-2009, 12:08 AM   #20 (permalink)
The Addict
 
Join Date: May 2009
Posts: 287
Thanks: 5
adamdecaf is on a distinguished road
Default

@CoryMathews

Your style seems great, but it seems to crammed for me. I have also learned to sort the CSS by alpha-numeric, it really speeds up the searching process.

Also you can compress the final CSS with this tool: Online CSS Compressor - CSSDrive.com, it offers a wealth of options to suit your compression needs.

One of my friends likes to put his CSS in the single line format, I guess its personal preference as to what style is used.
__________________
My Site
adamdecaf is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Tips to Improve Your Coding and Projects Village Idiot Tips & Tricks 45 12-03-2012 07:46 AM
Writing Clean Code Village Idiot Tips & Tricks 10 06-25-2012 12:35 PM
Game: Let's Develop Something Crazy! Wildhoney The Lounge 25 05-23-2009 09:18 PM
Locations based on ZIP code... allworknoplay Absolute Beginners 20 05-13-2009 03:55 PM
Snipply.com - Code snippet website codyodell Show Off 27 04-13-2008 02:09 PM


All times are GMT. The time now is 11:54 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design