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?
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
}
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.
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?
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.
if(condition)
{
//code
}
elseif(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(unsignedlong 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 }
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.
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.