So what format do you use?

I was reading around different forums and i noticed people tend to write their if statements differently than others. That got me wondering which is used more commonly. So which do you use more, the first example or the second example?

1.
if(){
stuff();
}

2.
if()
{
stuff();
}
1
2
3
4
if()
 {
   stuff();
 } 


I write it like this with spaces. So I guess #2 would be my answer.
It is called "brace style" or "indent style".
http://en.wikipedia.org/wiki/Indent_style

I tend to use (what I have learned is called) Whitesmith's style with two-space indents:

1
2
3
4
5
6
7
8
9
int fooey()
  {
  if (kablooey)
    {
    kapow();
    shazaam();
    }
  return kapow();
  }
The closest to my style would probably be Allman: http://en.wikipedia.org/wiki/Indent_style#Allman_style
1
2
3
4
5
6
7
8
9
if(a == b)
{
    while(a == b)
    {
        stuff_one();
        stuff_two();
        // etc...
    }
}

For me that makes the flow as clear as possible.
if(){stuff();} //all day every day
I'm with Galik, although that was only after 11 years at my last employer where it was the agreed-upon standard. Prior to that, I used the 1TBS. Whitesmith's style and GNU style are abominations. :-P

These days I don't even think about it. I set up my editor to format the code for me as I type.
for me it really depends if the code I'm going to modify uses a certain style, I usually stick to the style the author chose to be consistent.

also if I'm programming in php or javascript, I'd normally use the first style to have less lines in the code, because I think the web-server can load a smaller sized script faster, hence saving me some bandwidth as well even if it's only a few kilobytes.

1
2
3
4
5
if(condition) {
    // do something
} else {
    // do something else
}


for compiled languages though it's not really an issue (unless you want to speed up compile time?), so I would most likely use the second style:

1
2
3
4
5
6
7
8
if(condition)
{
    // do something
}
else
{
    // do something else
}


which is less cramped and easier to read for me.
Dacster13, File size != number of lines. Both blocks of code probably take the same amount of space. You have only changed 3 space chars into 3 new line chars.
I tried it before and it does effect the file size though. Maybe it just doesn't have much effect on such a small amount of code.

There is another good reason to do it that way though for php and javascript, interpreted languages executes the code on a line per line basis, so the first code would take 5 passes to get fully executed, whereas the 2nd one will need 8 passes, but it wouldn't really be noticeable in such a small script.
PHP interpreter DOES NOT execute script file on line by line basis, first load file in memory with comments and whitespaces removed.
PHP interpreter DOES NOT execute script file on line by line basis, first load file in memory with comments and whitespaces removed.


I've always read in books that interpreters executed the source line by line, and I thought that was also the reason why php only shows one error at a time as well, so thanks for clarifying that then.

I think I'll still keep using that style for php and javascript however cause I'm quite used to it by now.

Anyway, what kind of indent-style do you use though?
Last edited on
Modoran is correct. I use PHP all the time, and you could put 50 lines of space in it and it will execute the output at the same speed each time. PHP is similar to a compiler, it ignores white space.
I prefer the second style, bur do-while loops break my reasoning for it. I try to keep {} entirely on their own lines to promote spacing in my code and make it easier to see the flow of control.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
if(condition)
{
    //code
}
else if(condition)
{
    //one-liner still in braces
}
else
{
    //code
}

do
{
    //code
}while(condition); //this seem awkward but can't be helped
                   //without going onto a new line and looking detatched
for(unsigned long i = 0; i < count; ++i) //I prefer to specify unsigned and use pre-increment
{
    //code
}
std::cout << "one-liner cout statement" << std::endl;
std::cout <<
    "or I'll use string "
    "concatenation for"
    " really long strings" << std::endl <<
    "like for generating "
    "HTML for webservers" << std::endl;

int x; //I NEVER do this, I ALWAYS specify signed or unsigned

std::string MyString (constructor, args); //UpperCamelCase and space before constructor

//sometimes I use nameless scopes
{
    //like so for temporary stack variables
}
//that get destructed on the } 
Last edited on
L B you have the same thinking I do. :)
I (generally) use 1TBS, but after reading the Wiki page I'm thinking of switching to Allman because of the control structure out-commenting.

I'm just not a fan of "open lines" that just have a brace or something similar. Even if it doesn't matter performance-wise, I still like my code dense.
I just try to make my c++ look like a combination of python and css
haha, thats interesting... it seems like im the only one using the first style, and it doesnt really look that messy to me, it looks perfectly normal. although if i was just starting i would probably go with whitesmiths style or the second one, but since im used to it, ill just go with the first one.
I use number 2. That's how I learned it from the textbooks I learned off of, so it stuck. lol.
Topic archived. No new replies allowed.