Personal preferences and myths is all we really have to go by.
I prefer the the second and with nothing but myth to go on believe the former came about to reduce the volume of paper needed in both publishing language references, articles and such as well as programming print outs pre-portable computer days.
hsteve:
Are there any real advantages to using the first style ? Why does it seem to be the standard these days ?
It reduces the number of lines that a given piece of code takes up. Which used to be important, when we wrote programs on teletype terminals. Nowadays, not so much. I strongly prefer to see the matching braces lined up so that the intended structure of the code is obvious. The only reason I can see these days for using the other style is if it's what you're used to. (I personally don't see that as a good reason to burden another generation of programmers with the same style, but when you're used to something it gets hard to change.)
void setup ()
{
Serial.begin (115200);
} // end of setup
void loop ()
{
} // end of loop
I think it is logical for the start and end braces to have their own lines, it is just asymmetrical for them not to be like that.
And "things within the braces" should be indented, as are the braces themselves, in my own style universe.
I also have a space between a function name and the bracket, and I like to comment what the purpose of closing braces are (including in "if", "switch", "while" etc.).
[quote author=Nick Gammon link=topic=151874.msg1140391#msg1140391 date=1362272372]... and I like to comment what the purpose of closing braces are (including in "if", "switch", "while" etc.).
[/quote]
[quote author=Nick Gammon link=topic=151874.msg1140391#msg1140391 date=1362272372]... and I like to comment what the purpose of closing braces are (including in "if", "switch", "while" etc.).
[/quote]
No offense Nick but so far as I'm concerned only adds noise. But then again my editor highlights, and can collapse, the code between braces so it very easy to see what belongs to what.
void function_name(parameters)
{
function definition ;
}
To me, this is the way I code, and i'd refuse to do it any other way.
It makes it neater, easier to read... most other languages work in the same way
Procedure Function_Name(Params...);
Begin
End;
It seems the most easiest and most logical way to code and always will... if i ever had a boss "ride me" until i stopped, i'd probably end up seriously
hurting him one day....
style rather intensely. And it seems quite a lot of other people don't favour it either.
The explanations I have seen for why this style is "better" seem to be rather unconvincing, but
along the lines that with the opening curly brace at the end of the line with the function name,
you can tell that it really is the function definition and not just a declaration. Which is
an unconvincing reason to me. But then, people who write computer code as an end in itself
rather than as a means to an end, often have an interest in writing obsure code.
If I really want to save space, I sometimes do this
int some_function()
{ // start the calculation on this line so the line is not wasted
// and this is the second line of the calculation
}
This avoids one of the (almost) empty lines and the curly braces still line up.