Coding Conventions?

M

Mike Labosh

I realize that you people have not seen much of me other than some framework
responses I have posted.

I am primarily a VB guy (yes, you can laugh) But I have lurked here for
several years, picking up little crumbs of C# wisdom.

Perhaps it's just my style, but what's this annoying behavior of the IDE and
how do I stop it:

C# seems to want me to write all my code like this:

public class Foo
{
public void DoThis()
{
// code here
}
}

What I REALLY want is to write my code like this:

public class Foo {
public void DoThis() {
//code here
}
}

Things like loops, IF and CASE blocks get really irritating when they use
the lengthy expanded format that the IDE seems to enforce, and I really want
the code window to behave like how I think. Is there some option or
template that I have to modify?

Also, while I'm at it, how do you all prefer to write? Expanded curly
braces? Brace on the sig line? whole programs in one big long line of
text? I guess here, I am just asking how the rest of you prefer to write
code.
 
M

Mike Labosh

Things like loops, IF and CASE blocks get really irritating when they use

Whoops, that's my VB drain bramage showing its head. I mean SWITCH blocks.
 
N

Nicholas Paldino [.NET/C# MVP]

Mike,

Go to "Tools | Options". On the dialog that pops up, go to "Text Editor
| C# | Formatting"

Under there, past "General", there is "Indentation", "New Lines",
"Spacing" and "Wrapping".

That should allow you to set everything that you want. For your
specific braces issue, you want "New Lines" and then you want to UNCHECK the
following:

Place open brace on new line for types
Place open brace on new line for methods
Place open brace on new line for anonymous methods
Place open brace on new line for control blocks

As for my style, I prefer to code like this:

public
class
MyClass
{
public
MyClass
(
)
{
}
}

But that's just me.

Hope this helps.
 
D

Daniel O'Connell [C# MVP]

Also, while I'm at it, how do you all prefer to write? Expanded curly
braces? Brace on the sig line? whole programs in one big long line of
text? I guess here, I am just asking how the rest of you prefer to write
code.

For what its worth, I do prefer the default formatting. Braces on seperate
lines, class and method definitions on one line(with the exception of the
where operator or inheritets\implements lists of more than about 3 items,)
and statements taking one line each. I was quite pleased when I launced
VS.NET the first time and found the C# conventions to be almost exactly to
my liking.
 
K

Kevin Frey

Consistency is more important that the style you actually use, especially if
you code as part of a team. You want all code to look like it could have
been written by anyone within the team, and that any team member making
changes doesn't have to "think" about formatting style, since after a few
months of applying a consistent coding style it becomes second nature. If
you're a one-person developer you don't have these issues obviously, but
consistency is still important.

At my previous job I did not put opening curly braces on a line by
themselves - UNLESS they were for a class declaration or the opening body of
a function (classic C K&R style). My opinion back then was that if K&R
invented the language, so am I to argue about their formatting style?

But C# is not truly C, so I take some liberties with it. Really short
methods you might find writing on a line by themselves, like C++ inline
methods. For example, in C#:

public abstract string MyProperty( ) { get; }

Padding this out to multiple lines serves no useful purpose and in my
opinion actually reduces the clarity of the code, since there is no
implementation anyway.

At my new job braces go on a line by themselves, but we still have enough
flexibility to allow for the above convention where the construct is short
enough to warrant it (we effectively apply our C++ coding conventions -
where they make sense - to C#).
 

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

Top