Last one has no braces on a one-liner, something else I'm against. If you add anything else, you'll need to add the braces anyways. Might as well have them now.
Linux kernel style is to /not/ use braces on one line statements unless they're nested. A space afterwards is required for clarity. In the case of nested control statements or blocks larger than one statement, style 1 above is used to reduce line count while keeping clarity.
Thank you, that helps. I need to choose a style, and I`m going for which gets voted best that`s the one I will go with. Currently in my book they use the first one when the scope has a lot in it, but used the third one if there just a line or two.
I prefer first one. I dislike (almost) empty line between condition and body. Sometime when I write one-time code to test something I use third one, but it is too easy to make a mistake which will not be easy to find.
Second one. Though I would feel bad if I didn't mention that choosing what style to use based on what others like probably isn't the best way to go about it in my opinion.
If it is your choice on what style you can use (IE the codebase doesn't have a set style guideline in place) then choose whatever looks best to you and most importantly be consistent. Most programmers know how to read and write pretty much every common style in the book quite well so might as well choose one that you like the look of and then just be consistent with it.
Nothing makes the code more confusing then different styles plastered all over the same file (If you are working on a file that you didn't write try and use the same style that is already in use).
So my point is choose one that you like and that suits you, but be prepared to use and read other styles because you will have to specially on larger projects.
Thanks guys, I do like the first one as it saves lines and is a little more clear to me but I might have to start with the second one as I see loads using it and I should get myself used to it to.
You should definitely not choose a style based on what others use. All of these styles are still in rather heavy use anyways. You should either follow what your current project dictates (assuming it has rules laid out specific to the project), or use what you think is most clear.
I prefer braces to always be on their own line, it's easier for my brain to process the way the code looks then. Otherwise there is too much variety and I cannot easily tell which code belongs to which blocks.
@YellowPyrmid
The first two methods are fine. The last one you have to be careful with because if you don't have the braces it only executes the very next line after the for loop.
ResidentBiscuit wrote:
First one mixes styles, big nono.
Says who? Bjarne's latest edition of The C++ Programming Language has code like this:
As for the last option is only good if you want to print out a single statement:
1 2 3 4 5 6 7 8 9 10 11 12
for(int i = 0; i <= 10; ++i)
std::cout << i << std::endl; // only this is printed out if you exclude {}
// this is considered to be outside the for loop
// and will only be printed after the loop is done...
// or in this case will give an error because I'm
//accessing i out of scope
if( i % 2 == 0){
std::cout << i << " is even!" << std::endl;
}else{
std::cout << i << " is not even!" << std::endl;
}
test.cc:7:10: warning: if statement has empty body [-Wempty-body]
if(stuff); // < Notice the semi colon
^
test.cc:7:10: note: put the semicolon on a separate line to silence this warning
[-Wempty-body]
1 warning generated.
gcc says this:
test.cc: In function 'int main()':
test.cc:7:10: warning: suggest braces around empty body in an 'if' statement [-Wempty-body]