public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* Coding standards proposal, usage of "this"
@ 2021-08-13 14:26 Simon Marchi
  2021-08-13 14:46 ` Paul Koning
                   ` (4 more replies)
  0 siblings, 5 replies; 14+ messages in thread
From: Simon Marchi @ 2021-08-13 14:26 UTC (permalink / raw)
  To: Simon Marchi via Gdb-patches

Hi all,

Here's something I had in mind for a while.  We don't consistently use
`this` when referring to fields or methods of the current object.  I
never now if I should use it or not, or point it out in review.  I
therefore propose these rules so that we have something to refer to.

 - Use `this` when referring to a data member that is not prefixed by
   `m_`.  Rationale: without `this`, it's not clear that you are
   referring to a member of the current class, versus a local or global
   variable.
 - Don't use `this` when referring to a data member that is prefixed by
   `m_`.  Rationale: the prefix already makes it clear that you are
   referring to a member of the current class, so adding `this` would
   just add noise.
 - 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.

If we had a convention for how to name internal helper methods, which
would make it clear that they are methods of the current object and not
free functions (a bit like `m_` does for data members), we could omit
`this` when calling such a method.  But we don't have that at the
moment.

A concrete example:

  int a_global;

  struct a_struct
  {
    int a_method ()
    {
      int a_local = 17;

      return (a_local
	      + a_global
	      + m_a_private_field
	      + this->a_public_field
	      + this->a_helper_method ());
    }

    int a_public_field;

  private:
    int a_helper_method ()
    {
      return m_a_private_field;
    }

    int m_a_private_field;
  };

Any comments?  My intention would be to add this to the coding standards
on the wiki.

Simon

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2021-08-18 11:43 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-13 14:26 Coding standards proposal, usage of "this" Simon Marchi
2021-08-13 14:46 ` Paul Koning
2021-08-13 14:51   ` Simon Marchi
2021-08-13 14:47 ` Andrew Burgess
2021-08-15 13:34 ` Lancelot SIX
2021-08-16 16:40 ` Christian Biesinger
2021-08-16 16:59   ` Simon Marchi
2021-08-18 11:43     ` Ruslan Kabatsayev
2021-08-16 17:06 ` John Baldwin
2021-08-16 17:11   ` Simon Marchi
2021-08-16 17:23     ` Luis Machado
2021-08-16 17:31       ` Simon Marchi
2021-08-17 10:01       ` Andrew Burgess
2021-08-16 17:28     ` John Baldwin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).