 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
|
 |
|
 |
06-10-2009, 01:13 PM
|
#1 (permalink)
|
|
The Contributor
Join Date: Nov 2008
Location: Norway
Posts: 58
Thanks: 20
|
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( x == y )
{
print( x );
}
So, how do you write your code?
Let me know if the thread title could be better.
Yours, Runar
|
|
|
06-10-2009, 02:21 PM
|
#2 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
I generally stick to Allman/BSD style, easiest to read IMO.
|
|
|
|
06-10-2009, 03:01 PM
|
#3 (permalink)
|
|
The Acquainted
Join Date: May 2009
Location: Durham, UK
Posts: 134
Thanks: 9
|
Based on the code you provided I would write it like this:
PHP Code:
if (x == 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.
|
|
|
06-10-2009, 05:42 PM
|
#4 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
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.
|
|
|
|
06-10-2009, 09:27 PM
|
#5 (permalink)
|
|
The Contributor
Join Date: Oct 2008
Location: UK
Posts: 30
Thanks: 0
|
Something like this:
PHP Code:
if($a == 1){ echo "Hello world!"; }
|
|
|
|
06-10-2009, 09:46 PM
|
#6 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
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.
|
|
|
|
06-10-2009, 10:41 PM
|
#7 (permalink)
|
|
The Addict
Join Date: Aug 2008
Posts: 336
Thanks: 8
|
Mine is like this
PHP Code:
if($a == 1){ print('this are', ' arguments'); }
|
|
|
|
06-11-2009, 01:44 AM
|
#8 (permalink)
|
|
The Addict
Join Date: May 2009
Posts: 287
Thanks: 5
|
PHP Code:
if ($n == 12 || $n == (1 + 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...
|
|
|
|
06-11-2009, 09:18 AM
|
#9 (permalink)
|
|
The Contributor
Join Date: Feb 2009
Posts: 64
Thanks: 1
|
Just like Salathe
PHP Code:
if ($a == 1)
{
echo 'Hello world!';
}
|
|
|
|
06-11-2009, 09:40 AM
|
#10 (permalink)
|
|
The Contributor
Join Date: Nov 2007
Location: California
Posts: 82
Thanks: 0
|
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';
}
|
|
|
|
06-11-2009, 10:31 AM
|
#11 (permalink)
|
|
The Prestige
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
|
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)
|
|
|
|
06-11-2009, 11:02 AM
|
#12 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
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; }
__________________
|
|
|
|
06-11-2009, 08:56 PM
|
#13 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
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.
|
|
|
06-12-2009, 03:08 PM
|
#14 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
Quote:
Originally Posted by Wildhoney
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.
|
|
|
|
06-11-2009, 10:04 PM
|
#15 (permalink)
|
|
The Addict
Join Date: May 2009
Posts: 287
Thanks: 5
|
@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; }
|
|
|
|
06-12-2009, 03:32 PM
|
#16 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Quote:
Originally Posted by adamdecaf
@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 
__________________
|
|
|
|
06-11-2009, 10:16 PM
|
#17 (permalink)
|
|
The Contributor
Join Date: Nov 2008
Location: Norway
Posts: 58
Thanks: 20
|
I actually do the same, adamdecaf. It has worked pretty good for me so far.
|
|
|
06-12-2009, 08:06 PM
|
#18 (permalink)
|
|
The Contributor
Join Date: Feb 2009
Posts: 64
Thanks: 1
|
that css-code-style is new to me to. Might give it a try...
|
|
|
|
06-13-2009, 11:31 PM
|
#19 (permalink)
|
|
The Addict
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
|
@adamdecaf
I do something similar. instead of:
PHP Code:
#content {
color: #FFFFFF;
}
#content a {
color: #009900;
}
#content a:hover {
color: #00CC00;
}
#content p {
clear: left;
}
#footer {
border: 1px 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( a == b ) {
echo "Question";
for(x = 0; x < 10; x++) {
x++;
print x;
}
}
}
}
|
|
|
|
06-14-2009, 12:08 AM
|
#20 (permalink)
|
|
The Addict
Join Date: May 2009
Posts: 287
Thanks: 5
|
@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.
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Hybrid Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|