summaryrefslogtreecommitdiffstats
path: root/coding_style.md
blob: 584dac78f3ed843522c7cc4c683f596ebf0c870b (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
XRdp Coding Style
=================

The coding style used by XRdp is described below.

The XRdp project uses astyle (artistic style) to format the code. Astyle
requires a configuration file that describes how you want your code
formatted. This file is present in the XRdp root directory and is named
`astyle_config.as`.

Here is how we run the astyle command:

    astyle --options=/path/to/file/astyle_config.as "*.c"

This coding style is a work in progress and is still evolving.


Indentation
-----------

* 4 spaces per indent
* No tabs for any indents

☞

    if (fd < 0)
    {
        return -1;
    }


Line wrapping
-------------

* Keep lines shorter than 80 chars
* Align wrapped argument to the first argument

☞

    log_message("connection aborted: error %d",
                ret);


Variable names
--------------

* Use lowercase with underscores as needed
* Don't use camelCase

☞

    int fd;
    int bytes_in_stream;


Variable declaration
--------------------

* Each variable is declared on a separate line

☞

    int i;
    int j;


Whitespace
----------

* Use blank lines to group statements
* Use at most one empty line between statements
* For pointers and references, use a single space before * or & but not after
* Use one space after a cast
* No space before square brackets

☞

    char *cptr;
    int *iptr;
    cptr = (char *) malloc(1024);

    write(fd, &buf[12], 16);


Function declarations
---------------------

* Use newline before function name

☞

    static int
    value_ok(int val)
    {
        return (val >= 0);
    }


Braces
------

* Opening brace is always on a separate line
* Align braces with the line preceding the opening brace

☞

    struct stream
    {
        int flags;
        char *data;
    };

    void
    process_data(struct stream *s)
    {
        if (stream == NULL)
        {
            return;
        }
    }


`if` statements
---------------

* Always use braces
* Put both braces on separate lines

☞

    if (val <= 0xff)
    {
        out_uint8(s, val);
    }
    else if (val <= 0xffff)
    {
        out_uint16_le(s, val);
    }
    else
    {
        out_uint32_le(s, val);
    }


`for` statements
----------------

* Always use braces
* Put both braces on separate lines

☞

    for (i = 0; i < 10; i++)
    {
        printf("i = %d\n", i);
    }


`while` and `do while` statements
---------------------------------

* Always use braces
* `while` after the closing brace is on the same line

☞

    while (cptr)
    {
        cptr—;
    }

    do
    {
        cptr--;
    } while (cptr);


`switch` statements
-------------------

* Indent `case` once
* Indent statements under `case` one more time
* Put both braces on separate lines

☞

    switch (cmd)
    {
        case READ:
            read(fd, buf, 1024);
            break;

        default:
            printf("bad cmd\n");
    }