blah blah blah is here! blah blah » Close

up0down
link

Hi All
Just quict survey to help me in my doubts:
Do you consider to use this keyword everywhere where do you refer to member of an current instance within you are coding?
Like:
this.arrayList.Add(new object())
this.InvokeMethod(this.arrayList)
Thanks

last answered 2 years ago

1 answers

up0down
link

Just coders preference. I like to use it occassionally when there's a lot going on in moy code, just helps define what's what. Some people use it, although I still know a lot that don't. At university I generally taught to use "this".

up0down
link

I allwayas plead for to use it, but now I have seen, than some code editors tools don't use it. So I've fallen into doubts. Thanks for your opinion.

up0down
link

this is always good practice IMO. it makes code easier to read (knowing local scope right away). I know I dont always use it (ctrl+space lazyness), especially if the source isnt going to be looked at by very many people.

up0down
link

I generally don't use it unless I need to make it clear what object I'm using. As long as I can still tell that <i>this</i> is the object I'm working on, I'll just be lazy and leave it out (the compiler wasn't built to do that on its own for nothing ;) ).

up0down
link

My preference to use this is only when typing (so that I can select the variable from the list quickly rather than type all of it myself) and then double click to "this" with mouse and erase it to reduce amount of letters on the code.
I name all instance vars starting with _. For example, if I want a variable in the class "count", I name it "_count". All other variables (parameters and local variables) don't start with "_". That way, I know which variable is local and which is not at a glance.
(Kind of a cognitive psychological reason: _ can be identified through perception quickly, most of the time (saves high cognitive functions for other tasks, helps save attentional resources))
You can also find that Martin Fowler also prefers to start instance vars with _ (To tell you the truth, I saw from him and also someone else using it and adopted that convention). You may want to look at his book "Refactoring: Improving the design of existing code"

up0down
link

another advantage of starting all instance vars with _ is that you can immediately filter all but the instance variables by typing this._

up0down
link

Well it is not a question of good or bad practice...
Let me put it in this way :
It is a good practice to name a temporary variable like this :
int thisisatemporaryvariable;
it is good a practice, because you immediatly can tell that the above is a tmp variable.
But it can be done like this :
int tmp;
simple, clean and time saving.
Now, to sum it all :
Use "this" where your programming logic is too complex and you have a lot of things going on, but if this is the case, then you should reconsider the way you solve the problem ;)
And do not use "this" where ... the logic is trivial.

This post was imported from csharpfriends, if you have a similiar question please ask it again.

All previous members have been migrated, hope you enjoy the new platform!

Feedback