TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   The Lounge (http://www.talkphp.com/lounge/)
-   -   How do you write your code? (http://www.talkphp.com/lounge/4524-how-do-you-write-your-code.html)

Runar 06-10-2009 01:13 PM

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

Village Idiot 06-10-2009 02:21 PM

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

Hightower 06-10-2009 03:01 PM

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.

Village Idiot 06-10-2009 05:42 PM

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.

Normo 06-10-2009 09:27 PM

Something like this:
PHP Code:

if($a == 1){
  echo 
"Hello world!";



Salathe 06-10-2009 09:46 PM

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.

tony 06-10-2009 10:41 PM

Mine is like this
PHP Code:

if($a == 1){
    print(
'this are'' arguments');



adamdecaf 06-11-2009 01:44 AM

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...

Sakakuchi 06-11-2009 09:18 AM

Just like Salathe
PHP Code:

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



dschreck 06-11-2009 09:40 AM

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';



sketchMedia 06-11-2009 10:31 AM

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.

Tanax 06-11-2009 11:02 AM

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; }

Wildhoney 06-11-2009 08:56 PM

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

adamdecaf 06-11-2009 10:04 PM

@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;
}

Runar 06-11-2009 10:16 PM

I actually do the same, adamdecaf. It has worked pretty good for me so far.

Village Idiot 06-12-2009 03:08 PM

Quote:

Originally Posted by Wildhoney (Post 25243)
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.

Tanax 06-12-2009 03:32 PM

Quote:

Originally Posted by adamdecaf (Post 25254)
@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 :-)

Sakakuchi 06-12-2009 08:06 PM

that css-code-style is new to me to. Might give it a try...

CoryMathews 06-13-2009 11:31 PM

@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;
      }
    } 
  }



adamdecaf 06-14-2009 12:08 AM

@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.


All times are GMT. The time now is 08:13 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0