diff options
author | Anna Henningsen <anna@addaleax.net> | 2018-09-22 21:43:53 +0300 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2018-10-21 06:03:15 +0300 |
commit | 5550510056570ed1266c4c3f9bab07f00d4e8ef2 (patch) | |
tree | 5b14a740fc79b40d15cc011ac55417a1200fedba /CPP_STYLE_GUIDE.md | |
parent | 87b808f7611b5ed17164f939760f562d5af9fdb4 (diff) |
doc: formalize non-const reference usage in C++ style guide
We generally avoid using non-const references if not necessary. This
formalizes this rules by writing them down in the C++ style guide.
(Note: Some reviews are from the original PR.)
Refs: https://github.com/nodejs/node/pull/23028
PR-URL: https://github.com/nodejs/node/pull/23155
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Diffstat (limited to 'CPP_STYLE_GUIDE.md')
-rw-r--r-- | CPP_STYLE_GUIDE.md | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/CPP_STYLE_GUIDE.md b/CPP_STYLE_GUIDE.md index 36c3538f418..e14de6cc48b 100644 --- a/CPP_STYLE_GUIDE.md +++ b/CPP_STYLE_GUIDE.md @@ -19,6 +19,7 @@ * [Memory allocation](#memory-allocation) * [Use `nullptr` instead of `NULL` or `0`](#use-nullptr-instead-of-null-or-0) * [Ownership and Smart Pointers](#ownership-and-smart-pointers) + * [Avoid non-const references](#avoid-non-const-references) * [Others](#others) * [Type casting](#type-casting) * [Using `auto`](#using-auto) @@ -212,6 +213,43 @@ ownership to the callee and invalidates the caller's instance. Don't use `std::auto_ptr`, it is deprecated ([Reference][cppref_auto_ptr]). +### Avoid non-const references + +Using non-const references often obscures which values are changed by an +assignment. Consider using a pointer instead, which requires more explicit +syntax to indicate that modifications take place. + +```c++ +class ExampleClass { + public: + explicit ExampleClass(OtherClass* other_ptr) : pointer_to_other_(other_ptr) {} + + void SomeMethod(const std::string& input_param, + std::string* in_out_param); // Pointer instead of reference + + const std::string& get_foo() const { return foo_string_; } + void set_foo(const std::string& new_value) { foo_string_ = new_value; } + + void ReplaceCharacterInFoo(char from, char to) { + // A non-const reference is okay here, because the method name already tells + // users that this modifies 'foo_string_' -- if that is not the case, + // it can still be better to use an indexed for loop, or leave appropriate + // comments. + for (char& character : foo_string_) { + if (character == from) + character = to; + } + } + + private: + std::string foo_string_; + // Pointer instead of reference. If this object 'owns' the other object, + // this should be a `std::unique_ptr<OtherClass>`; a + // `std::shared_ptr<OtherClass>` can also be a better choice. + OtherClass* pointer_to_other_; +}; +``` + ## Others ### Type casting |