coding_style_en.md 6.3 KB
Newer Older
B
Bernard Xiong 已提交
1 2
                           RT-Thread Coding Style

3 4 5 6
This is an developing instruction for RT-Thread developers. As open source
software, RT-Thread is created by the cooperation of different people. This
document is a guide for RT-Thread developers. Please obey it as you develop.
RT-Thread users should also get to know some conventions in the code through it
B
Bernard Xiong 已提交
7 8 9 10 11
and thus easier to understand the implementations of RT-Thread.


1. Directory Naming

12
In normal conditions, please name directories in lowercase. Directories should
B
Bernard Xiong 已提交
13 14
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/
15
should name what the component does.
B
Bernard Xiong 已提交
16 17 18 19


2. File Naming

20
In normal conditions, please name files in lowercase. If the file is
B
Bernard Xiong 已提交
21 22 23 24 25 26 27 28 29
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

The symbol should begin and end with "__" to avoid naming collision. The words
36
of the file name should be connected by "_". (This convention is called "snake case".)
B
Bernard Xiong 已提交
37 38 39 40 41 42 43


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

5. Structure Defines

57
Please name structures in lowercase and connect words with "_". For example:
B
Bernard Xiong 已提交
58

59 60 61 62 63
    struct rt_list_node
    {
        struct rt_list_node *next;
        struct rt_list_node *prev;
    };
B
Bernard Xiong 已提交
64

65
Braces should have their own lines and the members should be indented.
B
Bernard Xiong 已提交
66

67
The names of type defines such as structure types should be the structure name
B
Bernard Xiong 已提交
68 69
plus "_t". For example:

70
    typedef struct rt_list_node rt_list_t;
B
Bernard Xiong 已提交
71 72


73
In order to be easily referenced, the types of objects in the kernel are pointers. For
B
Bernard Xiong 已提交
74 75
example:

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


79
6. Macros
B
Bernard Xiong 已提交
80

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

84
    #define RT_TRUE                         1
B
Bernard Xiong 已提交
85 86 87 88


7. Function Naming and Declaration

89
Please name functions in lowercase. Separate words with "_". The API provided to
B
Bernard Xiong 已提交
90 91 92
upper application should be declared in header files. If the function don't have
parameters, it should be declared as void:

93
    rt_thread_t rt_thread_self(void);
B
Bernard Xiong 已提交
94 95 96 97


8. Commenting

98 99 100 101
Please use English to comment. There shouldn't be many comments as the
comments should describe what the code does. It should describe complicated
algorithms, for example. Comments for statements should be placed before the
statements or to the right of them. Any other locations are invalid.
B
Bernard Xiong 已提交
102 103 104 105 106 107 108


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 "{":

109 110 111 112
    if (condition)
    {
        /* others */
    }
B
Bernard Xiong 已提交
113

114
The only one exception is switch. In case statements for switches, "case" should be
B
Bernard Xiong 已提交
115 116
aligned with "switch":

117 118 119 120 121
    switch (value)
    {
    case value1:
        break;
    }
B
Bernard Xiong 已提交
122

123
"case" is aligned with "switch". The following code block should be indented.
B
Bernard Xiong 已提交
124 125 126 127 128 129 130


10. Braces and Spaces

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

131 132 133 134
    if (condition)
    {
        /* others */
    }
B
Bernard Xiong 已提交
135

136
When matching braces have their own lines, the reader identifies the code
B
Bernard Xiong 已提交
137 138 139 140 141
blocks easily.

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

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

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

In expressions, there should be a space between most binary and ternary
153
operators and the strings. There should be no spaces around(inside) parentheses, like:
B
Bernard Xiong 已提交
154

155 156 157 158
    if ( x <= y )
    {
        /* other */
    }
B
Bernard Xiong 已提交
159 160 161 162

This is a bad practice.


163
11. trace, log Information
B
Bernard Xiong 已提交
164 165 166 167 168 169

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.

170 171
It is not recommended to use rt_kprintf frequently. Please be aware that frequen
use will make your code run slower.
B
Bernard Xiong 已提交
172

173
Logging should be off by default and can be turned on by a switch (e.g. a
174
variable or a macro). When logging, it should be easy to understand and easy to
B
Bernard Xiong 已提交
175 176 177 178 179
determine where the problem is.


12. Functions

180 181 182
Functions in kernel should be K.I.S.S. ("Keep it simple, stupid.") If the function
is too long, you should split it into smaller ones, with each of them simplified to
be easy to understand.
B
Bernard Xiong 已提交
183 184 185 186


13. Objects

187
The kernel of RT-Thread uses object-oriented techniques in C. The naming convention
B
Bernard Xiong 已提交
188 189 190
is: structure names are the object names, object names + verb phrases are the
method names of objects:

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

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

201 202 203 204 205 206
    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 已提交
207

208
rt_timer + verb phrase stands for the method that could be used on a timer object.
B
Bernard Xiong 已提交
209 210

When creating a new object, think twice on memory allocations: whether a static
211 212
object could be created or it could only created dynamically on the heap. Allocations
can be slower, but may be necessary in dynamic environments.
B
Bernard Xiong 已提交
213 214 215

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