I'm a highly opinionated developer. I've been doing it a little while, and worked with a few other developers, and this is a pet peeve of mine. The this
keyword, at least in C#, is oft misused and abused. From henceforth, the context of this post is C# development.
This post seems to have caused a bit of controversy. More to my point, even in base Visual Studio, there are warnings to remove this
from your code:
I've had managers want developers to use this
to signify calling a base class's method, which is incorrect. You should be using base
.
public class MyClass : YourClass {
public void MyMethod() {
if(!base.YourMethod()) return;
}
}
I've had coworkers who would put this
before every method call, because...? When calling your own method, in your own class, that inherits/implements no other classes, why on earth would you decorate your own method call with this
?
The only time I have seen prepending this
to anything as kind of acceptable, is when you have parameters in a method that are the same as fields/properties on your class, and you need to access those fields/properties inside said method. I am still not a fan of this, but the closest I will get to accepting it is in a constructor where you are passing values that will be stored as private fields.
public class SomeClass {
int maxAmount;
SomeClass(int maxAmount) => this.maxAmount = maxAmount;
ValidateMe(int someValue) => someValue <= maxAmount;
}
But even in this case, you should really be using Pascal casing as per Microsoft's Capitalization Conventions. I know, capitalization conventions change over time, and I could have sworn there was a time when camel casing was the recommended approach for fields (specifically private fields), but times they are a changing, and cleaner code will hopefully prevail, especially cleaner code without the misuse/abuse of this
.
Maybe I'm the only one this matters for, or maybe I'm just having a bad day and came across this
too many times this morning. I do hope you agree though.