views:

124

answers:

6

I have recently started using ReSharper which is a fantastic tool. Today I came across a naming rule for static fields, namely prefixing with an underscore ie.

private static string _myString;
  1. Is this really the standard way to name static variables? If so is it just personal preference and style, or does it have some sort of lower level impact? Eg Compilation JIT etc?
  2. Where does this style originate from? I have always associated it with C++, is that correct?
+6  A: 

It's actually the style for a private field, static or not. (At least in ReSharper)

Aren
Yes, specifically private. If you have the underscore on a protected or public field, it will not be CLS compliant.http://msdn.microsoft.com/en-us/library/k5645wwb(VS.80).aspx
Matt Greer
+2  A: 

According to StyleCop (and with the default settings), the correct way to name most fields (as specified below) is with a lowercase letter at the start.

SA1306: FieldNamesMustBeginWithLowerCaseLetter

... Field and variable names must begin with a lower-case letter, unless the field is public or internal, const, or non-private and readonly. In these cases, the field should begin with an upper-case letter.

See also SA1309: FieldNamesMustNotBeginWithUnderscore.

Mark Rushakoff
A: 

1 - There is not definitive standard rule for variable names. There are C# compiler requirements for what's allowed and what's not allowed (ie, can't start with a number), but programming language style rules are generally left up to the programmers / organizations. ReSharper has pre-defined style rules; however, they are merely set up as defaults in a convention over configuration approach and are modifiable.

2 - You can take a look at this wikipedia article to see the history behind Camel Casing.

JD Courtoy
A: 

They convention is whatever your company's coding standards says it is.

Muad'Dib
+3  A: 

The Microsoft guidelines are silent about private fields, they are only concerned with publicly visible members.

Common conventions are camelCase, _camelCase and even sometimes the hangover from C++/MFC m_camelCase.

If you use camelCase without a prefix, your property backing fields will differ from the property name only in case, which is not a problem in C#, but won't work in a case-insensitive language like VB.NET.

So many people, including myself, like to use an underscore prefix so that the same standards can be used in all languages. In my experience, underscore is much more common than m_.

Joe
Why do you think the Microsoft guidelines are only concerned with publicly visible members? http://msdn.microsoft.com/en-us/library/d53b55ey%28v=VS.71%29.aspx
dcp
@dcp I agree that the latest MSDN version you link isn't very explicit, but earlier versions were. For example VS2008 help says "The capitalization conventions described in this topic make it easy for developers to understand and work with a library." (which seems to exclude private members), and "Names cannot differ by case alone." (which is patently not true for private C# members).
Joe
@Joe - Actually, my point was that MSDN *does* actually give us a guideline on how to name static variables, and the guideline is to use PascalCase. Of course, as somebody else pointed out (and as I alluded to in my answer) StyleCop expects them to begin with a lowercase letter. :) Don't you just love consistency :)?
dcp
@dcp - but if the MSDN guideline is only intended to apply to publicly-visible members there is no inconsistency, just ambiguity in the MSDN docs.
Joe
A: 

According to MSDN, use Pascal Case for static fields. I always chuckle when MSDN and StyleCop contradict each other :).

So if you are following MSDN standards, the correct way is:

private static string MyString;
dcp