C# style conventions: more religious wars coming!

Yes, it”s one of those posts where you either agree or disagree completely :,,)

Anyway, after having a 15 minutes discussion with my friend Paulo (which, btw and as usual, disagrees with me) yesterday at 1AM, I”ve decided to write this post. The problem: MS recommends that variable names shouldn”t have any sort of prefix like the _ or m_.

And what do I think? Well, I don”t like! And I will only use that it my boss say something like: “hey, you must use that or we stop paying your paycheck”. Ok, why don”t I like that convention? Simply because I:

  • want to know if the current reference is pointing to a field or to a variable that was passed to a method;
  • don”t like using this to disambiguate between field names and parameter names;

These 2 reasons are more than enough for using the _ prefix for field names. What do you think?


~ by Luis Abreu on July 15, 2008.

16 Responses to “C# style conventions: more religious wars coming!”

  1. You”re right that this rule is very debatable and as a matter of facts, our team (ASP.NET) always prefixes private fields (which pretty much maps to fields 1:1 nowadays) with an underscore.
    Now, the hungarian notation or m_, no way.

  2. Personally, I”m sticking with “_”. I find that littering code with “this.” is very distracting.

  3. I bet you like ”Regions” as well…

  4. no comments on that 🙂

  5. Another reason I dislike NOT having “_” is that a property and its backing field may only differ by the capitalization of the first letter (e.g. Mode and mode). Then, whether or not you use “this”, its quite easy for statement completion to give you the wrong reference without you immediately realizing it, and it gets quite annoying dealing with that.

  6. Just as Le Roy said, I don”t like hungarian. I actually used it a lot before, but I have changed my mind, because the code gets less readable (in my opinion).
    Regarding “_”, I do use it, but I have also been using “this” to prefix the field names, just because it gets clearer. The same thing to point to class names outside the assembly: I use the “usings” directive quite carefully, because I want to know where that class comes from, most of the time. Unless the namespace is just really big, then an alias usually solves the problem.
    Now, with this post from StyleCop you pointed, I am considering dropping the “_”, because it makes sense if I am already using “this”, anyway. The problem is that I might forget to use “this” and end up with the wrong variable… but I think that would be very unlikely.
    And I don”t particularly like the “m_”. Its just ugly and makes the code less readable, just like hungarian (again, in my opinion). Anything that gets it more like human words and less like machine words is worth considering.

  7. I don”t use hungarian either (altough I think that the definition of hungarian notation would be another interesting “religious war” post), but I prefer to use the _ convention to the this convention. As I said, it”s all about personal style 🙂

  8. For those who need _ to know that it”s a field, how do you know when you are accessing a local varible or a parameter?

    I really can”t understand the use of “m_” in .NET.

    If you need to prefix stuff, why nhot go all the way?

  9. that question answers itselt. the _ is only applied to fields. parameters and local variables don”t have the prefix.

    btw, there is a place where some sort “hungarian” pays off: with controls on windows forms.

  10. (just noticed that the previous comment got published before I ended it)

    Regarding the parameter vs local variable thing, I haven”t found it important to make that distinction in the code I write. It”s not as important as seeing if you”re working with a field. Since I don”t like using the unecessary this prefix (btw, in these scenarios isn”t the “this.” really a “special” prefix?), I will keep using the _ to indicate a field.

    Regarding the m_ prefix, I believe that it exists for historical reasons.

    At the end of the day, it”s a matter of taste, so everyone is entitled to having his own style.

  11. How do you name private static fields?

  12. public static void SettersAndGetters ()
    string copyPaste = @”
    how-to generate those in textpad F8
    how-to Generate member accessors , properties for C# asp.net with textpad
    how-to setters and getters
    find: ^col(.*)$
    replace: private string _1 ; n public string 1 { n tt get { return _1 ; } n tt set { _1 = value ; } n } //comm — eof 1 property nn
    find:^(.*) (.*)$
    private string s1 ; n public string 1 { n tt get { return s1 ; } n tt set { s1 = value ; } n } //comm — eof 1 property nn

    private 1 _2 ; npublic 1 2 { n tt get { return _2 ; } n tt set { _2 = value ; } n } //eof property 2 nnn

    //for the constructor
    _2= this.2 ;

    //for the passing to the constuctor
    1 _2 ,

    } //eof method SettersAndGetters

  13. I think this is one of those issues that programmers get all bent out of shape over and waste time bitching about that ultimately do not matter. What matters is consistency within your team. Microsoft is just one vendor among many; why do we care what Microsoft recommends? Microsoft is neither an industry association nor a standards body? Would anyone care if General Motors declared that all right hand turns in GM cars must be preceded by flicking the right hand blinker at least 23.416 seconds before commencing a right hand turn? No. Some might care if the American Automobile Association or the National Highway Traffic Safety Administration (NHTSA) said the same thing and others would not; some states might issue their own right hand turn/blinker protocol and perhaps some counties too. What matters is that we understand what others drivers are doing when they implement their RightTurn methods (and does it really matter if you are driving alone in Death Valley at 4Am anyway?? It seems to me that it only matters when you are driving on the road with others – which happens to be the case with the majority of traffic – just not all.)

  14. I employ not only “this” but a doubled underscore (__) for private variables as well; it is my coding style. Style grammarians would be very displeased to learn that I use this style notwithstanding the project, team or client… or Microsoft.

    this.__variable1 = 1;
    this.__variable2 = 341;

  15. I”ll just stick with m_ and so on.

    Using “this” all the time to disambiguate is just code bloat in my opinion, furthermore it is harder to distguish between a local and a field when programming.

  16. I use “m_” because of IDesign name convention.
    You shouldn”t use only “_” because it may be used by system variables.
    I hate “this.” it”s redundant and removed by Resharper 🙂
    Actually I don”t like “m_” but I have no other choice.
    Best way add some VB syntax: use “.” instead “this.” with rule that “.” must! be used with every field in class. Without this rule it”s the same “this.” stuff.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: