coding_style_en.md 6.2 KB
Newer Older
B
Bernard Xiong 已提交
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
                           RT-Thread Coding Style

This is an developing instruction for RT-Thread developers. As an open source
software, RT-Thread is done by the cooperation of different people. This
document is a guide for RT-Thread developers and please obey it if you are.
RT-Thread users could also get to know some conventions in the code through it
and thus easier to understand the implementations of RT-Thread.


1. Directory Naming

In normal conditions, please name directories in lower-case. Directories should
have descriptive names. For example, the port of a chip should be composed of
the name of the chip and the category of the chip. Directories under components/
should stand for what the component does.


2. File Naming

In normal conditions, please name files in lower-case. If the file is
referencing other places, it can have the original name. To avoid naming
collision, do not use general names or the names that are frequently used.


3. Header Files

To avoid include the same header file for multiple times, you need to define a
symbol like this:

30 31 32 33
    #ifndef __FILE_H__
    #define __FILE_H__
    /* header file content */
    #endif
B
Bernard Xiong 已提交
34 35 36 37 38 39 40 41 42 43

The symbol should begin and end with "__" to avoid naming collision. The words
of the file name should be connected by "_".


4. Header File Comments

In every header file, there should be copyright information and Change Log
record like this:

44
    /*
45 46 47 48 49 50 51 52
    * Copyright (c) 2006-2020, RT-Thread Development Team
    *
    * SPDX-License-Identifier: Apache-2.0
    *
    * Change Logs:
    * Date           Author       Notes
    * 2006-03-18     Bernard      the first version
    * 2006-04-26     Bernard      add semaphore APIs
53
    */
B
Bernard Xiong 已提交
54 55 56 57 58

5. Structure Defines

Please name structures in lower-case and connect words with "_". For example:

59 60 61 62 63
    struct rt_list_node
    {
        struct rt_list_node *next;
        struct rt_list_node *prev;
    };
B
Bernard Xiong 已提交
64 65 66 67 68 69 70

Braces should have their own lines and the members should be defines with
indent.

The names of type defines such like structure types should be the structure name
plus "_t". For example:

71
    typedef struct rt_list_node rt_list_t;
B
Bernard Xiong 已提交
72 73 74 75 76


In order to be easily referenced, the types of objects in kernel is pointer. For
example:

77
    typedef struct rt_timer* rt_timer_t;
B
Bernard Xiong 已提交
78 79


80
6. Macros
B
Bernard Xiong 已提交
81

82
In RT-Thread, please use upper-case names for macro definitions. Words are
B
Bernard Xiong 已提交
83 84
connected by "_". Like:

85
    #define RT_TRUE                         1
B
Bernard Xiong 已提交
86 87 88 89 90 91 92 93


7. Function Naming and Declaration

Please name functions in lower-case and separate words with "_". API provided to
upper application should be declared in header files. If the function don't have
parameters, it should be declared as void:

94
    rt_thread_t rt_thread_self(void);
B
Bernard Xiong 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109


8. Commenting

Please use English to comment. There shouldn't be too much comments and the
comments should describe what does the code do. And it should describe how the
complicated algorithm works. Comments to statements should be placed before them
or right of them. Anther places are illegal.


9. Indent

Please use TAB or 4 spaces to indent. It's preferred to use 4 spaces. If no
other special meanings, the indent should begin right after "{":

110 111 112 113
    if (condition)
    {
        /* others */
    }
B
Bernard Xiong 已提交
114 115 116 117

The only one exception is switch. In switch-case statements, "case" should be
aligned with "switch":

118 119 120 121 122
    switch (value)
    {
    case value1:
        break;
    }
B
Bernard Xiong 已提交
123 124 125 126 127 128 129 130 131

"case" is aligned with "switch", the following code block should be indented.


10. Braces and Spaces

For ease of reading, it is advised that braces should occupy the whole line
instead of following other statements. Like:

132 133 134 135
    if (condition)
    {
        /* others */
    }
B
Bernard Xiong 已提交
136 137 138 139 140 141 142

When matching braces have their own lines, the reader would identify the code
blocks easily.

There should be a space before parentheses when it's not a function call. For
example:

143 144 145 146
    if (x <= y)
    {
        /* others */
    }
B
Bernard Xiong 已提交
147

148 149 150 151
    for (index = 0; index < MAX_NUMBER; index ++)
    {
        /* others */
    }
B
Bernard Xiong 已提交
152 153 154 155

In expressions, there should be a space between most binary and ternary
operators and the strings. No spaces around(inside) parentheses, like:

156 157 158 159
    if ( x <= y )
    {
        /* other */
    }
B
Bernard Xiong 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

This is a bad practice.


11. trace, log Informations

In RT-Thread, rt_kprintf is a commonly used logging routine. In RT-Thread
rt_kprintf is implemented as a polling, non-interrupting string output. It is
suitable in "instant" situations such as interrupt context. The polling method
would have influence to the timing sequence of the log output.

It is not recommended to use rt_kprintf frequently. Unless you are aware of that
it's not a big deal to run slower.

Logging should be off by default and can be turned on by a switch(e.g. a
175
variable or a macro). When logging, it should be easy to understand and easy to
B
Bernard Xiong 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
determine where the problem is.


12. Functions

Functions in kernel should be K.I.S.S. If the function is too long, you should
split it into smaller ones and make each of them simplified and easy to
understand.


13. Objects

The kernel of RT-Thread uses object-oriented tech in C. The naming convention
is: structure names are the object names, object names + verb phrases are the
method names of objects:

192 193 194 195 196 197
    struct rt_timer
    {
        struct rt_object parent;
        /* other fields */
    };
    typedef struct rt_timer* rt_timer_t;
B
Bernard Xiong 已提交
198 199 200 201

The definition of structure rt_timer stands for the object definition of timer
object.

202 203 204 205 206 207
    rt_timer_t rt_timer_create(const char* name,
        void (*timeout)(void* parameter), void* parameter,
        rt_tick_t time, rt_uint8_t flag);
    rt_err_t rt_timer_delete(rt_timer_t timer);
    rt_err_t rt_timer_start(rt_timer_t timer);
    rt_err_t rt_timer_stop(rt_timer_t timer);
B
Bernard Xiong 已提交
208 209 210 211 212 213 214 215

rt_timer + verb phrase stands for the method that could be used on timer object.

When creating a new object, think twice on memory allocations: whether a static
object could be created or it could only created dynamically on heap.

14. Use astyle to format the code automatically
parameters: --style=allman
216 217 218 219 220 221 222 223 224 225
            --indent=spaces=4
            --indent-preproc-block
            --pad-oper
            --pad-header
            --unpad-paren
            --suffix=none
            --align-pointer=name
            --lineend=linux
            --convert-tabs
            --verbose
B
Bernard Xiong 已提交
226