views:

232

answers:

5

StyleCop just informed me that I shouldn't be prefixing member variables with m_. Is that the offical line on c# coding styles? I guess so as its from MS. Does anyone know anything about this??

By default, StyleCop disallows the use of underscores, m_, etc., to mark local class fields, in favor of the ‘this.’ prefix. The advantage of using ‘this.’ is that it applies equally to all element types including methods, properties, etc., and not just fields, making all calls to class members instantly recognizable, regardless of which editor is being used to view the code. Another advantage is that it creates a quick, recognizable differentiation between instance members and static members, which will not be prefixed.

+7  A: 

yup, hungarian notation went out with VB6.

Thankfully.

Jamiec
Amen. Good-bye m_
Joel Etherton
There is some confusion regarding the definition of Hungarian Notation. Such prefixes as the m_ prefix mentioned here, is not what constitutes Hungarian Notation; rather, its distinguishing feature is encoding of variable type in the name. At least, this is what the Wikipedia article explains: http://en.wikipedia.org/wiki/Hungarian_notation
harms
I agree with @harms. There are aspects of "apps Hungarian" that are still beneficial. Blind use of "m_" on every member field is fairly worthless, but using "us_" and "s_" to differentiate between unsafe and safe strings is a good example of a perfectly sensible use of apps Hungarian that is still valid and useful today.
Simon P Stevens
+3  A: 

It is all a matter of your style. You can go in and modify that rule to suit your coding style so you don't get annoyed by it. Personally I feel the m_ is a little verbose, "I already know it's a member!" I would just go with an underscore ex. _member.

Khalid Abuhakmeh
+2  A: 

Though I do like a bit of _ prefixing for private member fields. Simply to distinguish them by more than just case from corresponding getters/setters.

Wim Hollebrandse
A: 

Naming conventions are documented here:

http://msdn.microsoft.com/en-us/library/xzf533w0(VS.71).aspx

Hungarian notation is discouraged.

David Stratton
+3  A: 

Bear in mind that Style Cop is a tool for enforcing a particular (in this case Microsoft's internal) coding style. Its recommendations should not be accorded the same weight as those of FxCop, or any other recommendations from the (excellent) Framework Design Guidelines book. The person or organisation that controls any given source code should be the source of the style guidelines for that code - there is no global 'official' coding style any more than there is an 'official' email signature or 'official' coding font.

AakashM
The *official* coding style is *my* coding style. Of course!
Greg D