root / CODING_STYLE @ 075e36b8
History | View | Annotate | Download (2.7 kB)
1 | e68b98dc | aliguori | Qemu Coding Style |
---|---|---|---|
2 | e68b98dc | aliguori | ================= |
3 | e68b98dc | aliguori | |
4 | e68b98dc | aliguori | 1. Whitespace |
5 | e68b98dc | aliguori | |
6 | e68b98dc | aliguori | Of course, the most important aspect in any coding style is whitespace. |
7 | e68b98dc | aliguori | Crusty old coders who have trouble spotting the glasses on their noses |
8 | e68b98dc | aliguori | can tell the difference between a tab and eight spaces from a distance |
9 | e68b98dc | aliguori | of approximately fifteen parsecs. Many a flamewar have been fought and |
10 | e68b98dc | aliguori | lost on this issue. |
11 | e68b98dc | aliguori | |
12 | e68b98dc | aliguori | QEMU indents are four spaces. Tabs are never used, except in Makefiles |
13 | 1cb499fa | edgar_igl | where they have been irreversibly coded into the syntax. |
14 | e68b98dc | aliguori | Spaces of course are superior to tabs because: |
15 | e68b98dc | aliguori | |
16 | e68b98dc | aliguori | - You have just one way to specify whitespace, not two. Ambiguity breeds |
17 | e68b98dc | aliguori | mistakes. |
18 | e68b98dc | aliguori | - The confusion surrounding 'use tabs to indent, spaces to justify' is gone. |
19 | e68b98dc | aliguori | - Tab indents push your code to the right, making your screen seriously |
20 | e68b98dc | aliguori | unbalanced. |
21 | e68b98dc | aliguori | - Tabs will be rendered incorrectly on editors who are misconfigured not |
22 | e68b98dc | aliguori | to use tab stops of eight positions. |
23 | e68b98dc | aliguori | - Tabs are rendered badly in patches, causing off-by-one errors in almost |
24 | e68b98dc | aliguori | every line. |
25 | e68b98dc | aliguori | - It is the QEMU coding style. |
26 | e68b98dc | aliguori | |
27 | e68b98dc | aliguori | Do not leave whitespace dangling off the ends of lines. |
28 | e68b98dc | aliguori | |
29 | e68b98dc | aliguori | 2. Line width |
30 | e68b98dc | aliguori | |
31 | e68b98dc | aliguori | Lines are 80 characters; not longer. |
32 | e68b98dc | aliguori | |
33 | e68b98dc | aliguori | Rationale: |
34 | e68b98dc | aliguori | - Some people like to tile their 24" screens with a 6x4 matrix of 80x24 |
35 | e68b98dc | aliguori | xterms and use vi in all of them. The best way to punish them is to |
36 | e68b98dc | aliguori | let them keep doing it. |
37 | e68b98dc | aliguori | - Code and especially patches is much more readable if limited to a sane |
38 | e68b98dc | aliguori | line length. Eighty is traditional. |
39 | e68b98dc | aliguori | - It is the QEMU coding style. |
40 | e68b98dc | aliguori | |
41 | e68b98dc | aliguori | 3. Naming |
42 | e68b98dc | aliguori | |
43 | e68b98dc | aliguori | Variables are lower_case_with_underscores; easy to type and read. Structured |
44 | e68b98dc | aliguori | type names are in CamelCase; harder to type but standing out. Scalar type |
45 | e68b98dc | aliguori | names are lower_case_with_underscores_ending_with_a_t, like the POSIX |
46 | e68b98dc | aliguori | uint64_t and family. Note that this last convention contradicts POSIX |
47 | e68b98dc | aliguori | and is therefore likely to be changed. |
48 | e68b98dc | aliguori | |
49 | e68b98dc | aliguori | Typedefs are used to eliminate the redundant 'struct' keyword. It is the |
50 | e68b98dc | aliguori | QEMU coding style. |
51 | e68b98dc | aliguori | |
52 | e68b98dc | aliguori | 4. Block structure |
53 | e68b98dc | aliguori | |
54 | e68b98dc | aliguori | Every indented statement is braced; even if the block contains just one |
55 | e68b98dc | aliguori | statement. The opening brace is on the line that contains the control |
56 | e68b98dc | aliguori | flow statement that introduces the new block; the closing brace is on the |
57 | e68b98dc | aliguori | same line as the else keyword, or on a line by itself if there is no else |
58 | e68b98dc | aliguori | keyword. Example: |
59 | e68b98dc | aliguori | |
60 | e68b98dc | aliguori | if (a == 5) { |
61 | e68b98dc | aliguori | printf("a was 5.\n"); |
62 | e68b98dc | aliguori | } else if (a == 6) { |
63 | e68b98dc | aliguori | printf("a was 6.\n"); |
64 | e68b98dc | aliguori | } else { |
65 | e68b98dc | aliguori | printf("a was something else entirely.\n"); |
66 | e68b98dc | aliguori | } |
67 | e68b98dc | aliguori | |
68 | e68b98dc | aliguori | An exception is the opening brace for a function; for reasons of tradition |
69 | e68b98dc | aliguori | and clarity it comes on a line by itself: |
70 | e68b98dc | aliguori | |
71 | e68b98dc | aliguori | void a_function(void) |
72 | e68b98dc | aliguori | { |
73 | e68b98dc | aliguori | do_something(); |
74 | e68b98dc | aliguori | } |
75 | e68b98dc | aliguori | |
76 | e68b98dc | aliguori | Rationale: a consistent (except for functions...) bracing style reduces |
77 | e68b98dc | aliguori | ambiguity and avoids needless churn when lines are added or removed. |
78 | e68b98dc | aliguori | Furthermore, it is the QEMU coding style. |