Re: Coding standards proposal, usage of "this"

Ruslan Kabatsayev via Gdb-patches
2021-08-18T11:43:33Z

On Mon, 16 Aug 2021 at 20:01, Simon Marchi via Gdb-patches
<gdb-patches@sourceware.org> wrote:
>
> >> - Use `this` when referring to a method of the current class.
> >> Rationale: without `this, it's not clear that you are referring to a
> >> method of the current class, versus a free function.
> >
> > I'm not really a fan of this, and I am also not aware of other
> > projects using such a style. Since this makes calling member functions
> > more verbose/uglier than calling free functions, this would also
> > discourage member functions. Is there a need to distinguish these?
>
> There's no *need*, of course. It's just based on my experience, I
> remember seeing some function calls, wondering how it could even work
> with the passed arguments. And then I realized it was a method call,
> where `this` is passed implicitly.
>
> Do you think it would discourage adding member functions, in opposition
> to adding free functions? Or just leaving the code in-line, leading to
> bigger methods?

This will prevent the "prefer nonmember, nonfriends" mantra from
working. Suppose you made a free function (so as to prevent accidental
coupling to the private state of your class), and later it happens to
need access to the state. This function is then turned into a
(possibly const) method. And now... you have to alter all the callers
to prefix the calls with `this->`, even though it's syntactically
useless. Moreover, it may be easy to miss some, so unless there's a
static analyzer checking for this rule, this leads to inconsistency.

>
> Simon