Skip to content

Commit e66b321

Browse files
jonatacklaanwj
andcommitted
Add C++ functions and methods section to developer notes
Credit for some parts to the Google C++ Style Guide "Inputs and Outputs" section at https://google.github.io/styleguide/cppguide.html#Inputs_and_Outputs Co-authored-by: laanwj <[email protected]>
1 parent 5fca70f commit e66b321

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

doc/developer-notes.md

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,27 @@ public:
141141
} // namespace foo
142142
```
143143

144+
Coding Style (C++ functions and methods)
145+
--------------------
146+
147+
- When ordering function parameters, place input parameters first, then any
148+
in-out parameters, followed by any output parameters.
149+
150+
- *Rationale*: API consistency.
151+
152+
- Prefer returning values directly to using in-out or output parameters. Use
153+
`std::optional` where helpful for returning values.
154+
155+
- *Rationale*: Less error-prone (no need for assumptions about what the output
156+
is initialized to on failure), easier to read, and often the same or better
157+
performance.
158+
159+
- Generally, use `std::optional` to represent optional by-value inputs (and
160+
instead of a magic default value, if there is no real default). Non-optional
161+
input parameters should usually be values or const references, while
162+
non-optional in-out and output parameters should usually be references, as
163+
they cannot be null.
164+
144165
Coding Style (C++ named arguments)
145166
------------------------------
146167

@@ -1390,22 +1411,9 @@ communication:
13901411
virtual boost::signals2::scoped_connection connectTipChanged(TipChangedFn fn) = 0;
13911412
```
13921413
1393-
- For consistency and friendliness to code generation tools, interface method
1394-
input and in-out parameters should be ordered first and output parameters
1395-
should come last.
1414+
- Interface methods should not be overloaded.
13961415
1397-
Example:
1398-
1399-
```c++
1400-
// Good: error output param is last
1401-
virtual bool broadcastTransaction(const CTransactionRef& tx, CAmount max_fee, std::string& error) = 0;
1402-
1403-
// Bad: error output param is between input params
1404-
virtual bool broadcastTransaction(const CTransactionRef& tx, std::string& error, CAmount max_fee) = 0;
1405-
```
1406-
1407-
- For friendliness to code generation tools, interface methods should not be
1408-
overloaded:
1416+
*Rationale*: consistency and friendliness to code generation tools.
14091417
14101418
Example:
14111419
@@ -1421,10 +1429,11 @@ communication:
14211429

14221430
### Internal interface naming style
14231431

1424-
- For consistency and friendliness to code generation tools, interface method
1425-
names should be `lowerCamelCase` and standalone function names should be
1432+
- Interface method names should be `lowerCamelCase` and standalone function names should be
14261433
`UpperCamelCase`.
14271434

1435+
*Rationale*: consistency and friendliness to code generation tools.
1436+
14281437
Examples:
14291438

14301439
```c++

0 commit comments

Comments
 (0)