this keyword. Every time you call a member function or use a member variable, you must preface it with
this.. In C++, this is optional, i.e. implicit
this. As a result, when you’re reading over a method, it’s not immediately obvious where each variable comes from. There are three possibilities:
In order to disambiguate the first option, some programmers prefix all member variables with
m_. I dislike this notation because it might confuse people reading your code who are unfamiliar with the practice. Instead, it’s best for the notation to have some meaning defined in the language itself, even if it is redundant. That’s why it’s good practice to preface all member variables with
this->. To some, this might reek of Java-esque verbosity (although Java also has implicit
this). I would counter with this article, and specifically a quote from Josh Bloch:
A little redundancy in a language is a good thing. It’s important for readability.
Not all redundancy is helpful; you have to pick the right constructs. For example, curly braces and semicolons just get in the way. While I don’t expect Java or C++ to change, as long as we’re stuck with them, we should do what we can to make our code readable.