summaryrefslogtreecommitdiff
path: root/docs/sections/coding_guidelines.md
blob: f1813d3b49f360e49579378ae40116eb3447bf1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# Coding standards and guidelines

## Contents

- [Introduction](#introduction)
- [Language version](#language-version)
- [File naming](#file-naming)
- [File layout](#file-layout)
- [Block Management](#block-management)
- [Naming Conventions](#naming-conventions)
  - [C++ language naming conventions](#c_language-naming-conventions)
  - [C language naming conventions](#c-language-naming-conventions)
- [Layout and formatting conventions](#layout-and-formatting-conventions)
- [Language usage](#language-usage)

## Introduction

This document presents some standard coding guidelines to be followed for contributions to this repository. Most of the
code is written in C++, but there is some written in C as well. There is a clear C/C++ boundary at the Hardware
Abstraction Layer (HAL). Both these languages follow different naming conventions within this repository, by design, to:

- have clearly distinguishable C and C++ sources.
- make cross language function calls stand out. Mostly these will be C++ function calls to the HAL functions written in C.
However, because we also issue function calls to third party API's (and they may not follow these conventions), the
intended outcome may not be fully realised in all of the cases.

## Language version

For this project, code written in C++ shall use a subset of the C++11 feature set and software
may be written using the C++11 language standard. Code written in C should be compatible
with the C99 standard.

Software components written in C/C++ may use the language features allowed and encouraged by this documentation.

## File naming

- C files should have `.c` extension
- C++ files should have `.cc` or `.cpp` extension.
- Header files for functions implemented in C should have `.h` extension.
- Header files for functions implemented in C++ should have `.hpp` extension.

## File layout

- Standard copyright notice must be included in all files:

  ```copyright
  /*
  * Copyright (c) <years additions were made to project> <your name>, Arm Limited. All rights reserved.
  * SPDX-License-Identifier: Apache-2.0
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
  ```

- Source lines must be no longer than 120 characters. Prefer to spread code out vertically rather than horizontally,
  wherever it makes sense:

  ```C++
  # This is significantly easier to read
  enum class SomeEnum1
  {
      ENUM_VALUE_1,
      ENUM_VALUE_2,
      ENUM_VALUE_3
  };

  # than this
  enum class SomeEnum2 { ENUM_VALUE_1, ENUM_VALUE_2, ENUM_VALUE_3 };
  ```

- Block indentation should use 4 characters, no tabs.

- Each statement must be on a separate line.

  ```C++
  int a, b; // Error prone
  int c, *d;

  int e = 0; // GOOD
  int *p = nullptr; // GOOD
  ```

- Source must not contain commented out code or unreachable code

## Block Management

- Blocks must use braces and braces location must be consistent.
  - Each function has its opening brace at the next line on the same indentation level as its header, the code within
  the braces is indented and the closing brace at the end is on the same level as the opening.
  For compactness, if the class/function body is empty braces are accepted on the same line.

  - Conditional statements and loops, even if are just single-statement body, needs to be surrounded by braces, the
opening brace is at the same line, the closing brace is at the next line on the same indentation level as its header;
the same rule is applied to classes.

    ```C++
    class Class1 {
    public:
        Class1();
    private:
        int element;
    };

    void NotEmptyFunction()
    {
        if (condition) {
            // [...]
        } else {
            // [...]
        }
        // [...]
        for(start_cond; end_cond; step_cond) {
            // [...]
        }
    }

    void EmptyFunction() {}
    ```

  - Cases within switch are indented and enclosed in brackets:

    ```C++
    switch (option)
    {
        case 1:
        {
            // handle option 1
            break;
        }
        case 2:
        {
            // handle option 2
            break;
        }
        default:
        {
            break;
        }
    }
    ```

## Naming Conventions

### C++ language naming conventions

- Type (class, struct, enum) names must be `PascalCase`:

  ```C++
  class SomeClass
  {
      // [...]
  };
  void SomeFunction()
  {
      // [...]
  }
  ```

- Variables and parameter names must be `camelCase`:

  ```C++
  int someVariable;

  void SomeFunction(int someParameter) {}
  ```

- Macros, pre-processor definitions, and enumeration values should use upper case names:

  ```C++
  #define SOME_DEFINE

  enum class SomeEnum
  {
      ENUM_VALUE_1,
      ENUM_VALUE_2
  };
  ```

- Namespace names must be lower case

  ```C++
  namespace nspace
  {
  void FunctionInNamespace();
  };
  ```

- Source code should use Hungarian notation to annotate the name of a variable with information about its meaning.

  | Prefix | Class | Description |
  | ------ | ----- | ----------- |
  | p | Type      | Pointer to any other type |
  | k | Qualifier | Constant |
  | v | Qualifier | Volatile |
  | m | Scope     | Member of a class or struct |
  | s | Scope     | Static |
  | g | Scope     | Used to indicate variable has scope beyond the current function: file-scope or externally visible scope|

The following examples  of Hungarian notation are one possible set of uses:

  ```C++
  int g_GlobalInt=123;
  char* m_pNameOfMemberPointer=nullptr;
  const float g_kSomeGlobalConstant = 1.234f;
  static float ms_MyStaticMember =  4.321f;
  bool myLocalVariable=true;
  ```

### C language naming conventions

For C sources, we follow the Linux variant of the K&R style wherever possible.

- For function and variable names we use `snake_case` convention:

  ```C
  int some_variable;

  void some_function(int some_parameter) {}
  ```

- Macros, pre-processor definitions, and enumeration values should use upper case names:

  ```C
  #define SOME_DEFINE

  enum some_enum
  {
      ENUM_VALUE_1,
      ENUM_VALUE_2
  };
  ```

## Layout and formatting conventions

- C++ class code layout
  Public function definitions should be at the top of a class definition, since they are things most likely to be used
by other people.
  Private functions and member variables should be last.
  Class functions and member variables should be laid out logically in blocks of related functionality.

- Class  inheritance keywords are not indented.

  ```C++
  class MyClass
  {
  public:
    int m_PublicMember;
  protected:
    int m_ProtectedMember;
  private:
    int m_PrivateMember;
  };
  ```

- Don't leave trailing spaces at the end of lines.

- Empty lines should have no trailing spaces.

- For pointers and references, the symbols `*` and `&` should be adjacent to the name of the type, not the name
  of the variable.

  ```C++
  char* someText = "abc";

  void SomeFunction(const SomeObject& someObject) {}
  ```

## Language usage

- Header `#include` statements should be minimized.
  Inclusion of unnecessary headers slows down compilation, and can hide errors where a function calls a
  subroutine which it should not be using if the unnecessary header defining this subroutine is included.

  Header statements should be included in the following order:

  - Header file corresponding to the current source file (if applicable)
  - Headers from the same component
  - Headers from other components
  - Third-party headers
  - System headers

  > **Note:** Leave one blank line between each of these groups for readability.
  >Use quotes for headers from within the same project and angle brackets for third-party and system headers.
  >Do not use paths relative to the current source file, such as `../Header.hpp`. Instead configure your include paths
>in the project makefiles.

  ```C++
  #include "ExampleClass.hpp"     // Own header

  #include "Header1.hpp"          // Header from same component
  #include "Header1.hpp"          // Header from same component

  #include "other/Header3.hpp"    // Header from other component

  #include <ThirdParty.hpp>       // Third-party headers

  #include <vector>               // System  header

  // [...]
  ```

- C++ casts should use the template-styled case syntax

  ```C++
  int a = 100;
  float b = (float)a; // Not OK
  float c = static_cast<float>(a); // OK
  ```

- Use the const keyword to declare constants instead of define.

- Should use `nullptr` instead of `NULL`,
  C++11 introduced the `nullptr` type to distinguish null pointer constants from the integer 0.