-
Notifications
You must be signed in to change notification settings - Fork 560
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Do not pass and return simple types by const ref #2453
Conversation
2dc13e3
to
567de39
Compare
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## main #2453 +/- ##
==========================================
+ Coverage 50.35% 50.83% +0.49%
==========================================
Files 390 391 +1
Lines 31954 32125 +171
==========================================
+ Hits 16087 16328 +241
+ Misses 15867 15797 -70
☔ View full report in Codecov by Sentry. |
@tylerjw Please check if this is fine. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general this PR does good stuff. Changing const double&
to double
in function declarations is a good change. Performance wise it makes no difference since doubles are 64 bits and we're all using 64 bit computers, but it's certainly a bit odd to see these fundamentally types used via a const ref. Likewise returning a const ref to a built in type is weird and doesn't solve any problems, generally speaking. It's good to avoid such things.
Let's be careful though. By changing const double&
to double
in a function definition we now are opening up the ability to mutate data was previously immutable which means some amount of intent is lost and it's a bit easier for someone to make potentially invalid changes to the code in the future. While adding const
to a non-reference parameter is meaningless in a function declaration, it does carry meaning in the function definition.
Also when changing const double&
return values to double
we run the risk of API breaks if users are storing the return value in another const double&
. Let's keep that in mind in case any public functions are being changed.
const double& value = someMoveItFunction(); // This breaks when the return type is not longer a reference
@@ -68,7 +68,7 @@ struct ContactTestData | |||
const std::vector<std::string>& active; | |||
|
|||
/** \brief If after a positive broadphase check the distance is below this threshold, a contact is added. */ | |||
const double& contact_distance; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has the the potential to change semantics since a reference to this double is prone to be changed by the calling code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverted back.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure if this particular struct is part of the API, but it does seem accesible the user to call.
One thing I am confused about though, the function ContactTestData
takes in a const double& parameter and initialises the variable. Was the function declared with nature of the reference in mind or was the variable declared just because it had to be assigned the const ref type passing in the function?
If it's the latter, maybe we should prefer to have it just as a double instead of a const&.
3b999b4
to
db0b936
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No more comments to make. Dismissing the review request.
ab09cf4
to
6869a63
Compare
re.compile(r"\b(const\s(bool|char|short|int|long|float|double|void))\s*&\s*")
6869a63
to
a5eb887
Compare
Description
Fixes #859
Continues the work from #2119
Checklist