C++ StyleGuide: Using methods and public class/struct members #10
No reviewers
Labels
No Label
No Milestone
No Assignees
7 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: blender/blender-developer-docs#10
Loading…
Reference in New Issue
No description provided.
Delete Branch "filedescriptor/blender-developer-docs:using-this-keyword"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
When using method or a public data member, use the
this
keyword.Using public class/struct membersto C++ StyleGuide: Using public class/struct membersThanks for writing this up! In general I think it's important that there's context and the reader can easily see the scope of accessed functions and variables. So +1 for the proposal. One detail though-- I don't think this totally matches up with what we've been doing so far though. Another way to state it would be:
In other words, no need to use it for private variables with an
_
suffix, where it's already clear they're part of the class.@HooglyBoogly Hm I thought that my wording (and the title) made it clear that this is only for public members. Or maybe I'm not understanding what you mean?
This for any methods, not only for public.
@mod_moder Ah yes, I see it now. Sorry, will fix.
I'm fine with this.
For visibility, can you create a devtalk topic that links here? Then wait a few days and commit assuming there are no objections.
I personally find that having
this->
everywhere adds unnecessary noise that hurts readability.While I've never found it necessary (free functions are typically preceded by their module prefix/namespace, and global variables are rare), if someone wants the extra help for making the distinction, the solution is as easy as setting the syntax highlighting of your editor accordingly.
I would also prefer not to see this enforced. I find it adding quite some noise to the code, and it should not be needed in most cases.
But I could live with this rule if most other devs think it's a good one. ;)
Not all editor support such functionality. And even more, i'd prefer to do not totally rely on parsing of includes for anything if this is not necessary.
@brecht Created a discussion thread: https://devtalk.blender.org/t/c-style-guide-using-the-this-keyword-when-accessing-public-members-and-methods/33029
I definitely prefer using
this->
because otherwise it's unclear whether the called function depends onthis
or not. I quite often found myself in code by others where I wondered whether something is calling a standalone function or something that depends onthis
. Even Python enforces this, which is otherwise a very concise language.I haven't seen this add a significant amount of noise. The only places I can think of where this is annoying is when writing a somewhat complex math expression that uses many data members with short names. Overall, this seems rare in most code. If it's really a problem in some function there are easy ways around it: make the data members private and use the trailing underscore or just making a local copy of the variable.
Shouldn't calls to private/protected methods also use
this->
actually?EDIT: Re-reading the current proposed text, this is what it says now I think... But then maybe the example can be updated to show also the call to a private method?
@mont29 Yes I can add a few more examples and cover more cases :)
C++ StyleGuide: Using public class/struct membersto C++ StyleGuide: Using methods and public class/struct membersI'll wait till the end of the week, so others have a chance of seeing the devtalk topic and give their feedback.
One question: Should I add an example for
struct
as well?To me the important thing is that
this->
is used when there is no trailing underscore in the member name. Not sure if it really helps to have even more examples. Makes it look more complex than it is.@ -735,6 +735,39 @@ class X {
};
```
## Using public class/struct data members and methods
Maybe call this "Use of this->"
To me this is not so much about the general use of
this
though. Or do you literally meanthis->
? I think it's better to be explicit.Maybe "Accessing methods and public data members in classes and structs" ?
I'm mostly just trying to make this guide useful for people who don't know already what's in it. People might go to it because they want to know whether they should use
this->
or not, not because they don't know how to access methods and data members. Don't have a strong opinion on this though.Agreed with Jacques here personally. Mentioning
this->
in the title of this section is the simplest way to tell people what its point is.@ -737,1 +737,4 @@
## Using public class/struct data members and methods
Use `this->` prefix when accessing methods and public data members.
"Use
this->
when accessing methods and data members that don't have a[trailing underscore](link-to-that-guide)
."Think that phrasing results in less confusion.
Seems a bit odd to me to use the trailing underscore as the primary reasoning. This would read better to me:
"Use
this->
when accessing methods and public data members (i.e. members that don't have a[trailing underscore](#class-data-member-names)
)."Hm, I thought I wrote a comment here earlier, but somehow it is gone. I'm not using the trailing underscore as a reason but as the rule/guide. The reason is that I want to be able to see whether something depends on
this
or not. With the trailing underscore it is already apparent, so the extrathis->
is not necessary.If, hypothetically, we had a rule like all class members (data and methods) had to use a trailing underscore, we wouldn't need
this->
IMO. So there is definitely an important relation.@ -738,0 +740,4 @@
Use `this->` prefix when accessing methods and public data members.
``` c++
class X {
Simplified example:
Again I'm not sure I would use the trailing underscore as the reasoning for using
this
. That raises more questions in my mind :D But maybe others can comment on this? @brecht @Sergey ?The trailing underscore is not the reason, it is the rule/guide.
The reason is that we want to be able to see whether a specific symbol belongs to a class/struct or not. When there is a trailing underscore, that is already enough information.
If, hypothetically, we had a rule that would force us to use trailing underscores for all members, then we wouldn't have to use
this->
imo. Obviously, we don't and shouldn't have this rule.@ -738,0 +765,4 @@
};
```
Private and protected members don't need the explcit `this->`
This part can be removed with the proposed sentence above. Also there is a typo in
explicit
.Added some comments for how I'd phrase this style guide.