Visual Studio 2015 says "this" is redundant

Asked

Viewed 72 times

4

In the early days of my first object orientation classes I was taught to use the thiswhere 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 that too, but that’s not the question yet.

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

SS

I always prefer to wear the this, I think it makes the code more readable and I got used to it like this, but I had not attempted it until then to the fact of ambiguity, so I’m starting to change. For the question not to be subjective I ask:

Is there any reference/coding standard that always recommends the use of this where my teacher might have taken this?

  • 1

    It wouldn’t be because you can use the value algo directly, without using assign it to anything else?

  • 1

    @Murilo, the answers below already say a lot but, about C#, has languages that use this is mandatory, for example PHP. I just wanted to make that reservation!

2 answers

4


You need to see where you learned OOP. The fact is that almost everything on the Internet teaches wrong. Many books, mainly by national authors, but not only, teach very dirty. But I also don’t rule out misinterpreting what was said in the book.

The fact is that the this is not necessary if there is no ambiguity, that is, if that identifier exists only as an instance of the class and cannot be confused as a local variable (includes the parameter there), there is no need to use it. You already know that.

Some think it’s more readable. Okay, that’s a matter of opinion. You are using a configured IDE to inform you that this is unnecessary, his opinion is this, you can follow it or not, the language does not require that it be removed just because it is redundant.

You should be able to turn it off, but I don’t have VS 2015 here to see where it is, but it’s easy to find (however it’s not what was asked).

As it is opinion, use what you think best, there is nothing to indicate that you should do a way or not. Unless you are working on a team that requires a way, then do what was agreed. And do it consistently.

  • This point of the team I think weighs a lot. Here in the system is each place one way

2

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

No. At least nothing widely spread, even because it would not even make sense. The this is only necessary 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 several cases use the this.Propriedade it’s really unnecessary if you can just write Propriedade.

You also have a little taste for it (your teacher for example prefers to use the this, to mine, I’ve always said otherwise). The IDE indicates that you don’t use it, but it doesn’t force you to do anything, even this can be disabled.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.