I've spent most of the last several years working mainly with C# and SQL. Every programmer I've worked with over that time was in the habit of placing the opening brace of a function or control flow statement on a new line. So ...
public void MyFunction(string myArgument)
{
//do stuff
}
if(myBoolean == true)
{
//do something
}
else
{
//do something else
}
I have always been struck by how space wasteful this is, especially in if/else statements. And I know alternatives exist in later versions of C#, like:
if(myBoolean == true)
//do something on one line of code
But hardly anyone used them. Everyone did the curly-brace-on-newline thing.
Then I got back into doing JavaScript after a long absence. In my memory, JavaScript developers used to do the exact same curly-brace-newline thing but with all the fancy new libraries and stuff, most developers put the opening brace after the declaration:
function MyJavaScriptFunction() {
//do something
}
You can see the sense in this, because since using closures and function pointers has become popular in JavaScript, it saves a lot of space and makes things more readable. So I wondered why it wasn't seen as the done thing in C#. In fact, if you try the above construct in Visual Studio 2013, it actually reformats it for you, putting the opening brace on a new line!
Now, I just saw this question on Code Review SE: https://codereview.stackexchange.com/questions/48035/questions-responses-let-me-tell-you-about-you In which I learned that in Java, a language I'm not overly familiar with, it's considered de-rigour to open your curly braces right after the declaration, in modern JavaScript fashion.
I had always understood that C# was originally modelled after Java, and kept to a lot of the same basal coding standards. But in this instance, it seems not. So I presume there must be a good reason: what is the reason? Why do C# developers (and Visual Studio) enforce opening curly brackets on a new line?
if(myBoolean == true)makes little sense to me. While we're at it, while notif ((myBoolean == true) == true)?? Justif (myBoolean)and that's enough. Sorry, a pet peeve of mine. – Konrad Morawski Apr 25 '14 at 14:42if(myBoolean){}. No need for a belt and suspenders. In fact, since someone here has probably guessed that I think highly of many of Steve McConnell's conclusions, I like to see somewhat self-commenting code likebool allDocumentsAreReady = (/*long list of conditions*/); if( allDocumentsAreReady ){ /* do something */ }. FWIW. – Craig Tullis Apr 25 '14 at 14:47condition == falsethough and this is where I start having doubts :)!is more brief, but it's easier to miss in case of a condition likeif (!String.IsNullOrEmpty(settings.UserName))– Konrad Morawski Apr 25 '14 at 14:53bool allDocumentsAreReady = (/*long list of conditions*/); if( ! allDocumentsAreReady ){ /* do something */ }– Craig Tullis Apr 25 '14 at 15:05var fn = function() {return {a:1};};will return an object, vsvar fn = function() {\n return\n {\na:1\n};\n };which will returnundefinedbecause of automatic semicolon insertion. – Zev Spitz Mar 06 '16 at 23:30return {is not the same as thereturnand the{in different lines, becausereturnwithout anything on the same line becomesreturn;... and the rest is ignored. This is only relevant when working with literal objects and not with functions, but for consistency it's better to stick to the single rule "brace on the same line". – bgusach Oct 07 '16 at 11:30#if foo,#else, and#endiflines. Having a curly bracket at the end of each method signature line would mean they don't match up with the single closing curly bracket. (1/2) – Amos M. Carpenter Jun 05 '19 at 07:06