Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any reasons not to use "this" ("Self", "Me", ...)?

I read this answer and its comments and I'm curious: Are there any reasons for not using this / Self / Me ?

BTW: I'm sorry if this has been asked before, it seems that it is impossible to search for the word this on SO.

like image 470
Daniel Rikowski Avatar asked Dec 02 '08 08:12

Daniel Rikowski


2 Answers

Warning: Purely subjective answer below.

I think the best "reason" for not using this/self/me is brevity. If it's already a member variable/function then why redundantly add the prefix?

Personally I avoid the use of this/self/me unless it's necessary to disambiguate a particular expression for the compiler. Many people disagree with this but I haven't ever had it be a real sticking point in any group I've worked for.

like image 160
JaredPar Avatar answered Sep 19 '22 01:09

JaredPar


I think most of the common scenarios have been covered in the two posts already cited; mainly brevity and redundancy vs clarity - a minor addition: in C#, it is required to use "this" in order to access an "extension method" for the current type - i.e.

this.Foo();

where Foo() is declared externally as:

public static void Foo(this SomeType obj) {...}
like image 31
Marc Gravell Avatar answered Sep 21 '22 01:09

Marc Gravell