473,804 Members | 3,649 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# convention

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
}
Nov 17 '05 #1
19 2279
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! :-)

Nov 17 '05 #2
John Salerno <jo******@NOSPA Mgmail.com> wrote:
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 Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3
Bruce Wood wrote:
Oh dear. The curly brace debate. There's a special punishment reserved
for people who resurrect this one. ;-) My 2¢ worth... let the games begin! :-)


Oh no, what did I do!? :)
Nov 17 '05 #4
Jon Skeet [C# MVP] wrote:
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.
Nov 17 '05 #5
John Salerno <jo******@NOSPA Mgmail.com> wrote:
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,


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!).

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6


Bruce Wood wrote:
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 ;)

--
Helge Jensen
mailto:he****** ****@slog.dk
sip:he********* *@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #7
> 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. :-)

Nov 17 '05 #8
John Salerno wrote:
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.WriteLi ne(6/0); // div by 0
} catch (Exception) {
Console.WriteLi ne("you knew that would happen.");
}
}
Nov 17 '05 #9
John Salerno wrote:
[snip]
Rotor?


http://www.microsoft.com/downloads/d...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.
Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
12042
by: Generic Usenet Account | last post by:
A lot has been said in this newsgroup regarding the "evil" set/get accessor methods. Arthur Riel, (of Vanguard Training), in his class, "Heuristis for O-O Analysis & Design", says that there is almost never an excuse for accessor methods. Personally, I do not go that far. I do feel that they serve a useful purpose (albeit in a limited manner). Personally I prefer dropping the "set" and "get" prefixes from the method names altogether. ...
1
2328
by: Asapi | last post by:
1. Are linkage convention and calling convention referring to the same thing? 2. Does calling convention differ between languages C and C++? 3. How does calling convention differ between static and non-static class member function? 4. Could it be possible to specify different calling convention for each "global" or each class member function? Even in a single file scope?
0
1748
by: Carl Colijn | last post by:
Hi all, Disclaimer: before I might trigger your "let's start a holy war!" button, I'd like to say I'm not intended to; I just post this message to get some input and not to promote "Yet Another Naming Convention". It's a bit of a long post, but I've spent the past few days on perfecting this, finally came to the conclusion that maybe I went a bit overboard with it, almost willing to just say "the @#$ with it" but I didn't want to let it...
4
1499
by: **Developer** | last post by:
I looked at MSDN and a book and both described the naming convention for parameters should be camel style. The book used camel, except for Set (ByVal Value as ...) There it always used an uppercase V What is the correct convention for Set?
12
2031
by: Belebele | last post by:
Suppose that a class A depends on class B because an object of class B is passed into A's constructor. See below: class B { ... }; class A { public: A(B const& b); ... };
114
7889
by: Jonathan Wood | last post by:
I was just wondering what naming convention most of you use for class variables. Underscore, "m_" prefix, camel case, capitalized, etc? Has one style emerged as the most popular? Thanks for any comments. --
35
12201
by: Smithers | last post by:
Is it common practise to begin the name of form classes with "frm" (e.g., frmOneForm, frmAnotherForm). Or is that generally considered an outdated convention? If not "frm" what is a common or recommended practise? Thanks.
50
2547
by: John Salerno | last post by:
I know it's popular and very handy, but I'm curious if there are purists out there who think that using something like: for x in range(10): #do something 10 times is unPythonic. The reason I ask is because the structure of the for loop seems to be for iterating through a sequence. It seems somewhat artificial to use the for loop to do something a certain number of times, like above.
6
11582
by: Ole Nielsby | last post by:
VC has a __cdecl specifier which allows functions and methods to be called with varying parameter count. (I understand this is the default for functions in general but in VC, instances use another convention unless they have an ellipsis argument.) I can force GCC and other compilers to use such a convention by declaring all methods with an ellipsis, but I'd rather not clutter my method definitions with these.
0
9705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10310
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9138
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7613
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5515
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4291
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2983
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.