Recommendations on using the "this" keywords?

  • Thread starter Thread starter Anders Borum
  • Start date Start date
A

Anders Borum

Hello!

I'm wondering what the recommendations are for using the "this" keyword?

The problem is that I really like to use the "this" keyword, but it tends to
make the code more verbose (which some don't like). Your oppinion please?
...

internal void PageDeleted(Page page)
{
if (this.pages == null) { return; }
this.pages.InnerList.Remove(page.PageID);
}

Thanks in advance - it's greatly appreciated.
 
I use it only when there is a possible conflict, e.g.

public void DoSomethingToPage(Page page)
{
//blah blah blah
this.page = page;
}

The form designers probably write this.everything to avoid this sort of
thing without having to parse the code in order to do it.


--
Pete
====
Audio compression components, DIB graphics controls, FastStrings
http://www.droopyeyes.com

Read or write articles on just about anything
http://www.HowToDoThings.com
 
Hello!

I'm wondering what the recommendations are for using the "this" keyword?

The problem is that I really like to use the "this" keyword, but it tends to
make the code more verbose (which some don't like). Your oppinion please?
..

internal void PageDeleted(Page page)
{
if (this.pages == null) { return; }
this.pages.InnerList.Remove(page.PageID);
}

Thanks in advance - it's greatly appreciated.
I used it alot in VS 2002/2003 as it brought up the intellisense however
as of vs 2005 the intellisense works as you type (which is nice) so I
would only use it as a conflict resolver.
 
It's probably more important to use it consistently, than whether or not you
use it; but personally, I have come up with the following guidelines:

1) I use "this." to refer to fields or properties of the current object,
reasoning that it avoids any confusion about whether you are referring to a
local variable or to a member. I feel that being specific makes the code
more immediately readable to other (or to me, six months later).

2) I do NOT use "this." to call methods of the current object, because if I
am calling a method of some other object I'll have a reference (or a class
name, in the case of a static method) to prefix that call with. There is no
such thing in C# as a global function that's not associated with an object,
so there's nothing to confuse an internal method call with if you don't use
"this." Therefore I avoide "this." for method calls as all it seems to
accomplish is to make code more verbose.

--Bob
 
Back with MFC I used to put m_ in front of the name of all my private class
fields. I went on doing this when I changed to .NET, but after a while I
found it didn't fit so well in .NET, specialy when dealing with inner
classes (since they have access to private members of the class they are
in). So I changed to using the this keyword instead of a m_ name prefix with
private fields. This is also more consistant with the code generated by VS.

Etienne Boucher
 
Hello!

I would like to thank everybody for their suggestions and recommendations. I
am currently writing a set of guidelines (naming etc.) for our developers,
and want to make sure they are as objective and logic as possible.

Thanks again :-)
 
Anders Borum said:
Hello!

I'm wondering what the recommendations are for using the "this" keyword?

The problem is that I really like to use the "this" keyword, but it tends
to
make the code more verbose (which some don't like). Your oppinion please?
..

internal void PageDeleted(Page page)
{
if (this.pages == null) { return; }
this.pages.InnerList.Remove(page.PageID);
}

I always use it because I like to be consistent in the format of my code and
it does immediately identify the scope. Else people end up doing that ugly
m_ stuff. So I prefer always instead of "when needed".

SP
 
Back
Top