HAProxyHAProxy coding style for contributions |
|
Mirror Sites: Master Language: English |
Quick linksQuick NewsRecent News Introduction Indentation Alignment Braces Line breaks Spaces Parenthesis NULL processing Syscall returns Declarations Macros Includes Comments Assembly Contacts Download Documentation Live demo They use it! Commercial Support Products using HAProxy Add-on features Other Solutions External links Mailing list archives 10GbE load-balancing (updated) Contributions Known bugs Web Based User Interface HATop: Ncurses Interface Willy TARREAU You want to donate ? ![]() |
IntroductionA number of contributors are often embarrassed with coding style issues, they don't always know if they're doing it right, especially since the coding style has elvoved along the years. What is explained here is not necessarily what is applied in the code, but new code should as much as possible conform to this style. Coding style fixes happen when code is replaced. It is useless to send patches to fix coding style only, they will be rejected, unless they belong to a patch series which needs these fixes prior to get code changes. Also, please avoid fixing coding style in the same patches as functional changes, they make code review harder.
A good way to quickly validate your patch before submitting it is to pass it
through the Linux kernel's
When modifying a file, you must accept the terms of the license of this file which is recalled at the top of the file, or is explained in the LICENSE file, or if not stated, defaults to LGPL version 2.1 or later for files in the include directory, and GPL version 2 or later for all other files. When adding a new file, you must add a copyright banner at the top of the file with your real name, e-mail address and a reminder of the license. Contributions under incompatible licenses or too restrictive licenses might get rejected. If in doubt, please apply the principle above for existing files. Tabs in this document will be represented as a series of 8 spaces so that it displays the same everywhere. 1) Indentation and alignment1.1) IndentationIndentation and alignment are two completely different things that people often get wrong. Indentation is used to mark a sub-level in the code. A sub-level means that a block is executed in the context of another block (eg: a function or a condition) :
In the example above, the code belongs to the Note that there are places where the code was not properly indented in the past. In order to view it correctly, you may have to set your tab size to 8 characters. 1.2) AlignmentAlignment is used to continue a line in a way to makes things easier to group together. By definition, alignment is character-based, so it uses spaces. Tabs would not work because for one tab there would not be as many characters on all displays. For instance, the arguments in a function declaration may be broken into multiple lines using alignment spaces :
In this example, the "
If we take again the example above marking tabs with "
It is worth noting that some editors tend to confuse indentations and aligment. Emacs is notoriously known for this brokenness, and is responsible for almost all of the alignment mess. The reason is that Emacs only counts spaces, tries to fill as many as possible with tabs and completes with spaces. Once you know it, you just have to be careful, as alignment is not used much, so generally it is just a matter of replacing the last tab with 8 spaces when this happens. Indentation should be used everywhere there is a block or an opening brace. It is not possible to have two consecutive closing braces on the same column, it means that the innermost was not indented. Right :
Wrong :
A special case applies to switch/case statements. Due to my editor's settings,
I've been used to align "
2) BracesBraces are used to delimit multiple-instruction blocks. In general it is preferred to avoid braces around single-instruction blocks as it reduces the number of lines : Right :
Wrong :
But it is not that strict, it really depends on the context. It happens from time to time that single-instruction blocks are enclosed within braces because it makes the code more symmetrical, or more readable. Example :
Braces are always needed to declare a function. A function's opening brace must be placed at the beginning of the next line : Right :
Wrong :
Note that a large portion of the code still does not conforms to this rule, as it took years to get all authors to adapt to this more common standard which is now preferred, as it avoids visual confusion when function declarations are broken on multiple lines : Right :
Wrong :
Braces should always be used where there might be an ambiguity with the code
later. The most common example is the stacked " Dangerous code waiting of a victim :
Wrong change :
It will do this instead of what your eye seems to tell you :
Right :
Similarly dangerous example :
Wrong change to silent the annoying message :
... which in fact means :
3) Breaking linesThere is no strict rule for line breaking. Some files try to stick to the 80 column limit, but given that various people use various tab sizes, it does not make much sense. Also, code is sometimes easier to read with less lines, as it represents less surface on the screen (since each new line adds its tabs and spaces). The rule is to stick to the average line length of other lines. If you are working in a file which fits in 80 columns, try to keep this goal in mind. If you're in a function with 120-chars lines, there is no reason to add many short lines, so you can make longer lines. In general, opening a new block should lead to a new line. Similarly, multiple instructions should be avoided on the same line. But some constructs make it more readable when those are perfectly aligned : A copy-paste bug in the following construct will be easier to spot :
than in this one :
What is important is not to mix styles. For instance there is nothing wrong
with having many one-line "
Otherwise, prefer to have the " Right :
Wrong :
Right :
or Right :
but Wrong :
When complex conditions or expressions are broken into multiple lines, please do ensure that alignment is perfectly appropriate, and group all main operators on the same side (which you're free to choose as long as it does not change for every block. Putting binary operators on the right side is preferred as it does not mangle with alignment but various people have their preferences. Right :
Right :
Wrong :
If it makes the result more readable, parenthesis may even be closed on their own line in order to align with the opening one. Note that should normally not be needed because such code would be too complex to be digged into. The " Right :
Right :
Right : |