views:

214

answers:

8

Recently I've taken note of class member variables in C# moving to the this.foobarbaz for every reference for class member variables rather than the previously acceptable m_ or just _. How many of you seasoned programmers are coding to this new acceptable standard, if so why, if not why? I ask because it changes our coding standards at work, it may be a tough sell for some. So then I ask: what would be a static class var? s_ or that.foobarbaz Okay okay just kidding. Thanks - Old man.

+1  A: 

The keyword this is simply used to specify that we are referencing this very instance. One is not obliged to always specify this, except if one wants to make it obvious that the variable is one of the instance, and not a parameter or something alike.

Most of the time, I see the underscore character to designate field members, and rarely m_.

Will Marcouiller
Not "this very class" but rather "this very object". this. can't be used to refer to class (static) members.
Michael Shimmins
@Will, you should change the wording to say "this very instance", not "this very class". Class members are static.
code4life
@code4life and @Micheal Shimmins: Thanks for your comments. I changed "this very class" for "this very instance". =)
Will Marcouiller
+1  A: 

It fits better with the code from the framework, General Guidelines in the Capitalization Conventions section. I'm quite sure the m_ and _ is from before .NET 1.0, back in Visual Studio 6.

Choosing identifiers that conform to these guidelines improves the usability of your library and encourages users to trust that your library will not require learning a new set of conventions.

Since local variables are also camel cased, the 'this' differentiate the variables that are members of the class, most syntax tools (StyleCop, FXCop, CodeIt.Right) will flag class members missing this , or containing _ and m_ as warnings.

GenEric35
Thanks - good info. - Old Man
ddm
+3  A: 

That sort of syntax is necessary when the class member variables have the same name as the local variables passed in to a function or constructor.

public class Foo 
{
    private string name;

    public Foo(string name) 
    {
        this.name = name;
    }
}

personally i find it a PITA when devs code that way, and prefer to have an underscore prefixed to the member variable just like old times.

slugster
+1  A: 

Because this is only used when necessary. Adding a prefix to an identifier forces you to use it everywhere, which is very inconvenient.

_ or m_ prefixes are a C++ leftover that's not appropriate for C#.

Joren
Yes - C++ (and Java) that's where I came from.
ddm
Although inappropriate, I see them everywhere...!
code4life
A: 

First, read "Framework Design Guidelines" (Cwalina/Abrams). Then, download and use FxCop. It will teach you so much and (hopefully) change your outlook on coding, for the good.

Next, aside from best-practices being "the time-tested, best way to approach a problem", sometimes, if there are several "right" answers, you pick one for consistency sake. With naming standards, it's "slightly" more intuitive to get away from hungarian notation, SCREAMING_CAPS, and abbreviations for example - to instead use PascalCase and camelCase. This convention is now the accepted standard. It's not wrong to use the other styles, but there certainly is no advantage to it.

Lastly, from your post, the "this" keyboard is not a prefix. It is a language convention (in VB.NET, it's "Me"). When you use this.variableName, you are simply being very specific is saying "I am referencing a class-level member, of the current instance. It's not an arbitrary prefix. Hope that helps.

Robert Seder
Thanks - BTW - I've adopted the newer standard as I find it more obvious. I was only referencing C#. I don't know VB.
ddm
A: 

My own preference is to only adorn interfaces with the 'I' prefix. Everything else (including member, static and control related variables) get pascal casing with no prefixs (especially hungarian or underscores).

lzcd
A: 

Often, when I see lots of this.Member in .NET code, it's not so much a style choice as an indicator that the original coder was trying to bring up IntelliSense; typing this. is one way to do this, but unfortunately it leaves a trail behind.

Learning the keyboard shortcut (Ctrl-J) gives you a bigger list to sift through, but leaves cleaner code.

millerjs
Ctrl-Space should also activate the intellisense, just FYI.
Funka
A: 

I am a fan of using "this." The single biggest reason is that this tells the compiler what you intended, and it can give you warnings / errors when something is wrong. You can't get this from m_.

Almost everyone I talk to about "this." is resistent. More typing, don't see the value, etc, change is scarey, etc, etc. Trying to convince someone (never mind a team) to do this is painful for everyone. Either they see the value, or they don't.

For statics, internal to the class I don't do anything, external to the class I use the class name.

Chris
I fail to see your point about the compiler and the use of *this* to reference member variables - what extra protection do you get that correct usage of a prefix doesn't give?. Having an underscore prefix is a long established "standard" that not only groups the member vars together at the top of intellisense lists, but i can also **visually** tell straight away that the variable being referenced is a class level (private) member variable. Having said that, it is all open to individual or team coding preferences - different strokes for different folks.
slugster
The compiler has no understanding to m_ or _ or any other prefix you give it. The compiler does know that when you use this. you are refering to a member variable of the class, instead of a variable local to your method.You can only tell, if everyone carefully maintains the practice through changes. Suppose for example a person moves a variable from method level to class level, but was lazy and didn't rename it? Or suppose the opposite is true? Using "this." is only valid at the class level.After using it for awhile, you will realize when it has saved you pain. Then it all makes sense.
Chris