mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 14:59:39 -04:00
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 <126646+laanwj@users.noreply.github.com>
This commit is contained in:
parent
5fca70f5b1
commit
e66b321fd1
1 changed files with 26 additions and 17 deletions
|
@ -141,6 +141,27 @@ public:
|
||||||
} // namespace foo
|
} // namespace foo
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Coding Style (C++ functions and methods)
|
||||||
|
--------------------
|
||||||
|
|
||||||
|
- When ordering function parameters, place input parameters first, then any
|
||||||
|
in-out parameters, followed by any output parameters.
|
||||||
|
|
||||||
|
- *Rationale*: API consistency.
|
||||||
|
|
||||||
|
- Prefer returning values directly to using in-out or output parameters. Use
|
||||||
|
`std::optional` where helpful for returning values.
|
||||||
|
|
||||||
|
- *Rationale*: Less error-prone (no need for assumptions about what the output
|
||||||
|
is initialized to on failure), easier to read, and often the same or better
|
||||||
|
performance.
|
||||||
|
|
||||||
|
- Generally, use `std::optional` to represent optional by-value inputs (and
|
||||||
|
instead of a magic default value, if there is no real default). Non-optional
|
||||||
|
input parameters should usually be values or const references, while
|
||||||
|
non-optional in-out and output parameters should usually be references, as
|
||||||
|
they cannot be null.
|
||||||
|
|
||||||
Coding Style (C++ named arguments)
|
Coding Style (C++ named arguments)
|
||||||
------------------------------
|
------------------------------
|
||||||
|
|
||||||
|
@ -1390,22 +1411,9 @@ communication:
|
||||||
virtual boost::signals2::scoped_connection connectTipChanged(TipChangedFn fn) = 0;
|
virtual boost::signals2::scoped_connection connectTipChanged(TipChangedFn fn) = 0;
|
||||||
```
|
```
|
||||||
|
|
||||||
- For consistency and friendliness to code generation tools, interface method
|
- Interface methods should not be overloaded.
|
||||||
input and in-out parameters should be ordered first and output parameters
|
|
||||||
should come last.
|
|
||||||
|
|
||||||
Example:
|
*Rationale*: consistency and friendliness to code generation tools.
|
||||||
|
|
||||||
```c++
|
|
||||||
// Good: error output param is last
|
|
||||||
virtual bool broadcastTransaction(const CTransactionRef& tx, CAmount max_fee, std::string& error) = 0;
|
|
||||||
|
|
||||||
// Bad: error output param is between input params
|
|
||||||
virtual bool broadcastTransaction(const CTransactionRef& tx, std::string& error, CAmount max_fee) = 0;
|
|
||||||
```
|
|
||||||
|
|
||||||
- For friendliness to code generation tools, interface methods should not be
|
|
||||||
overloaded:
|
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
|
@ -1421,10 +1429,11 @@ communication:
|
||||||
|
|
||||||
### Internal interface naming style
|
### Internal interface naming style
|
||||||
|
|
||||||
- For consistency and friendliness to code generation tools, interface method
|
- Interface method names should be `lowerCamelCase` and standalone function names should be
|
||||||
names should be `lowerCamelCase` and standalone function names should be
|
|
||||||
`UpperCamelCase`.
|
`UpperCamelCase`.
|
||||||
|
|
||||||
|
*Rationale*: consistency and friendliness to code generation tools.
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
|
|
||||||
```c++
|
```c++
|
||||||
|
|
Loading…
Add table
Reference in a new issue