Recently I posted a piece of my code here and got a comment (unrelated to the original question) that using this
for all member functions and attributes of a class is "not simply a matter of personal coding style, it is bad practice". Unfortunately, the person refused to elaborate and told me to look it up myself.
I have used Google a bunch (but it's really hard looking up anything with "this" as a keyword), and looked around here, but I only found some examples of when this
has to be used.
I know the situations where using this
is unavoidable (parameter / variable with the same name, template inheritance etc.), but in time I started using this
wherever possible because I can find my way around my code easier and faster. My reasons include:
f
should be a member function at all: if there is no this
in the code, it can be taken out of the classf
can be a const
function: if no this
on the left side, most probably can be made const
(not always but I find it useful when skimming)f
, or if it's a composite member function (a member method called with this
vs. an "outside" algorithm operating on the object without this)this
to find the problem, as other lines do not change the objectFrankly, the comment about this being "bad practice" rattled me a bit. But, one comment in itself does not mean much, so I would like to ask is there anything inherently bad with using this
consistently for all member functions and attributes? If so, what are the major drawbacks that put it past a (possibly clumsy, unpopular or not-widespread) personal style, and place it in the "bad practice" category?
You must use the this pointer when: Returning the current object. Setting up relations between objects (passing this into a constructor or setter) Checking for self reference: this !=
In C++ programming, this is a keyword that refers to the current instance of the class. There can be 3 main usage of this keyword in C++. It can be used to pass current object as a parameter to another method. It can be used to refer current class instance variable.
This answer is opinion-based (as noted by others).
I think it is a bad practice, because:
class <class-name> var;
, instead of <class-name> var;
and to ignoring the "rule of zero").this->
(because the names you use for classes, functions and variables form the mental model you use to understand the structure of the code).There is no technical reason why this cannot be used everywhere.
If you are only interested in technical reasons, the that is your answer. I would implore you however to consider non technical reasons. Opinions are formed for a reason, some of those reasons may be good ones. For example, I would suggest that using this everywhere would decrease the maintainability of your code, and you would be better served by rethinking your naming schemes.
Consider that normally this
is used where it is needed, and nowhere else. There are reasons why this
might be required, as you say, and when most programmers encounter a this
they will wonder to themselves, "That must be needed here for a non-obvious reason. I wonder what that reason is."
Consistency is an important attribute of maintainable code. One of the main problems with using this
everywhere is it's inconsistent -- with most other programmer's practices. Since most other programmers won't use this
everywhere, when you do use it everywhere it will make it more difficult for them to maintain your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With