Visual Studio 2015 says "this" is redundant

3

In the early days of my first object-oriented classes I was taught to use this whenever it is a variable or class property.

Today I understand when there is a real need to use, for example when a parameter is received in the method that has the same name as a class variable:

private string algo;
public void FazerAlgo(string algo) 
{
   this.algo = algo;
}

Here answers this too, but that's not the question yet.

I noticed that Visual 2015 is now saying that using this in situations other than the above is redundant. This did not happen in other versions as far as I remember.

As I see it, I always prefer to use this , I think it gets more readable and I got used to it, but I had not tried until then because of the ambiguity, so I'm starting to change. For the question not to be subjective I ask:

Is there any reference / encoding standard that always recommends using this from where my teacher might have taken it?

    
asked by anonymous 09.11.2016 / 14:50

2 answers

4

You need to see where you learned OOP. The fact is that almost everything you have on the internet teaches you wrong. Many books, especially of national authors, but not only, teach very well. But I also do not rule out having misinterpreted what was said in the book.

The fact is that this is not necessary if there is no ambiguity, ie if that identifier only exists as an instance of the class and can not be confused as a local variable (it includes the parameter there), it does not have to use it. That already knows.

Some find it more readable. Okay, this is a matter of opinion. You are using an IDE configured to report that this is unnecessary, its opinion is this, you can follow it or not, language does not require it to be removed just because it is redundant. It should be able to shut down, but I do not have VS 2015 here to see where it is, but it's easy to find (which is not the way it was asked).

As you are told, use whatever you think is best, there is nothing to indicate that you should do it one way or another. Unless you're working on a team that needs a way, then do what you've agreed.

    
09.11.2016 / 15:00
2
  

Is there any reference / encoding standard that always recommends using thisde where my teacher might have taken it?

No. At least nothing widespread, even because it would not even make sense. this is only required when there is ambiguity.

Visual Studio has adopted a policy to help make code as simplified as possible, indicating the removal of most (or all) redundancies. In many cases using this.Propriedade is really unnecessary if you can only write Propriedade .

It also has a bit of taste in it (your teacher for example prefers to use this , mine, always said otherwise). IDE indicates that you do not use, but it does not force you to anything, even this can be disabled.

    
09.11.2016 / 15:00