@@ -32,6 +32,7 @@ Developer Notes
32
32
- [ C++ data structures] ( #c-data-structures )
33
33
- [ Strings and formatting] ( #strings-and-formatting )
34
34
- [ Shadowing] ( #shadowing )
35
+ - [ Lifetimebound] ( #lifetimebound )
35
36
- [ Threads and synchronization] ( #threads-and-synchronization )
36
37
- [ Scripts] ( #scripts )
37
38
- [ Shebang] ( #shebang )
96
97
Guidelines] ( https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Renum-caps ) ,
97
98
which recommend using ` snake_case ` . Please use what seems appropriate.
98
99
- Class names, function names, and method names are UpperCamelCase
99
- (PascalCase). Do not prefix class names with ` C ` .
100
+ (PascalCase). Do not prefix class names with ` C ` . See [ Internal interface
101
+ naming style] ( #internal-interface-naming-style ) for an exception to this
102
+ convention.
103
+
100
104
- Test suite naming convention: The Boost test suite in file
101
105
` src/test/foo_tests.cpp ` should be named ` foo_tests ` . Test suite names
102
106
must be unique.
@@ -138,6 +142,27 @@ public:
138
142
} // namespace foo
139
143
```
140
144
145
+ Coding Style (C++ functions and methods)
146
+ --------------------
147
+
148
+ - When ordering function parameters, place input parameters first, then any
149
+ in-out parameters, followed by any output parameters.
150
+
151
+ - * Rationale* : API consistency.
152
+
153
+ - Prefer returning values directly to using in-out or output parameters. Use
154
+ ` std::optional ` where helpful for returning values.
155
+
156
+ - * Rationale* : Less error-prone (no need for assumptions about what the output
157
+ is initialized to on failure), easier to read, and often the same or better
158
+ performance.
159
+
160
+ - Generally, use ` std::optional ` to represent optional by-value inputs (and
161
+ instead of a magic default value, if there is no real default). Non-optional
162
+ input parameters should usually be values or const references, while
163
+ non-optional in-out and output parameters should usually be references, as
164
+ they cannot be null.
165
+
141
166
Coding Style (C++ named arguments)
142
167
------------------------------
143
168
@@ -657,10 +682,6 @@ Wallet
657
682
658
683
- Make sure that no crashes happen with run-time option ` -disablewallet ` .
659
684
660
- - Include ` db_cxx.h ` (BerkeleyDB header) only when ` ENABLE_WALLET ` is set.
661
-
662
- - * Rationale* : Otherwise compilation of the disable-wallet build will fail in environments without BerkeleyDB.
663
-
664
685
General C++
665
686
-------------
666
687
@@ -863,12 +884,22 @@ from using a different variable with the same name),
863
884
please name variables so that their names do not shadow variables defined in the source code.
864
885
865
886
When using nested cycles, do not name the inner cycle variable the same as in
866
- the upper cycle, etc.
887
+ the outer cycle, etc.
888
+
889
+ Lifetimebound
890
+ --------------
891
+
892
+ The [Clang `lifetimebound`
893
+ attribute](https://clang.llvm.org/docs/AttributeReference.html#lifetimebound)
894
+ can be used to tell the compiler that a lifetime is bound to an object and
895
+ potentially see a compile-time warning if the object has a shorter lifetime from
896
+ the invalid use of a temporary. You can use the attribute by adding a `LIFETIMEBOUND`
897
+ annotation defined in `src/attributes.h`; please grep the codebase for examples.
867
898
868
899
Threads and synchronization
869
900
----------------------------
870
901
871
- - Prefer `Mutex` type to `RecursiveMutex` one
902
+ - Prefer `Mutex` type to `RecursiveMutex` one.
872
903
873
904
- Consistently use [Clang Thread Safety Analysis](https://clang.llvm.org/docs/ThreadSafetyAnalysis.html) annotations to
874
905
get compile-time warnings about potential race conditions in code. Combine annotations in function declarations with
@@ -947,6 +978,8 @@ TRY_LOCK(cs_vNodes, lockNodes);
947
978
Scripts
948
979
--------------------------
949
980
981
+ Write scripts in Python rather than bash, when possible.
982
+
950
983
### Shebang
951
984
952
985
- Use `#!/usr/bin/env bash` instead of obsolete `#!/bin/bash`.
@@ -1389,22 +1422,9 @@ communication:
1389
1422
virtual boost::signals2::scoped_connection connectTipChanged(TipChangedFn fn) = 0;
1390
1423
```
1391
1424
1392
- - For consistency and friendliness to code generation tools, interface method
1393
- input and inout parameters should be ordered first and output parameters
1394
- should come last.
1425
+ - Interface methods should not be overloaded.
1395
1426
1396
- Example:
1397
-
1398
- ```c++
1399
- // Good: error output param is last
1400
- virtual bool broadcastTransaction(const CTransactionRef& tx, CAmount max_fee, std::string& error) = 0;
1401
-
1402
- // Bad: error output param is between input params
1403
- virtual bool broadcastTransaction(const CTransactionRef& tx, std::string& error, CAmount max_fee) = 0;
1404
- ```
1405
-
1406
- - For friendliness to code generation tools, interface methods should not be
1407
- overloaded:
1427
+ *Rationale*: consistency and friendliness to code generation tools.
1408
1428
1409
1429
Example:
1410
1430
@@ -1418,10 +1438,13 @@ communication:
1418
1438
virtual bool disconnect(NodeId id) = 0;
1419
1439
```
1420
1440
1421
- - For consistency and friendliness to code generation tools, interface method
1422
- names should be `lowerCamelCase` and standalone function names should be
1441
+ ### Internal interface naming style
1442
+
1443
+ - Interface method names should be ` lowerCamelCase ` and standalone function names should be
1423
1444
` UpperCamelCase ` .
1424
1445
1446
+ * Rationale* : consistency and friendliness to code generation tools.
1447
+
1425
1448
Examples:
1426
1449
1427
1450
``` c++
0 commit comments