1*cfb92d14SAndroid Build Coastguard Worker# OpenThread Coding Conventions and Style 2*cfb92d14SAndroid Build Coastguard Worker 3*cfb92d14SAndroid Build Coastguard Worker- [1 C and C++](#c-and-c) 4*cfb92d14SAndroid Build Coastguard Worker - [1.1 Standards](#standards) 5*cfb92d14SAndroid Build Coastguard Worker - [1.2 Conventions and Best Practices](#conventions-and-best-practices) 6*cfb92d14SAndroid Build Coastguard Worker - [1.3 Tightly-constrained Systems and Shared Infrastructure](#tightly-constrained-systems-and-shared-infrastructure) 7*cfb92d14SAndroid Build Coastguard Worker - [1.4 Format and Style](#format-and-style) 8*cfb92d14SAndroid Build Coastguard Worker - [1.5 Comments](#comments) 9*cfb92d14SAndroid Build Coastguard Worker- [2 Python](#python) 10*cfb92d14SAndroid Build Coastguard Worker - [2.1 Standards](#standards) 11*cfb92d14SAndroid Build Coastguard Worker - [2.2 Conventions and Best Practices](#conventions-and-best-practices) 12*cfb92d14SAndroid Build Coastguard Worker - [2.3 Format and Style](#format-and-style) 13*cfb92d14SAndroid Build Coastguard Worker 14*cfb92d14SAndroid Build Coastguard Worker# C and C++ 15*cfb92d14SAndroid Build Coastguard Worker 16*cfb92d14SAndroid Build Coastguard Worker## Standards 17*cfb92d14SAndroid Build Coastguard Worker 18*cfb92d14SAndroid Build Coastguard Worker- C 19*cfb92d14SAndroid Build Coastguard Worker - OpenThread uses and enforces the ISO9899:1999 (aka ISO C99, C99) C language standard as the minimum. 20*cfb92d14SAndroid Build Coastguard Worker- C++ 21*cfb92d14SAndroid Build Coastguard Worker - OpenThread uses and enforces the ISO14882:2011 (aka ISO C++11, C++11) C++ language standard as the minimum. 22*cfb92d14SAndroid Build Coastguard Worker- Extensions 23*cfb92d14SAndroid 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*cfb92d14SAndroid Build Coastguard Worker 25*cfb92d14SAndroid Build Coastguard Worker## Conventions and Best Practices 26*cfb92d14SAndroid Build Coastguard Worker 27*cfb92d14SAndroid Build Coastguard Worker### Language Independent 28*cfb92d14SAndroid Build Coastguard Worker 29*cfb92d14SAndroid Build Coastguard Worker- Inline functions should be used judiciously. 30*cfb92d14SAndroid 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*cfb92d14SAndroid Build Coastguard Worker- Return Statements 32*cfb92d14SAndroid Build Coastguard Worker - There should be one return statement per free function or method at the end of the free function or method. 33*cfb92d14SAndroid Build Coastguard Worker- Non-local Goto 34*cfb92d14SAndroid Build Coastguard Worker - There should be no calls to the functions `setjmp` or `longjmp`. 35*cfb92d14SAndroid Build Coastguard Worker- Local Goto 36*cfb92d14SAndroid 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*cfb92d14SAndroid Build Coastguard Worker- C Preprocessor 38*cfb92d14SAndroid Build Coastguard Worker - Use of the C preprocessor should be limited to file inclusion and simple macros. 39*cfb92d14SAndroid 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*cfb92d14SAndroid 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*cfb92d14SAndroid 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*cfb92d14SAndroid Build Coastguard Worker - Preprocessor `#include` directives in a file shall only be preceded by other preprocessor directives or comments. 43*cfb92d14SAndroid 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*cfb92d14SAndroid Build Coastguard Worker - Preprocessor `#include` directives should use double quote (‘“‘) and (‘“‘) style for all private or relative headers. 45*cfb92d14SAndroid Build Coastguard Worker - Preprocessor `#include` directives should be grouped, ordered, or sorted as follows: 46*cfb92d14SAndroid Build Coastguard Worker - If the unit is a core/private header file, `"openthread-core-config.h"` should be the first header file included. 47*cfb92d14SAndroid Build Coastguard Worker - If the unit is a core/private `.c` or `.cpp` file: 48*cfb92d14SAndroid Build Coastguard Worker - If the unit has a corresponding header file, the unit's corresponding header file should be included before any other header file. 49*cfb92d14SAndroid Build Coastguard Worker - If the unit has no corresponding header file, then it should directly include `"openthread-core-config.h"` before any other header file. 50*cfb92d14SAndroid Build Coastguard Worker - C++ Standard Library headers 51*cfb92d14SAndroid Build Coastguard Worker - C Standard Library headers 52*cfb92d14SAndroid Build Coastguard Worker - Third-party library headers 53*cfb92d14SAndroid Build Coastguard Worker - First-party library headers 54*cfb92d14SAndroid Build Coastguard Worker - Private or local headers 55*cfb92d14SAndroid Build Coastguard Worker - Alphanumeric order within each subgroup 56*cfb92d14SAndroid Build Coastguard Worker - The preprocessor shall not be used to redefine reserved language keywords. 57*cfb92d14SAndroid 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. 58*cfb92d14SAndroid Build Coastguard Worker - Use of the preprocessor token concatenation operator '##' should be avoided. 59*cfb92d14SAndroid Build Coastguard Worker - The `undef` preprocessor directive should be avoided and shall never be used to undefine a symbol from a foreign module. 60*cfb92d14SAndroid Build Coastguard Worker- Object Scope 61*cfb92d14SAndroid Build Coastguard Worker - Data objects shall be declared at the smallest possible level of scope. 62*cfb92d14SAndroid 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. 63*cfb92d14SAndroid Build Coastguard Worker- Unbounded Recursion 64*cfb92d14SAndroid Build Coastguard Worker - There shall be no direct or indirect use of unbounded recursive function calls. 65*cfb92d14SAndroid Build Coastguard Worker- Symmetric APIs 66*cfb92d14SAndroid 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. 67*cfb92d14SAndroid Build Coastguard Worker- Use C stdint.h or C++ cstdint for Plain Old Data Types 68*cfb92d14SAndroid 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. 69*cfb92d14SAndroid Build Coastguard Worker- Constant Qualifiers 70*cfb92d14SAndroid 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. 71*cfb92d14SAndroid 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. 72*cfb92d14SAndroid Build Coastguard Worker- Header Include Guard 73*cfb92d14SAndroid Build Coastguard Worker - All C and C++ headers shall use preprocessor header include guards. 74*cfb92d14SAndroid 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. 75*cfb92d14SAndroid 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 (“\_”). 76*cfb92d14SAndroid Build Coastguard Worker- Function and Method Prototypes 77*cfb92d14SAndroid Build Coastguard Worker - All void functions or methods shall explicitly declare and specify the void type keyword. 78*cfb92d14SAndroid Build Coastguard Worker- Unused parameters 79*cfb92d14SAndroid Build Coastguard Worker - All unused parameters shall be declared as such using the `OT_UNUSED_VARIABLE` macro at the top of a function or method before all local variable declarations. 80*cfb92d14SAndroid Build Coastguard Worker 81*cfb92d14SAndroid Build Coastguard Worker### C 82*cfb92d14SAndroid Build Coastguard Worker 83*cfb92d14SAndroid Build Coastguard Worker- C / C++ Linkage Wrappers 84*cfb92d14SAndroid Build Coastguard Worker - All header files intended to have C symbol linkage shall use “extern C” linkage wrappers. 85*cfb92d14SAndroid Build Coastguard Worker 86*cfb92d14SAndroid Build Coastguard Worker### C++ 87*cfb92d14SAndroid Build Coastguard Worker 88*cfb92d14SAndroid Build Coastguard Worker- Prefer Passing Parameters by Reference to Pointer 89*cfb92d14SAndroid 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. 90*cfb92d14SAndroid Build Coastguard Worker- Passing Base Scalars 91*cfb92d14SAndroid 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. 92*cfb92d14SAndroid Build Coastguard Worker- Eliminate Unnecessary Destructors 93*cfb92d14SAndroid Build Coastguard Worker - The creation of empty or useless destructors should be avoided. Empty or useless destructors should be removed. 94*cfb92d14SAndroid Build Coastguard Worker- Default Parameters 95*cfb92d14SAndroid Build Coastguard Worker - When you declare C++ free functions and object methods, you should avoid or minimize using default parameters. 96*cfb92d14SAndroid Build Coastguard Worker - When you declare C++ virtual object methods, you shall avoid using default parameters. 97*cfb92d14SAndroid Build Coastguard Worker- Global and Scoped Static Construction 98*cfb92d14SAndroid 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. 99*cfb92d14SAndroid Build Coastguard Worker- C++-style Casts 100*cfb92d14SAndroid Build Coastguard Worker - Wherever possible and practical, C++ style casts should be used and preferred to the C style cast equivalent. 101*cfb92d14SAndroid Build Coastguard Worker- Avoid `using namespace` Statements in Headers 102*cfb92d14SAndroid Build Coastguard Worker - The C++ `using namespace` statement should not be used outside of object scope inside header files. 103*cfb92d14SAndroid Build Coastguard Worker 104*cfb92d14SAndroid Build Coastguard Worker## Tightly-constrained Systems and Shared Infrastructure 105*cfb92d14SAndroid Build Coastguard Worker 106*cfb92d14SAndroid Build Coastguard Worker- Heap-based resource allocation should be avoided. 107*cfb92d14SAndroid Build Coastguard Worker- There shall be no direct or indirect use of recursive function calls. 108*cfb92d14SAndroid Build Coastguard Worker- The use of virtual functions should be avoided. 109*cfb92d14SAndroid Build Coastguard Worker- The use of the C++ Standard Library shall be avoided. 110*cfb92d14SAndroid Build Coastguard Worker- The use of the C++ Standard Template Library (STL) should be avoided or minimized. 111*cfb92d14SAndroid Build Coastguard Worker- The use of the C++ templates should be avoided or minimized. 112*cfb92d14SAndroid Build Coastguard Worker- Code shall not use exceptions. 113*cfb92d14SAndroid Build Coastguard Worker- Code shall not use C++ runtime type information (RTTI), including facilities that rely upon it, such as `dynamic_cast` and `typeid`. 114*cfb92d14SAndroid Build Coastguard Worker 115*cfb92d14SAndroid Build Coastguard Worker## Format and Style 116*cfb92d14SAndroid Build Coastguard Worker 117*cfb92d14SAndroid Build Coastguard Worker- OpenThread uses `script/make-pretty` to reformat code and enforce code format and style. `script/make-pretty check` build target is included in OpenThread's continuous integration and must pass before a pull request is merged. 118*cfb92d14SAndroid Build Coastguard Worker 119*cfb92d14SAndroid 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 v0.31.0](https://github.com/google/yapf) for Python. 120*cfb92d14SAndroid Build Coastguard Worker 121*cfb92d14SAndroid Build Coastguard Worker### File Names 122*cfb92d14SAndroid Build Coastguard Worker 123*cfb92d14SAndroid 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. 124*cfb92d14SAndroid 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. 125*cfb92d14SAndroid Build Coastguard Worker- File names should be all lower case. 126*cfb92d14SAndroid Build Coastguard Worker- File extensions shall be indicative and appropriate for the type and usage of the source or header file. 127*cfb92d14SAndroid Build Coastguard Worker 128*cfb92d14SAndroid Build Coastguard Worker### Naming 129*cfb92d14SAndroid Build Coastguard Worker 130*cfb92d14SAndroid 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_. 131*cfb92d14SAndroid Build Coastguard Worker- Names shall not give any idea of type, such as is done with System Hungarian notation. 132*cfb92d14SAndroid Build Coastguard Worker- Case 133*cfb92d14SAndroid Build Coastguard Worker - C preprocessor symbols should be all uppercase. 134*cfb92d14SAndroid 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'. 135*cfb92d14SAndroid 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_. 136*cfb92d14SAndroid Build Coastguard Worker- Symbol Qualification 137*cfb92d14SAndroid Build Coastguard Worker - All OpenThread C public data types and free functions should have `ot` prepended to their name. 138*cfb92d14SAndroid Build Coastguard Worker - All OpenThread C++ code should be in the ‘ot’ top-level namespace. 139*cfb92d14SAndroid Build Coastguard Worker- Scope 140*cfb92d14SAndroid Build Coastguard Worker - All global data shall have a `g` prepended to the name to denote global scope. 141*cfb92d14SAndroid Build Coastguard Worker - All static data shall have a `s` prepended to the name to denote static scope. 142*cfb92d14SAndroid Build Coastguard Worker - All class or structure data members shall have a `m` prepended to the name to denote member scope. 143*cfb92d14SAndroid Build Coastguard Worker - All free function or method parameters should have an `a` prepended to the name to denote function parameter scope. 144*cfb92d14SAndroid Build Coastguard Worker - All variables that do not have such prefixes shall be assumed to be function local scope. 145*cfb92d14SAndroid Build Coastguard Worker 146*cfb92d14SAndroid Build Coastguard Worker### White Space 147*cfb92d14SAndroid Build Coastguard Worker 148*cfb92d14SAndroid Build Coastguard Worker- Indentation shall be 4 space characters. 149*cfb92d14SAndroid Build Coastguard Worker- Conditionals shall always appear on a separate line from the code to execute as a result of the condition. 150*cfb92d14SAndroid Build Coastguard Worker- Scoped Variable declarations 151*cfb92d14SAndroid 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. 152*cfb92d14SAndroid Build Coastguard Worker - There shall be an empty line after all such variable declarations. 153*cfb92d14SAndroid Build Coastguard Worker - The names of all variable declarations should be left aligned. 154*cfb92d14SAndroid Build Coastguard Worker- Data Member declarations 155*cfb92d14SAndroid Build Coastguard Worker - All data member declarations should be placed together. 156*cfb92d14SAndroid Build Coastguard Worker - The names of all data member declarations should be left aligned. 157*cfb92d14SAndroid Build Coastguard Worker - The data member declarations for C++ classes should be placed at the end or tail of the class. 158*cfb92d14SAndroid Build Coastguard Worker- Braces 159*cfb92d14SAndroid Build Coastguard Worker - Braces should go on their own lines. 160*cfb92d14SAndroid Build Coastguard Worker - Statements should never be on the same line following a closing brace. 161*cfb92d14SAndroid Build Coastguard Worker- Keywords 162*cfb92d14SAndroid Build Coastguard Worker - There should be a single space after language-reserved keywords (for, while, if, etc). 163*cfb92d14SAndroid Build Coastguard Worker 164*cfb92d14SAndroid Build Coastguard Worker## Comments 165*cfb92d14SAndroid Build Coastguard Worker 166*cfb92d14SAndroid Build Coastguard Worker- All code should use Doxygen to: 167*cfb92d14SAndroid Build Coastguard Worker - Detail what the various source and header files are and how they fit into the broader context. 168*cfb92d14SAndroid Build Coastguard Worker - Detail what the various C++ namespaces are. 169*cfb92d14SAndroid Build Coastguard Worker - Detail what the constants, C preprocessor definitions, and enumerations are. 170*cfb92d14SAndroid Build Coastguard Worker - Detail what the globals are and how they are to be used. 171*cfb92d14SAndroid 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. 172*cfb92d14SAndroid 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. 173*cfb92d14SAndroid Build Coastguard Worker- Every public, and ideally private, free function and class method should likewise have a prologue comment that: 174*cfb92d14SAndroid Build Coastguard Worker - Briefly describes what it is and what it does. 175*cfb92d14SAndroid Build Coastguard Worker - Describes in detail, optionally, what it is and what it does. 176*cfb92d14SAndroid Build Coastguard Worker - Describes the purpose, function, and influence of each parameter as well as whether it is an input, an output, or both. 177*cfb92d14SAndroid Build Coastguard Worker - Describes the return value, if present, and the expected range or constraints of it. 178*cfb92d14SAndroid Build Coastguard Worker 179*cfb92d14SAndroid Build Coastguard WorkerIf your description is longer than 120 characters, continue the comment on the next line: 180*cfb92d14SAndroid Build Coastguard Worker 181*cfb92d14SAndroid Build Coastguard Worker``` 182*cfb92d14SAndroid Build Coastguard Worker * @brief 183*cfb92d14SAndroid Build Coastguard Worker * Records the history of different events, for example RX and TX messages or network info changes. All tracked 184*cfb92d14SAndroid Build Coastguard Worker * entries are timestamped. 185*cfb92d14SAndroid Build Coastguard Worker``` 186*cfb92d14SAndroid Build Coastguard Worker 187*cfb92d14SAndroid Build Coastguard Worker# Python 188*cfb92d14SAndroid Build Coastguard Worker 189*cfb92d14SAndroid Build Coastguard Worker## Standards 190*cfb92d14SAndroid Build Coastguard Worker 191*cfb92d14SAndroid Build Coastguard Worker- OpenThread uses and enforces Python 3. 192*cfb92d14SAndroid Build Coastguard Worker 193*cfb92d14SAndroid Build Coastguard Worker## Conventions and Best Practices 194*cfb92d14SAndroid Build Coastguard Worker 195*cfb92d14SAndroid 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. 196*cfb92d14SAndroid Build Coastguard Worker 197*cfb92d14SAndroid Build Coastguard Worker## Format and Style 198*cfb92d14SAndroid Build Coastguard Worker 199*cfb92d14SAndroid Build Coastguard Worker- All code should adhere to [Google Python Style Guide](https://google.github.io/styleguide/pyguide.html) except maximum line length being 119. 200