I suppose this only really woks using the same language so I'm gonna say for C++. But I started using Unity3D and MonoDevelop which comes with it seems to have the most annoying possible default auto-formatting possible.
My style is kinda like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
namespace Name{
class Class{
statement;
FuncCall(arg1, arg2);
};
type member;
type function(){
if(cond){
doSomething();
doSomethingElse();
}
}
}
Where as MonoDevelop wants to do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
namespace Name //WHY IS OPENING BRACE ON A DIFFERENT LINE?!
{
class Class //Same again
{
statement;
FuncCall (arg1, arg2); //WHY IS THERE A SPACE HERE?!
};
type member;
type function ()
{
if (cond)
{
doSomething ();
doSomethingElse ();
}
}
}
It just seems that MonoDevelop is so far away from what looks nice and neat to me, I couldn't imagine many people want all these spaces everywhere. What's your natural typing format?
I love using new lines for the opening brace, the top and bottom braces are then in line vertically so it's really easy to see which braces go together although I have no idea what's with all of the spaces before the brackets, that just seems to be really messy.
I always put opening braces on a new line except for one case: functions defined in a class definition. Don't know why I made that a habit but its pretty much ingrained in my style now. I also put several spaces between declarations in a header file and function definitions in a source file. I never put a space between function names and parenthesis but I do for every other statement. I would've written code but my PC is broke and I suck at typing on mobile.
Okay I know that often people put the opening curly braces on new lines (and never realised why) but I just let the closing curly brace line up with the start of the declaration.
But there's lots of other things MonoDevelop does like it ALWAYS adds an empty line after an opening of something (classes, functions, ifs, etc)
1 2 3 4 5 6
void function ()
{
//Cursor automatically thrown here
}//Brace automatically here
But yeah, nice to see that it's not just me being completely loopy and Mono is set to quite an unusual format.
I would normally use something similar to @SatsumaBenji, though with a few more spaces throw in:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
namespace Name {
class Class {
statement;
FuncCall(arg1, arg2);
};
type member;
type function() {
if (cond) {
doSomething();
doSomethingElse();
}
}
}
I (personally) find the reasoning for the braces being on the same line rather silly - normally its easier to look at the indentation then the braces in any case, and all the braces do is add more space between the start of the block and the end, separating it from the contents of the block.
Oh, let me chime in on the never-ending style discussion :)
I'm with SatsumaBenji on the general style. And I too hate the opening brace on a separate line.
BUT:
I caught myself leaving an empty line between the function header and the first line of the body when both lines are pretty wide, like:
1 2 3 4 5 6 7 8
void foo(int a, int lot, int of, int parameters){
int stuff = that_fills_a_line_pretty_good(a, lot, of, parameters);
while(stuff--){
if(whatever()) break;
}
}
instead of:
1 2 3
void foo(int a, int lot, int of, int parameters){
int stuff = that_fills_a_line_pretty_good(a, lot, of, parameters);
...
From that point of view it makes sense to separate the two lines by putting the brace in between.
And talking about parenthesis:
Can someone please ban things like this from the C++ standard:
Well that I can see sense with... Usually I don't leave extra lines and I also usually write my parameters all on one line but when I'm writing WinMain I write parameters over multiple lines and leave a spare line after opening.
My moan is mostly about Mono forcing these behaviors by default when it looks like most of us wouldn't get along with it :)
And it's kind of interesting to see that I'm not alone in the keeping the braces on one line, and keeping spaces compact.
Just to chime in with my own opinion, my personal preference is to put the opening brace at the start of the next line, so that it lines up with the closing brace. That feels more natural and intuitive to me than putting it at the end of the previous statement.
It's interesting to note that when K & R first published their seminal book, they originally laid their code out in that style. The publisher decided that they could save money by using less paper by moving the opening brace to the beginning of the previous line, and so that was the style that appeared in the published book. That style was always known as "K-and-R style" when I first started my career; I wonder how they felt about that :D
I'm also a fan of using whitespace to make it easier to read code at a glance. That includes blank lines at times.
Having said all that, the most important thing is to be consistent within the unit of code you're working on. When I have to work on some pre-existing code, I'll adapt to the style it's already written - because an inconsistent mixture of styles is always going to be more confusing for the person who has to come after me.
My moan is mostly about Mono forcing these behaviors by default when it looks like most of us wouldn't get along with it :)
Any IDE worth using will have some way of changing the defaults, so you won't be forced to use a style you don't like.
But there's lots of other things MonoDevelop does like it ALWAYS adds an empty line after an opening of something (classes, functions, ifs, etc)
That seems a bit stupid to me. Opening a block with a brace on a single line already supplies enough whitespace to clearly demarcate the new block from the surrounding code.
Can someone please ban things like this from the C++ standard:
return(2);
sizeof(foo_t);
I die a little inside every time I see that.
Are you referring to the redundant parentheses around expressions after return and sizeof (although if foo_t is a type, they are needed there) or to something about the spacing in those lines (since this thread is about spacing)?
For a bit of trivia, C++11 technically disallowed automatic move for return(x); where allowed for return x;, although every compiler writer of course understood the intent -- C++14 nevertheless additionally clarified that the parentheses have no meaning in that case. but then C++14 introduced a completely different meaning for return x; vs return(x); if the function happens to be declared to return a decltype(auto).
To use sizeof, the keyword "sizeof" is followed by a type name or an expression (which may be merely a variable name). If a type name is used, it must always be enclosed in parentheses, whereas expressions can be specified with or without parentheses.
C99_Standard wrote:
sizeof unary-expression
sizeof ( type-name )
File a bugreport: your compiler does not conform to standard.
Actually about you mentioning redundant brackets made me remember that in mathematics usually the trigonometric functions and logarithmic functions are often used without any brackets and it's really confusing.
i.e. sin 2 x ln x
I always place my function arguments in brackets even when it's not coding because I'd naturally read the above as sin(2).x.ln(x)... But the intent could be sin(2x).ln(x) or even really dodgy sin(2x.ln(x))