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?

Filed under:

Comments

# Bertrand Le Roy said:

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.

Tuesday, July 15, 2008 5:48 PM
# Dave Ward said:

Personally, I'm sticking with "_".  I find that littering code with "this." is very distracting.

Tuesday, July 15, 2008 7:24 PM
# Ollie said:

I bet you like 'Regions' as well...

Friday, July 18, 2008 5:50 AM
# luisabreu said:

no comments on that :)

Friday, July 18, 2008 6:07 AM
# Dave Reed said:

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.

Friday, July 18, 2008 1:20 PM
# Giovanni Bassi said:

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.

Sunday, July 20, 2008 9:51 AM
# luisabreu said:

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 :)

Sunday, July 20, 2008 4:30 PM
# Paulo Morgado said:

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?

msmvps.com/.../naming-conventions-for-c.aspx

Sunday, July 20, 2008 6:59 PM
# luisabreu said:

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.

Monday, July 21, 2008 3:22 AM
# luisabreu said:

(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.

Monday, July 21, 2008 4:18 AM
# Paulo Morgado said:

How do you name private static fields?

Monday, July 21, 2008 2:47 PM
# Yordan Georgiev said:

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 \t\t get { return _\1 ; } \n  \t\t set { _\1 = value ; }  \n } //comm -- eof \1 property \n\n

find:^(.*) (.*)$

private string s\1 ; \n public string \1 { \n \t\t get { return s\1 ; } \n  \t\t set { s\1 = value ; }  \n } //comm -- eof \1 property \n\n

private \1 _\2 ; \npublic \1 \2 { \n \t\t get { return _\2 ; } \n  \t\t set { _\2 = value ; }  \n } //eof property \2 \n\n\n

//for the constructor

_\2= this.\2 ;

//for the passing to the constuctor

\1 _\2 ,

";

} //eof method SettersAndGetters

Monday, July 28, 2008 8:05 AM
# chrismo111 said:

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.)

Thursday, July 31, 2008 9:29 AM
# Nicholas de Lioncourt said:

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;

Wednesday, August 06, 2008 7:25 AM
# Granville Barnett said:

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.

Wednesday, August 06, 2008 9:58 AM

Leave a Comment

(required) 
(required) 
(optional)
(required)