C# convention

  • Thread starter Thread starter John Salerno
  • Start date Start date
J

John Salerno

I just realized that the C# convention for uses braces is to put the
opening brace immediately after the first line that defines the block
(except for class declarations) like this:

if (something) {
stuff
}

Why is that? Do a lot of people do this? It seems like putting braces on
separate lines is much nicer and easier to read. In the book I'm
reading, it says it's important that the closing brace line up with the
first line (like above), I guess for readability, but how common is
this? It doesn't seem as readable as this:

if (something)
{
stuff
}
 
Oh dear. The curly brace debate. There's a special punishment reserved
for people who resurrect this one. ;-)

I used to be passionate about this university. I knew others who were
equally passionate about using the other style. There was even a third
option, which looked like this:

if (something)
{ stuff
...
}

Twenty years on, I have a very simply rule: use whatever style the IDE
gives you for free. In cases like Visual Studio, where the IDE allows
you to choose, everybody pick one common style and stick to it.

Really! "Readable" is in the eyes of the beholder, and the only
less-readable code is code that's formatted in an unfamiliar way, which
forces you to do extra mental work to read it. If everyone in your
company is accustomed to seeing braces lined up vertically, then use
that style. If everyone is used to saving vertical space by putting the
opening brace at the end of the line, then use that style.

If you're a consultant, then adapt to the style of your client. That's
why they pay you the big bucks, after all. :-)

At every place I've worked in the past 15 years (since I lost curly
brace religion), the only thing I've wanted in this regard was an
auto-formatter to run everyone's code through (including mine), so that
it all looked the same, and no, I didn't care which convention it used,
so long as there was just one convention.

My 2¢ worth... let the games begin! :-)
 
John Salerno said:
I just realized that the C# convention for uses braces is to put the
opening brace immediately after the first line that defines the block
(except for class declarations) like this:

if (something) {
stuff
}

Which convention do you mean? The MS "Design Guidelines for Class
Library Developers" seems to use the bracing you've used below, in the
few examples I looked at just now.
Why is that? Do a lot of people do this? It seems like putting braces on
separate lines is much nicer and easier to read. In the book I'm
reading, it says it's important that the closing brace line up with the
first line (like above), I guess for readability, but how common is
this? It doesn't seem as readable as this:

if (something)
{
stuff
}

In my experience, this is far more common than the former (K&R)
bracing.
 
Jon said:
Which convention do you mean? The MS "Design Guidelines for Class
Library Developers" seems to use the bracing you've used below, in the
few examples I looked at just now.

Yeah, actually I thought the MS guidelines used my second example too,
and maybe I've seen some samples of it. But the book I'm reading
(Beginning C# Objects) says that the C# convention is to put the opening
brace on the same line as the code, and since I was unsure about this I
went to the FCL and checked a few of the samples there (I just went to
some random Forms classes, for example) and those use the first example.
 
John Salerno said:
Yeah, actually I thought the MS guidelines used my second example too,
and maybe I've seen some samples of it. But the book I'm reading
(Beginning C# Objects) says that the C# convention is to put the opening
brace on the same line as the code,

Without specifying *which* C# convention he means, the author's
statement is pretty much meaningless.
and since I was unsure about this I
went to the FCL and checked a few of the samples there (I just went to
some random Forms classes, for example) and those use the first example.

By the FCL, do you mean the Rotor implementation? If so, from what I've
seen there isn't a consistent convention (for pretty much anything!).
 
Bruce said:
Oh dear. The curly brace debate. There's a special punishment reserved
for people who resurrect this one. ;-)

big-endian braces are better than little endian, the nazi's says so.

(that should help the fire get started ;)
 
In the book I'm reading, it says it's important that the closing brace line up with the first line (like above), I guess for readability.

"Important"? Oh, absolutely. The author is absoluately correct: your
code will run 20% slower if you do not follow this convention. ;-)

If you go into Visual Studio's Tools -> Options menu, and choose C# ->
Formatting, you'll see that the first option is "Leave open braces on
same line as construct". It is, by default, turned off, leading to the
second kind of formatting: open braces on a separate line, directly
above closing braces.

If it's so "important" to leave the brace on the same line as the
construct, why does VS turn this off by default? The author's statement
is simply silly.

This is not to say that there is nothing to recommend the
brace-at-the-end convention. The common justification I've heard for it
is that it saves a line of screen real estate that you can use to show
more useful things, like more code. People who like the
open-brace-on-same-line convention complain that putting the brace on
its own line spreads the code out too much and leads to more scrolling
about in order to read things.

Defenders of the open-brace-above-closed-brace convention typically
complain that the other convention leads to densely packed and
difficult to read code.

The third convention that I posted was a later attempt to make both
camps happy. Apparently, it failed.

As I said, I don't care any more. If the IDE ships with "Leave open
braces on same line as construct" turned off, then I just leave it off.
I have better things to worry about.

The only people who piss me off are guys who insist on using the "other
convention" (whatever that is in their particular workplace) because
it's "far superior," and insist on going through their colleague's
code, reformatting all of the curly braces. I get my revenge by
assigning them a heavier workload: obviously they don't have enough to
do. :-)
 
John said:
I just realized that the C# convention for uses braces is to put the
opening brace immediately after the first line that defines the block
(except for class declarations) like this:

if (something) {
stuff
}

Why is that? Do a lot of people do this? It seems like putting braces on
separate lines is much nicer and easier to read. In the book I'm
reading, it says it's important that the closing brace line up with the
first line (like above), I guess for readability, but how common is
this? It doesn't seem as readable as this:

if (something)
{
stuff
}


I use the first style you describe. Personally I really don't care what
style any code I come across uses, I just CTRL+A, CTRL+F (select all,
format selection) the whole lot of it and go on with my way. If I'm
working on someone else's code, i adopt their style. If I write my own
code (the usual case) i use K&R style, where the opening curly is on the
same line as the condition statement, and the elses and catches are all
cuddled.

What really annoys me to no end is websites that give examples of code
like this: (this is the most annoying style i can imagine)

if (condition)
{
expression;
}
else if (condition)
{
if (condition)
{
if (condition)
{
try
{
expression
}
catch
{
}
}
}

i'm to the point now where just looking at working code gives me all the
tips i need (usually) to make my code work, so copying the example code
and putting it into a new project just so i can reformat it is a lot of
extra work that i don't want to bother with.

so, to answer your question, i use the style i example below, and i
recommend that every new programmer uses this style because its the way
i like it and if everyone did things my way there would be no war or
hunger or any other nasty stuff. It looks a little dense but I guess I
like it that way:

if (condition) {
// that's three spaces
} else if (condition) {
try {
Console.WriteLine(6/0); // div by 0
} catch (Exception) {
Console.WriteLine("you knew that would happen.");
}
}
 
Jon said:
Without specifying *which* C# convention he means, the author's
statement is pretty much meaningless.

What do you mean? The author(s) said that the C# convention is to put
the opening brace on the same line as the first line that starts the
code block.
By the FCL, do you mean the Rotor implementation? If so, from what I've
seen there isn't a consistent convention (for pretty much anything!).

Rotor?
 
Bruce said:
If it's so "important" to leave the brace on the same line as the
construct, why does VS turn this off by default? The author's statement
is simply silly.

No, the authors said it was "important" that the *closing* brace line up
with the line of code that started the block.
 
John said:
[snip]
Rotor?

http://www.microsoft.com/downloads/...FA-7462-47D0-8E56-8DD34C6292F0&displaylang=en

Microsoft's Shared Source C# compiler. MS says that it compiles out of
the box on FreeBSD, Windows XP, and MacOS X 10.2.

From what I've read in various places, it is a .NET 1.1 compatible
implementation, and that the garbage collector isn't as efficient as
what is available in .NET.

I've not dug very deeply into this, so I may be mistaken on the points
above.

If you're looking for a Free implementation of a C# compiler, have a
look at http://mono-project.com/ - its open sourced and runs very well
from what I understand.
 
I have a slightly different take on this, but only slightly. IMHO, when you
have several lines of code in a statement block, it isn't really important
whether or not the first brace goes on a separate line or not. The second
brace is more important, as it identifies the end of the block, and can get
lost in the code.

However, there are cases when I put BOTH braces on the same line. Example:

private string _Foo;
public string Foo
{
get { return _Foo; }
set { _Foo = value; }
}

public int Add(int a, int b) { return (a + b); }

In a case like this, it really makes sense to put them on the same line, for
the sake of your scroll hand. It doesn't make the code any more difficult to
read. In the first case, you're talking about a property, which you will
most often treat like a string (or whatever data type it works with). Since
it is simply getting and setting a private field, why add all the extra
lines?

The second example is similar to a macro. Again, the code is brief, and not
hard to follow, regardless of how many lines you use to write it.

Anyway, that's my 2 cents, and that's probably what it's worth! ;-)

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Big things are made up of
lots of little things.
 
Dan said:
Godwins Avenger. You loose. :-P~

Your terrorism doesn't frighten me!

Such gross simplifications and insults based on too rigid and
undemocratic rules is the *reason* that we big-endian bracers remain an
unfairly supressed minority :^p
 
Jon said:
But where does "the" come in here? There are lots of different C#
conventions available to pick and choose from - which does the author
mean?

Oh, I see what you mean. They meant that the C# convention for using
braces after methods, control statements, etc. I think that's specific.
 
John Salerno said:
Oh, I see what you mean. They meant that the C# convention for using
braces after methods, control statements, etc. I think that's specific.

No it's not. There *is* no one specific C# convention. The closest
thing to it is the MS guidelines for developers and the naming
guidelines in the ECMA spec, and those don't cover bracing as far as I
can see. The C# spec uses K&R bracing (in the bit I just looked at,
anyway) but I can't see it actually being recommended.

Clearly *someone* must be stating this as *a* convention, but the
question is who.
 
One "convention" is established by the way Visual C# defaults the formatting
of the code you type, and via the "edit/advanced/format selection" menu
item.
If you put the brace on the same line as preceding statement, the formatter
will move it to a line by itself, indented.

Now, it is true that you can change the default behavior in the
"Options/Text
Editor/C#/Formatting/New Lines" option, but taking the default to be the
"standard", one can only conclude that the brace on a separate line is
to be preferred. Anyway, that's the way I like it <G>.
 
Jon said:
No it's not. There *is* no one specific C# convention. The closest
thing to it is the MS guidelines for developers and the naming
guidelines in the ECMA spec, and those don't cover bracing as far as I
can see. The C# spec uses K&R bracing (in the bit I just looked at,
anyway) but I can't see it actually being recommended.

Clearly *someone* must be stating this as *a* convention, but the
question is who.

I see what you mean. The books seems to suggest that it is generally
accepted one way, but maybe they just kind of made that up. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top