Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using "this" for all member functions and attributes bad practice? [closed]

Tags:

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:

  • quick check if the function f should be a member function at all: if there is no this in the code, it can be taken out of the class
  • quick check if f 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)
  • quick check if the object is "changing" itself in a "predefined" way in 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)
  • debugging; i.e. if a member attribute gets assigned the wrong value at any point, I have to concentrate on the lines containing this to find the problem, as other lines do not change the object

Frankly, 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?

like image 296
penelope Avatar asked Oct 07 '14 12:10

penelope


People also ask

Should you use this in C++?

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 !=

Where should you use this in C++?

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.


2 Answers

This answer is opinion-based (as noted by others).

I think it is a bad practice, because:

  • it makes code larger, unnecessarily (the easiest code to maintain, is the one you don't write because you don't have to).
  • it is unexpected (while you may expect it, others won't - so you get an increased WTF/SLOC ratio in your code)
  • it increases maintenance costs.
  • it requires extra effort for keeping code consistent (with little or no extra benefits).
  • while it looks consistent, it is redundant (similar to declaring all object instances with the syntax class <class-name> var;, instead of <class-name> var; and to ignoring the "rule of zero").
  • it creates coding habits that will not fit in most development teams and coding standards.
  • it is much better practice to rename variables and functions to avoid name colisions than it is to use this-> (because the names you use for classes, functions and variables form the mental model you use to understand the structure of the code).
  • after working for a few months in a code base that doesn't follow / accept this practice, you may find your own code difficult to read / maintain (in other words, in a year or so it may become pure cruft).
like image 137
utnapistim Avatar answered Oct 28 '22 01:10

utnapistim


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.

like image 42
John Dibling Avatar answered Oct 28 '22 01:10

John Dibling