xref: /aosp_15_r20/external/ot-br-posix/STYLE_GUIDE.md (revision 4a64e381480ef79f0532b2421e44e6ee336b8e0d)
1*4a64e381SAndroid Build Coastguard Worker# OpenThread Coding Conventions and Style
2*4a64e381SAndroid Build Coastguard Worker
3*4a64e381SAndroid Build Coastguard Worker- [1 C and C++](#c-and-c)
4*4a64e381SAndroid Build Coastguard Worker  - [1.1 Standards](#standards)
5*4a64e381SAndroid Build Coastguard Worker  - [1.2 Conventions and Best Practices](#conventions-and-best-practices)
6*4a64e381SAndroid Build Coastguard Worker  - [1.3 Tightly-constrained Systems and Shared Infrastructure](#tightly-constrained-systems-and-shared-infrastructure)
7*4a64e381SAndroid Build Coastguard Worker  - [1.4 Format and Style](#format-and-style)
8*4a64e381SAndroid Build Coastguard Worker  - [1.5 Comments](#comments)
9*4a64e381SAndroid Build Coastguard Worker- [2 Python](#python)
10*4a64e381SAndroid Build Coastguard Worker  - [2.1 Standards](#standards)
11*4a64e381SAndroid Build Coastguard Worker  - [2.2 Conventions and Best Practices](#conventions-and-best-practices)
12*4a64e381SAndroid Build Coastguard Worker  - [2.3 Format and Style](#format-and-style)
13*4a64e381SAndroid Build Coastguard Worker
14*4a64e381SAndroid Build Coastguard Worker# C and C++
15*4a64e381SAndroid Build Coastguard Worker
16*4a64e381SAndroid Build Coastguard Worker## Standards
17*4a64e381SAndroid Build Coastguard Worker
18*4a64e381SAndroid Build Coastguard Worker- C
19*4a64e381SAndroid Build Coastguard Worker  - OpenThread uses and enforces the ISO9899:1999 (aka ISO C99, C99) C language standard as the minimum.
20*4a64e381SAndroid Build Coastguard Worker- C++
21*4a64e381SAndroid Build Coastguard Worker  - OpenThread uses and enforces the ISO14882:2011 (aka ISO C++11, C++11) C++ language standard as the minimum.
22*4a64e381SAndroid Build Coastguard Worker- Extensions
23*4a64e381SAndroid Build Coastguard Worker  - Wherever possible, toolchain-specific (e.g GCC/GNU) extensions or the use of later standards shall be avoided or shall be leveraged through toolchain-compatibility preprocessor macros.
24*4a64e381SAndroid Build Coastguard Worker
25*4a64e381SAndroid Build Coastguard Worker## Conventions and Best Practices
26*4a64e381SAndroid Build Coastguard Worker
27*4a64e381SAndroid Build Coastguard Worker### Language Independent
28*4a64e381SAndroid Build Coastguard Worker
29*4a64e381SAndroid Build Coastguard Worker- Inline functions should be used judiciously.
30*4a64e381SAndroid Build Coastguard Worker  - The use of code in headers and, more specifically, the use of the non-local scope inline functions should be avoided. Exception: Simple setters and getters are fine since the compiler can efficiently optimize these and make their overhead as low as a direct data member access.
31*4a64e381SAndroid Build Coastguard Worker- Return Statements
32*4a64e381SAndroid Build Coastguard Worker  - There should be one return statement per free function or method at the end of the free function or method.
33*4a64e381SAndroid Build Coastguard Worker- Non-local Goto
34*4a64e381SAndroid Build Coastguard Worker  - There should be no calls to the functions `setjmp` or `longjmp`.
35*4a64e381SAndroid Build Coastguard Worker- Local Goto
36*4a64e381SAndroid Build Coastguard Worker  - There should be no calls to the C/C++ keyword goto. Exception: The use of local gotos for the purposes of common error handling blocks and single points of function return at the bottom of a function.
37*4a64e381SAndroid Build Coastguard Worker- C Preprocessor
38*4a64e381SAndroid Build Coastguard Worker  - Use of the C preprocessor should be limited to file inclusion and simple macros.
39*4a64e381SAndroid Build Coastguard Worker  - Macros shall not be defined within a function or a block and should be defined at the top of a file.
40*4a64e381SAndroid Build Coastguard Worker  - All `#else`, `#elif`, and `#endif` preprocessor directives shall reside in the same file as the `#if` or `#ifdef` directive to which they are related.
41*4a64e381SAndroid Build Coastguard Worker  - All `#endif` directives equal to or greater than 20 lines away from the `#if` or `#ifdef` directive to which they are related shall be decorated by language comment indicating the conditional they are associated with.
42*4a64e381SAndroid Build Coastguard Worker  - Preprocessor `#include` directives in a file shall only be preceded by other preprocessor directives or comments.
43*4a64e381SAndroid Build Coastguard Worker  - Preprocessor `#include` directives shall use brace (“<”) and (“>”) style for all public headers, including C and C++ standard library, or other first- and third-party public library headers.
44*4a64e381SAndroid Build Coastguard Worker  - Preprocessor `#include` directives should use double quote (‘“‘) and (‘“‘) style for all private or relative headers.
45*4a64e381SAndroid Build Coastguard Worker  - Preprocessor `#include` directives should be grouped, ordered, or sorted as follows:
46*4a64e381SAndroid Build Coastguard Worker    - This compilation unit's corresponding header, if any.
47*4a64e381SAndroid Build Coastguard Worker    - C++ Standard Library headers
48*4a64e381SAndroid Build Coastguard Worker    - C Standard Library headers
49*4a64e381SAndroid Build Coastguard Worker    - Third-party library headers
50*4a64e381SAndroid Build Coastguard Worker    - First-party library headers
51*4a64e381SAndroid Build Coastguard Worker    - Private or local headers
52*4a64e381SAndroid Build Coastguard Worker    - Alphanumeric order within each subgroup
53*4a64e381SAndroid Build Coastguard Worker  - The preprocessor shall not be used to redefine reserved language keywords.
54*4a64e381SAndroid Build Coastguard Worker  - Unused code shall not be disabled by commenting it out with C- or C++-style comments or with preprocessor `#if 0 ... #endif` semantics.
55*4a64e381SAndroid Build Coastguard Worker  - Use of the preprocessor token concatenation operator '##' should be avoided.
56*4a64e381SAndroid Build Coastguard Worker  - The `undef` preprocessor directive should be avoided and shall never be used to undefine a symbol from a foreign module.
57*4a64e381SAndroid Build Coastguard Worker- Object Scope
58*4a64e381SAndroid Build Coastguard Worker  - Data objects shall be declared at the smallest possible level of scope.
59*4a64e381SAndroid Build Coastguard Worker  - No declaration in an inner scope shall hide or shadow a declaration in an outer scope. Compiler flags shall be set to flag and enforce this.
60*4a64e381SAndroid Build Coastguard Worker- Unbounded Recursion
61*4a64e381SAndroid Build Coastguard Worker  - There shall be no direct or indirect use of unbounded recursive function calls.
62*4a64e381SAndroid Build Coastguard Worker- Symmetric APIs
63*4a64e381SAndroid Build Coastguard Worker  - Wherever possible and appropriate, particularly around the management of resources, APIs should be symmetric. For example, if there is a free function or object method that allocates a resource, then there should be one that deallocates it. If there is a free function or object method that opens a file or network stream, then there should be one that closes it.
64*4a64e381SAndroid Build Coastguard Worker- Use C stdint.h or C++ cstdint for Plain Old Data Types
65*4a64e381SAndroid Build Coastguard Worker  - Standard, scalar data types defined in stdint.h (C) or cstdint (C++) should be used for basic signed and unsigned integer types, especially when size and serialization to non-volatile storage or across a network is concerned. Examples of these are: `uint8_t`, `int8_t`, etc.
66*4a64e381SAndroid Build Coastguard Worker- Constant Qualifiers
67*4a64e381SAndroid Build Coastguard Worker  - Read-only methods, global variables, stack variables, or data members are read-only should be qualified using the C or C++ `const` qualifier.
68*4a64e381SAndroid Build Coastguard Worker  - Pointers or references to read-only objects or storage, including but not limited to function parameters, should be qualified using the C or C++ `const` qualifier.
69*4a64e381SAndroid Build Coastguard Worker- Header Include Guard
70*4a64e381SAndroid Build Coastguard Worker  - All C and C++ headers shall use preprocessor header include guards.
71*4a64e381SAndroid Build Coastguard Worker  - The terminating endif preprocessor directive shall have a comment, C or C++ depending on the header type, containing the preprocessor symbol introduced by the ifndef directive starting the guard.
72*4a64e381SAndroid Build Coastguard Worker  - The symbol used for the guard should be the file name, converted to all uppercase, with any spaces (“ “) or dots (“.”) converted to underscores (“\_”).
73*4a64e381SAndroid Build Coastguard Worker- Function and Method Prototypes
74*4a64e381SAndroid Build Coastguard Worker  - All void functions or methods shall explicitly declare and specify the void type keyword.
75*4a64e381SAndroid Build Coastguard Worker
76*4a64e381SAndroid Build Coastguard Worker### C
77*4a64e381SAndroid Build Coastguard Worker
78*4a64e381SAndroid Build Coastguard Worker- C / C++ Linkage Wrappers
79*4a64e381SAndroid Build Coastguard Worker  - All header files intended to have C symbol linkage shall use “extern C” linkage wrappers.
80*4a64e381SAndroid Build Coastguard Worker
81*4a64e381SAndroid Build Coastguard Worker### C++
82*4a64e381SAndroid Build Coastguard Worker
83*4a64e381SAndroid Build Coastguard Worker- Prefer Passing Parameters by Reference to Pointer
84*4a64e381SAndroid Build Coastguard Worker  - Unlike C, C++ offers an alternate way to alias data over and above a pointer, the reference, indicated by the & symbol. Where appropriate, the reference should be preferred to the pointer.
85*4a64e381SAndroid Build Coastguard Worker- Passing Base Scalars
86*4a64e381SAndroid Build Coastguard Worker  - Size- and call frequency-based considerations should be made when passing scalars as to whether they should be passed by value or by constant reference; however, pass-by-value should generally be preferred.
87*4a64e381SAndroid Build Coastguard Worker- Eliminate Unnecessary Destructors
88*4a64e381SAndroid Build Coastguard Worker  - The creation of empty or useless destructors should be avoided. Empty or useless destructors should be removed.
89*4a64e381SAndroid Build Coastguard Worker- Default Parameters
90*4a64e381SAndroid Build Coastguard Worker  - When you declare C++ free functions and object methods, you should avoid or minimize using default parameters.
91*4a64e381SAndroid Build Coastguard Worker  - When you declare C++ virtual object methods, you shall avoid using default parameters.
92*4a64e381SAndroid Build Coastguard Worker- Global and Scoped Static Construction
93*4a64e381SAndroid Build Coastguard Worker  - There shall be no use of global, static or otherwise, object construction. The use of scoped static object construction should be avoided.
94*4a64e381SAndroid Build Coastguard Worker- C++-style Casts
95*4a64e381SAndroid Build Coastguard Worker  - Wherever possible and practical, C++ style casts should be used and preferred to the C style cast equivalent.
96*4a64e381SAndroid Build Coastguard Worker- Avoid `using namespace` Statements in Headers
97*4a64e381SAndroid Build Coastguard Worker  - The C++ `using namespace` statement should not be used outside of object scope inside header files.
98*4a64e381SAndroid Build Coastguard Worker
99*4a64e381SAndroid Build Coastguard Worker## Tightly-constrained Systems and Shared Infrastructure
100*4a64e381SAndroid Build Coastguard Worker
101*4a64e381SAndroid Build Coastguard Worker- Heap-based resource allocation should be avoided.
102*4a64e381SAndroid Build Coastguard Worker- There shall be no direct or indirect use of recursive function calls.
103*4a64e381SAndroid Build Coastguard Worker- The use of virtual functions should be avoided.
104*4a64e381SAndroid Build Coastguard Worker- The use of the C++ Standard Library shall be avoided.
105*4a64e381SAndroid Build Coastguard Worker- The use of the C++ Standard Template Library (STL) should be avoided or minimized.
106*4a64e381SAndroid Build Coastguard Worker- The use of the C++ templates should be avoided or minimized.
107*4a64e381SAndroid Build Coastguard Worker- Code shall not use exceptions.
108*4a64e381SAndroid Build Coastguard Worker- Code shall not use C++ runtime type information (RTTI), including facilities that rely upon it, such as `dynamic_cast` and `typeid`.
109*4a64e381SAndroid Build Coastguard Worker
110*4a64e381SAndroid Build Coastguard Worker## Format and Style
111*4a64e381SAndroid Build Coastguard Worker
112*4a64e381SAndroid Build Coastguard Worker- OpenThread uses `script/make-pretty` to reformat code and enforce code format and style. `script/make-pretty check` is included in OpenThread's continuous integration and must pass before a pull request is merged.
113*4a64e381SAndroid Build Coastguard Worker
114*4a64e381SAndroid Build Coastguard Worker- `script/make-pretty` requires [clang-format v14.0.0](https://releases.llvm.org/download.html#14.0.0) for C/C++ and [yapf](https://github.com/google/yapf) for Python.
115*4a64e381SAndroid Build Coastguard Worker
116*4a64e381SAndroid Build Coastguard Worker### File Names
117*4a64e381SAndroid Build Coastguard Worker
118*4a64e381SAndroid Build Coastguard Worker- File names should match the names and types of what is described in the file. If a file contains many declarations and definitions, the author should choose the one that predominantly describes or that makes the most sense.
119*4a64e381SAndroid Build Coastguard Worker- File contents and names should be limited in the scope of what they contain. It may also be possible that there is too much stuff in one file and you need to break it up into multiple files.
120*4a64e381SAndroid Build Coastguard Worker- File names should be all lower case.
121*4a64e381SAndroid Build Coastguard Worker- File extensions shall be indicative and appropriate for the type and usage of the source or header file.
122*4a64e381SAndroid Build Coastguard Worker
123*4a64e381SAndroid Build Coastguard Worker### Naming
124*4a64e381SAndroid Build Coastguard Worker
125*4a64e381SAndroid Build Coastguard Worker- Names should be descriptive but not overly so and they should give some idea of scope and should be selected such that _wrong code looks wrong_.
126*4a64e381SAndroid Build Coastguard Worker- Names shall not give any idea of type, such as is done with System Hungarian notation.
127*4a64e381SAndroid Build Coastguard Worker- Case
128*4a64e381SAndroid Build Coastguard Worker  - C preprocessor symbols should be all uppercase.
129*4a64e381SAndroid Build Coastguard Worker  - All OpenThread class, namespace, structure, method, function, enumeration, and type names in the C/C++ language shall be in _upper camel case_. Exception: the top level OpenThread namespace 'ot'.
130*4a64e381SAndroid Build Coastguard Worker  - All OpenThread instantiated names of instances of classes, namespaces, structures, methods, functions, enumerations, and types as well as method and function parameters in the C++ language shall be in _lower camel case_.
131*4a64e381SAndroid Build Coastguard Worker- Symbol Qualification
132*4a64e381SAndroid Build Coastguard Worker  - All OpenThread C public data types and free functions should have `ot` prepended to their name.
133*4a64e381SAndroid Build Coastguard Worker  - All OpenThread C++ code should be in the ‘ot’ top-level namespace.
134*4a64e381SAndroid Build Coastguard Worker- Scope
135*4a64e381SAndroid Build Coastguard Worker  - All global data shall have a `g` prepended to the name to denote global scope.
136*4a64e381SAndroid Build Coastguard Worker  - All static data shall have a `s` prepended to the name to denote static scope.
137*4a64e381SAndroid Build Coastguard Worker  - All class or structure data members shall have a `m` prepended to the name to denote member scope.
138*4a64e381SAndroid Build Coastguard Worker  - All free function or method parameters should have an `a` prepended to the name to denote function parameter scope.
139*4a64e381SAndroid Build Coastguard Worker  - All variables that do not have such prefixes shall be assumed to be function local scope.
140*4a64e381SAndroid Build Coastguard Worker
141*4a64e381SAndroid Build Coastguard Worker### White Space
142*4a64e381SAndroid Build Coastguard Worker
143*4a64e381SAndroid Build Coastguard Worker- Indentation shall be 4 space characters.
144*4a64e381SAndroid Build Coastguard Worker- Conditionals shall always appear on a separate line from the code to execute as a result of the condition.
145*4a64e381SAndroid Build Coastguard Worker- Scoped Variable declarations
146*4a64e381SAndroid Build Coastguard Worker  - All scoped (i.e. stack) variable declarations should be placed together at the top of the enclosing scope in which they are used.
147*4a64e381SAndroid Build Coastguard Worker  - There shall be an empty line after all such variable declarations.
148*4a64e381SAndroid Build Coastguard Worker  - The names of all variable declarations should be left aligned.
149*4a64e381SAndroid Build Coastguard Worker- Data Member declarations
150*4a64e381SAndroid Build Coastguard Worker  - All data member declarations should be placed together.
151*4a64e381SAndroid Build Coastguard Worker  - The names of all data member declarations should be left aligned.
152*4a64e381SAndroid Build Coastguard Worker  - The data member declarations for C++ classes should be placed at the end or tail of the class.
153*4a64e381SAndroid Build Coastguard Worker- Braces
154*4a64e381SAndroid Build Coastguard Worker  - Braces should go on their own lines.
155*4a64e381SAndroid Build Coastguard Worker  - Statements should never be on the same line following a closing brace.
156*4a64e381SAndroid Build Coastguard Worker- Keywords
157*4a64e381SAndroid Build Coastguard Worker  - There should be a single space after language-reserved keywords (for, while, if, etc).
158*4a64e381SAndroid Build Coastguard Worker
159*4a64e381SAndroid Build Coastguard Worker## Comments
160*4a64e381SAndroid Build Coastguard Worker
161*4a64e381SAndroid Build Coastguard Worker- All code should use Doxygen to:
162*4a64e381SAndroid Build Coastguard Worker  - Detail what the various source and header files are and how they fit into the broader context.
163*4a64e381SAndroid Build Coastguard Worker  - Detail what the various C++ namespaces are.
164*4a64e381SAndroid Build Coastguard Worker  - Detail what the constants, C preprocessor definitions, and enumerations are.
165*4a64e381SAndroid Build Coastguard Worker  - Detail what the globals are and how they are to be used.
166*4a64e381SAndroid Build Coastguard Worker  - Detail what the free function and object / class methods are and how they are to be used, what their parameters are, and what their return values are.
167*4a64e381SAndroid Build Coastguard Worker  - Detail any other important technical information or theory of operation unique and relevant to the stack that is not otherwise captured in architecture, design, or protocol documentation.
168*4a64e381SAndroid Build Coastguard Worker- Every public, and ideally private, free function and class method should likewise have a prologue comment that:
169*4a64e381SAndroid Build Coastguard Worker  - Briefly describes what it is and what it does.
170*4a64e381SAndroid Build Coastguard Worker  - Describes in detail, optionally, what it is and what it does.
171*4a64e381SAndroid Build Coastguard Worker  - Describes the purpose, function, and influence of each parameter as well as whether it is an input, an output, or both.
172*4a64e381SAndroid Build Coastguard Worker  - Describes the return value, if present, and the expected range or constraints of it.
173*4a64e381SAndroid Build Coastguard Worker
174*4a64e381SAndroid Build Coastguard Worker# Python
175*4a64e381SAndroid Build Coastguard Worker
176*4a64e381SAndroid Build Coastguard Worker## Standards
177*4a64e381SAndroid Build Coastguard Worker
178*4a64e381SAndroid Build Coastguard Worker- OpenThread uses and enforces both Python 2 and Python 3. Support for Python 2 is a result of the fact that some current Linux distributions and Macs are still using 2.x as default.
179*4a64e381SAndroid Build Coastguard Worker
180*4a64e381SAndroid Build Coastguard Worker## Conventions and Best Practices
181*4a64e381SAndroid Build Coastguard Worker
182*4a64e381SAndroid Build Coastguard Worker- Run `pylint` over your code. `pylint` is a tool for finding bugs and style problems in Python source code. It finds problems that are typically caught by a compiler for less dynamic languages like C and C++. Because of the dynamic nature of Python, some warnings may be incorrect; however, spurious warnings should be fairly infrequent.
183*4a64e381SAndroid Build Coastguard Worker
184*4a64e381SAndroid Build Coastguard Worker## Format and Style
185*4a64e381SAndroid Build Coastguard Worker
186*4a64e381SAndroid Build Coastguard Worker- All code should adhere to [PEP 8](https://www.python.org/dev/peps/pep-0008/).
187