25

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?

Community
  • 1
  • 1
penelope
  • 8,251
  • 8
  • 45
  • 87
  • 17
    This is likely to be based on opinions. I don't think there are any hard facts either way. Personally, I find use of `this` where it is not required to be distracting/annoying noise. – juanchopanza Oct 07 '14 at 12:28
  • 1
    I think, it is already explained http://stackoverflow.com/questions/2337540/when-should-you-use-the-this-keyword-in-c Am i wrong? – carlos.baez Oct 07 '14 at 12:29
  • @carlos.baez Thank you for noticing this answer. Unfortunately, while this (and the two other questions I already linked), explain when `this` _should_ be used, none of those questions actually answer what I am asking here: is the consistent use of `this` for all attributes and member functions inherently bad practice. – penelope Oct 07 '14 at 12:33
  • In my opinion, it is very personal. I like using "this" because i can different what variables are parameters, local variables or global variables... – carlos.baez Oct 07 '14 at 12:34
  • The answer is implicit in the duplicate: it is a subjective question. Or rather, answers will be subjective. – juanchopanza Oct 07 '14 at 12:35
  • 2
    @juanchopanza Hey, I saw the post you linked as duplicate (as well as two other similar ones I linked in my Q). However, I _am_ actually looking for _hard facts_: the comment I got was phrased in such a way that it said it is _obviously_ bad practice for reasons that should be clear to everybody, and which I should be able to find myself. So while I agree that everybody has their personal preference, what I want to know is if there is any **hard facts** that prove this is _bad coding practice_ and not _unpopular coding style_. Would it help if I tried re-phrasing my question to this end? – penelope Oct 07 '14 at 12:36
  • 3
    There are no hard facts. Therefore the answers will be subjective. But I can re-open this if you want. – juanchopanza Oct 07 '14 at 12:37
  • @juanchopanza There is no hard facts that you are aware of. And apparently, today, you speak for the whole community. For example, I know there was research done on readability of text and speed of reading in different formats and/or fonts, so I taught, it should be possible to do it for code stypes as well, and maybe somebody did. Thank you for letting me know nothing like that was done before. – penelope Oct 07 '14 at 12:44
  • 1
    And I know that at the language level there are no hard facts to discuss. But your question is open. Now you can get people's opinions on this, because you won't get any facts (other than the fact there aren't any.) – juanchopanza Oct 07 '14 at 12:47
  • refer to [this](http://stackoverflow.com/questions/2337540/when-should-you-use-the-this-keyword-in-c) (haha) – Theolodis Oct 07 '14 at 12:59
  • 1
    I don't like to see `this->` in front of every member variable, but somebody else might like. That is why this questions asks for opinions. – BЈовић Oct 07 '14 at 12:59
  • You might get confused if you have overloaded operator -> – rmflow Oct 07 '14 at 13:01
  • 6
    Voted to re-open. Question isn't opinion based. The question was effectively if there are any *technical* reasons not to use `this` everywhere, and the answer to that question is "no." The question naturally leads to some opinion-based elaboration on why it's still a bad idea anyway, but the question itself is fact-based. One could close virtually every otherwise legitimate question as opinion-based if the possibility for opinion-based elaboration were a closure criteria. – John Dibling Oct 07 '14 at 14:11
  • Following a "clumsy, unpopular or not-widespread personal style" (your words) **is** bad practice... that would be my answer. – DevSolar Oct 07 '14 at 15:41
  • @JohnDibling It *is* a duplicate though. – juanchopanza Oct 07 '14 at 15:48
  • @juanchopanza: Not that I've seen. The link provided in comments by carlos.baez isn't. Do you know of a direct dupe? I'll vote to close myself in that case. – John Dibling Oct 07 '14 at 15:55
  • I have seen "this->" coding style before, so I would easily adapt to it. I believe any good C++ programmer should be able to adapt readily to reading the code, but you are bound to forget to use it occasionally, and the code will still compile without complaint. – jxh Oct 07 '14 at 19:24

2 Answers2

24

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).
utnapistim
  • 26,809
  • 3
  • 46
  • 82
  • 2
    How is `this` unexpected or make code difficult to read? It's meaning is obvious even if you don't have the habit of using it, isn't it? – Luke B. Oct 07 '14 at 15:02
  • 4
    It's unexpected and difficult to read because it's very uncommon. I usually refer to code like this as "mental speed-bump". – DevSolar Oct 07 '14 at 15:40
  • Because it's unusual, it makes the reader suspect that something unusual is going on. For example, http://stackoverflow.com/questions/8842886/how-bad-is-if-this-in-a-c-member-function?rq=1 – pjc50 Oct 07 '14 at 16:02
  • 1
    I can't agree that code is any harder to read with 'this'. It should be automatic to you if you are experienced with reading code. – Kik Oct 07 '14 at 17:10
  • 1
    Its unexpected or making code difficult to read because I would expect some semi sane coding standard where member vars have a prefix. If a method took a param with the same name as the mem var and so this was required then that too is also horrible IMO. – paulm Oct 07 '14 at 17:30
  • Subquestion: I saw manu C# code using this.some-var in the same way. Does c# enforce using it always? – Mauro H. Leggieri Oct 07 '14 at 17:31
  • 1
    @Mauro C# does not require the use of `this` if you don't need to. The likely reason is that in Visual Studio, typing `this.` will display all available members, making it easier to look up methods and the like. – James T Oct 07 '14 at 17:39
7

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.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • 2
    There is one technical reason: if used inside a class template, `this` can make a difference for name lookup. See http://stackoverflow.com/questions/10639053/name-lookups-in-c-templates – Tobias Brandt Oct 07 '14 at 13:45
  • 1
    @TobiasBrandt: That's an example of where `this` must be used, not an example of where `this` can't be used? – John Dibling Oct 07 '14 at 14:04
  • Yes, where it must be used. So, that's one case where it is certainly not "bad practice". – Tobias Brandt Oct 07 '14 at 14:21
  • @TobiasBrandt: Of course; I agree. But I don't see your point. – John Dibling Oct 07 '14 at 15:36
  • 1
    You last paragraph argues all C++ programmers should write C++ code using exactly the same style, which is difficult to agree with. – jxh Oct 07 '14 at 18:37
  • @JohnDibling Sorry, I misread your first sentence. – Tobias Brandt Oct 07 '14 at 18:58
  • @jxh: I don't maintain that *all* programmers should use a universal coding style, but not using `this` everywhere is a coding style that already has been adopted almost universally. Also, like it or not, agree with it or not, many (most?) organizations believe that coding style is something that should be dictated throughout the organization. – John Dibling Oct 07 '14 at 21:08
  • I agree on organization wide coding standards. Thus, if you are working in an organization that adopts using `this->`, then it is arguably better to use it than to raise a ruckus. – jxh Oct 07 '14 at 21:49