3 Source code that has a common style is easier to read than code that uses
4 different styles in different places. It helps making the code feel like one
5 single code base. Easy-to-read is a very important property of code and helps
6 making it easier to review when new things are added and it helps debugging
7 code when developers are trying to figure out why things go wrong. A unified
8 style is more important than individual contributors having their own personal
11 Our C code has a few style rules. Most of them are verified and upheld by the
12 `lib/checksrc.pl` script. Invoked with `make checksrc` or even by default by
13 the build system when built after `./configure --enable-debug` has been used.
15 It is normally not a problem for anyone to follow the guidelines, as you just
16 need to copy the style already used in the source code and there are no
17 particularly unusual rules in our set of rules.
19 We also work hard on writing code that are warning-free on all the major
20 platforms and in general on as many platforms as possible. Code that obviously
21 will cause warnings will not be accepted as-is.
25 Try using a non-confusing naming scheme for your new functions and variable
26 names. It doesn't necessarily have to mean that you should use the same as in
27 other places of the code, just that the names should be logical,
28 understandable and be named according to what they're used for. File-local
29 functions should be made static. We like lower case names.
31 See the [INTERNALS](INTERNALS.md) document on how we name non-exported
32 library-global symbols.
36 We use only spaces for indentation, never TABs. We use two spaces for each new
39 if(something_is_true) {
40 while(second_statement == fine) {
47 Since we write C89 code, `//` comments are not allowed. They weren't
48 introduced in the C standard until C99. We use only `/*` and `*/` comments:
50 /* this is a comment */
54 Source code in curl may never be wider than 79 columns and there are two
55 reasons for maintaining this even in the modern era of very large and high
58 1. Narrower columns are easier to read than very wide ones. There's a reason
59 newspapers have used columns for decades or centuries.
61 2. Narrower columns allow developers to easier show multiple pieces of code
62 next to each other in different windows. I often have two or three source
63 code windows next to each other on the same screen - as well as multiple
64 terminal and debugging windows.
68 In if/while/do/for expressions, we write the open brace on the same line as
69 the keyword and we then set the closing brace on the same indentation level as
70 the initial keyword. Like this:
73 /* clearly a youngster */
76 You may omit the braces if they would contain only a one-line statement:
81 For functions the opening brace should be on a separate line:
83 int main(int argc, char **argv)
88 ## 'else' on the following line
90 When adding an `else` clause to a conditional expression using braces, we add
91 it on a new line after the closing brace. Like this:
94 /* clearly a youngster */
100 ## No space before parentheses
102 When writing expressions using if/while/do/for, there shall be no space
103 between the keyword and the open parenthesis. Like this:
109 ## Use boolean conditions
111 Rather than test a conditional value such as a bool against TRUE or FALSE, a
112 pointer against NULL or != NULL and an int against zero or not zero in
113 if/while conditions we prefer:
115 result = do_something();
117 /* something went wrong */
121 ## No assignments in conditions
123 To increase readability and reduce complexity of conditionals, we avoid
124 assigning variables within if/while conditions. We frown upon this style:
126 if((ptr = malloc(100)) == NULL)
129 and instead we encourage the above version to be spelled out more clearly:
135 ## New block on a new line
137 We never write multiple statements on the same source line, even for very
138 short if() conditions.
148 else if(b) return FALSE;
150 ## Space around operators
152 Please use spaces on both sides of operators in C expressions. Postfix `(),
153 [], ->, ., ++, --` and Unary `+, - !, ~, &` operators excluded they should
162 size += -2 + 3 * (a + b);
168 empty = (!*string) ? TRUE : FALSE;
172 Some statements cannot be completed on a single line because the line would
173 be too long, the statement too hard to read, or due to other style guidelines
174 above. In such a case the statement will span multiple lines.
176 If a continuation line is part of an expression or sub-expression then you
177 should align on the appropriate column so that it's easy to tell what part of
178 the statement it is. Operators should not start continuation lines. In other
179 cases follow the 2-space indent guideline. Here are some examples from libcurl:
182 if(Curl_pipeline_wanted(handle->multi, CURLPIPE_HTTP1) &&
183 (handle->set.httpversion != CURL_HTTP_VERSION_1_0) &&
184 (handle->set.httpreq == HTTPREQ_GET ||
185 handle->set.httpreq == HTTPREQ_HEAD))
186 /* didn't ask for HTTP/1.0 and a GET or HEAD */
191 case CURLOPT_KEEP_SENDING_ON_ERROR:
192 data->set.http_keep_sending_on_error = (0 != va_arg(param, long)) ?
198 data->set.http_disable_hostname_check_before_authentication =
199 (0 != va_arg(param, long)) ? TRUE : FALSE;
204 result = parse_login_details(option, strlen(option),
205 (userp ? &user : NULL),
206 (passwdp ? &passwd : NULL),
212 DEBUGF(infof(data, "Curl_pp_readresp_ %d bytes of trailing "
213 "server response left\n",
217 ## Platform dependent code
219 Use `#ifdef HAVE_FEATURE` to do conditional code. We avoid checking for
220 particular operating systems or hardware in the #ifdef lines. The HAVE_FEATURE
221 shall be generated by the configure script for unix-like systems and they are
222 hard-coded in the config-[system].h files for the others.
224 We also encourage use of macros/functions that possibly are empty or defined
225 to constants when libcurl is built without that feature, to make the code
226 seamless. Like this style where the `magic()` function works differently
227 depending on a build-time conditional:
238 int content = magic(3);