5

Coming from eclipse I've developed my own coding standards which I got used to. In Visual C# 2010 however, it appears that some coding standards that MS recommends are enforced in the default configuration.

E.g.: I'm used to write conditional statements like this:

if (somecondition) {
    return true;
} else {
    return false;
}

But the braces are forced to a newline in visual c#. Is it recommended to use that standard ?

1
  • Are you interesting in code formatting/layout, or in naming etc.? This question and its answers seems to be mixing them up.
    – Richard
    Commented Dec 19, 2010 at 11:11

6 Answers 6

9

Languages all have their own odd coding styles - I'd say it's easiest just to follow them and don't think too much about it. If everyone just follows the most common standard of the language, then no time is wasted on fighting about personal ideas of good style.

In the case of C#, the programs StyleCop and FxCop check's your C# code and .NET binaries against some coding style guidelines defined by Microsoft.

2
  • 1
    -1: Languages/platforms have naming guidelines and common coding patterns, but they often do not have formatting guidelines (.NET certainly does not have a formatting guideline: for a start MS uses more than one internally).
    – Richard
    Commented Dec 19, 2010 at 11:06
  • @Richard: Yes, C# does have formatting guidelines: msdn.microsoft.com/en-us/library/ff926074.aspx However, next-line braces are not explicitly mentioned (but similar issues like 'four-character indents' and 'tabs saved as spaces' are) Commented Jul 5, 2015 at 1:09
18

Brace style in C# is not enforced by Visual Studio. In fact you can customize it to format source matching your style.

Look under Tools > Options, Text Editor > C# > Formatting > New Lines there you'll find lots of options to control the behavior.

Obviously, you may want to consider having your coding style adapt to the style of other team members but you should not need to change the style to match the defaults in the IDE.

8

The old coding standards seem like they are from Java environment. I think it's better to stick to coding standards that the new language uses than transfers standards from language to language.

You will save yourself a trouble when working with someone else or taking/using existing code.

2
  • 1
    It also gives your brain a quicker way to recognize what language you are in, and aids context switching between languages.
    – CaffGeek
    Commented Jun 14, 2012 at 16:41
  • Right on: Using the defaults for the IDE means that integration with your peers is going to go MUCH more smoothly. It also means that on a rebuild of your dev environment, you don't have to think about this stuff - just use the defaults! Commented Jun 14, 2012 at 17:18
6

I resist the urge to fun of you for not simplifying that statement to return somecondition, since that's clearly not what you're asking about.

This is one of the classic style holy wars. Personally, I prefer the C# convention because visual alignment improves readability (for me), but in practice, I adopt the convention that's most common in the development community in question.

That is to say, in Java, I typically start the brace on the conditional line. In C#, I start a new line. In ruby or python there are no braces needed. In Lisp, I typically follow elisp conventions.

My opinion is simple: following a convention is more important than which convention you use.

3
  • 1
    There is no "C# [formatting] convention": MS themselves use more than one. But +1 for "following the existing convention", or as I would say: "You MUST be consistent".
    – Richard
    Commented Dec 19, 2010 at 11:09
  • Well, the curly braces convention is fairly pervasive in C#, thanks to default settings in Visual Studio. The convention is not a strict rule, but a matter of community habit. Certainly other style guidelines promoted by MS are inconsistently applied even in their own codebase.
    – JasonTrue
    Commented Dec 19, 2010 at 18:27
  • It is rather unusual to come across any C# book, or tutorial, or even code samples using "Egyptian brackets" (Java-style). Commented Aug 8, 2011 at 11:56
3

When in Rome...or Redmond. I switch back and forth between C# and Java in my job, and I use the convention of each language because, as mentioned earlier, it avoids issues when looking at other's code, or for others to read mine.

0

I don't think it really matters as long as your code is clear and consistent. I think the latter is really the driving force behind your concern. I am a huge ReSharper fan and I work with code from many different developers; however, I often can't stand the way they format their code. Using my ReSharper scheme I can format it the way I like and when I'm done I can put it back the way it was before. I don't really think it matters so much in terms of formatting because everyone has their own preference. I will say that I think naming conventions should be adhered to more strictly than formatting style.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.