1XL CODING STYLE
2========================
3
4
5AN APOLOGY AND WARNING
6----------------------
7
8Much of the code in xl does not yet follow this coding style
9document in every respect.  However, new code is expected to conform.
10
11Patches to improve the style of existing code are welcome.  Please
12separate these out from functional changes.
13
14If it is not feasible to conform fully to the style while patching old
15code, without doing substantial style reengineering first, we may
16accept patches which contain nonconformant elements, provided that
17they don't make the coding style problem worse overall.
18
19In this case, the new code should conform to the prevailing style in
20the area being touched.
21
22ERROR HANDLING
23--------------
24
25Unless, there are good reasons to do otherwise, the following error
26handling and cleanup paradigm should be used:
27
28  * All local variables referring to resources which might need
29    cleaning up are declared at the top of the function, and
30    initialised to a sentinel value indicating "nothing allocated".
31    For example,
32            libxl_evgen_disk_eject *evg = NULL;
33            int nullfd = -1;
34
35  * If the function is to return a libxl error value, `rc' is
36    used to contain the error code, but it is NOT initialised:
37            int rc;
38
39  * There is only one error cleanup path out of the function.  It
40    starts with a label `out:'.  That error cleanup path checks for
41    each allocated resource and frees it iff necessary.  It then
42    returns rc.  For example,
43         out:
44             if (evg) libxl__evdisable_disk_eject(gc, evg);
45             if (nullfd >= 0) close(nullfd);
46             return rc;
47
48  * Function calls which might fail (ie most function calls) are
49    handled by putting the return/status value into a variable, and
50    then checking it in a separate statement:
51            char *dompath = libxl__xs_get_dompath(gc, bl->domid);
52            if (!dompath) { rc = ERROR_FAIL; goto out; }
53
54  * If a resource is freed in the main body of the function (for
55    example, in a loop), the corresponding variable has to be reset to
56    the sentinel at the point where it's freed.
57
58Whether to use the `out' path for successful returns as well as error
59returns is a matter of taste and convenience for the specific
60function.
61
62If you reuse the `out' path for successful returns, there may be
63resources which are to be returned to the caller rather than freed.
64In that case you have to reset the local variable to `nothing here',
65to avoid the resource being freed on the out path.  That resetting
66should be done immediately after the resource value is stored at the
67applicable _r function parameter (or equivalent).  Do not test `rc' in
68the out section, to discover whether to free things.
69
70The uses of the single-line formatting in the examples above are
71permitted exceptions to the usual xl code formatting rules.
72
73FORMATTING AND NAMING
74---------------------
75
76Blatantly copied from qemu and linux with few modifications.
77
78
791. Whitespace
80
81Of course, the most important aspect in any coding style is whitespace.
82Crusty old coders who have trouble spotting the glasses on their noses
83can tell the difference between a tab and eight spaces from a distance
84of approximately fifteen parsecs.  Many a flamewar have been fought and
85lost on this issue.
86
87Xl indents are four spaces.  Tabs are never used, except in
88Makefiles where they have been irreversibly coded into the syntax.
89Spaces of course are superior to tabs because:
90
91 - You have just one way to specify whitespace, not two.  Ambiguity breeds
92   mistakes.
93 - The confusion surrounding 'use tabs to indent, spaces to justify' is gone.
94 - Tab indents push your code to the right, making your screen seriously
95   unbalanced.
96 - Tabs will be rendered incorrectly on editors who are misconfigured not
97   to use tab stops of eight positions.
98 - Tabs are rendered badly in patches, causing off-by-one errors in almost
99   every line.
100 - It is the xl coding style.
101
102Do not leave whitespace dangling off the ends of lines.
103
104
1052. Line width
106
107Lines are limited to 75-80 characters.
108
109Rationale:
110 - Some people like to tile their 24" screens with a 6x4 matrix of 80x24
111   xterms and use vi in all of them.  The best way to punish them is to
112   let them keep doing it.
113 - Code and especially patches is much more readable if limited to a sane
114   line length.  Eighty is traditional.
115 - It is the xl coding style.
116
117
1183. Naming
119
120C is a Spartan language, and so should your naming be.  Unlike Modula-2
121and Pascal programmers, C programmers do not use cute names like
122ThisVariableIsATemporaryCounter.  A C programmer would call that
123variable "tmp", which is much easier to write, and not the least more
124difficult to understand.
125
126HOWEVER, while mixed-case names are frowned upon, descriptive names for
127global variables are a must.  To call a global function "foo" is a
128shooting offense.
129
130GLOBAL variables (to be used only if you _really_ need them) need to
131have descriptive names, as do global functions.  If you have a function
132that counts the number of active users, you should call that
133"count_active_users()" or similar, you should _not_ call it "cntusr()".
134
135Encoding the type of a function into the name (so-called Hungarian
136notation) is brain damaged - the compiler knows the types anyway and can
137check those, and it only confuses the programmer.
138
139LOCAL variable names should be short, and to the point.  If you have
140some random integer loop counter, it should probably be called "i".
141Calling it "loop_counter" is non-productive, if there is no chance of it
142being mis-understood.  Similarly, "tmp" can be just about any type of
143variable that is used to hold a temporary value.
144
145Local variables used to store return values should have descriptive name
146like "rc" or "ret". Following the same reasoning the label used as exit
147path should be called "out".
148
149Function arguments which are used to return values to the caller
150should be suffixed `_r' or `_out'.
151
152Variables, type names and function names are
153lower_case_with_underscores.
154Xl should avoid using libxl_ and libxl__ as prefix for its own function
155names.
156
1574. Statements
158
159Don't put multiple statements on a single line.
160Don't put multiple assignments on a single line either.
161Error code paths with an if statement and a goto or a return on the same
162line are allowed. Examples:
163
164    if (rc) goto out;
165    if (rc < 0) return;
166
167Xl coding style is super simple.  Avoid tricky expressions.
168
169
1705. Block structure
171
172Every indented statement is braced, but blocks that contain just one
173statement may have the braces omitted.  To avoid confusion, either all
174the blocks in an if...else chain have braces, or none of them do.
175
176The opening brace is on the line that contains the control flow
177statement that introduces the new block; the closing brace is on the
178same line as the else keyword, or on a line by itself if there is no
179else keyword.  Examples:
180
181    if (a == 5) {
182        printf("a was 5.\n");
183    } else if (a == 6) {
184        printf("a was 6.\n");
185    } else {
186        printf("a was something else entirely.\n");
187    }
188
189    if (a == 5)
190        printf("a was 5.\n");
191
192An exception is the opening brace for a function; for reasons of tradition
193and clarity it comes on a line by itself:
194
195    void a_function(void)
196    {
197        do_something();
198    }
199
200Rationale: a consistent (except for functions...) bracing style reduces
201ambiguity and avoids needless churn when lines are added or removed.
202Furthermore, it is the xl coding style.
203