| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- Dpkg C coding style 2012-05-24
- ===================
- C language extensions
- ~~~~~~~~~~~~~~~~~~~~~
- The code base assumes C89 plus the following C99 extensions:
- * Designated initializers.
- * Compound literals.
- * Trailing comma in enum.
- * Variadic macros.
- * Working bool type in <stdbool.h>.
- * Working %j and %z printf modifiers.
- * Magic __func__ variable.
- Those are checked at build time, and it will abort in case a needed extension
- is not supported.
- General
- ~~~~~~~
- The coding style is a mix of parts from KNF [K] and the Linux CodingStyle [L].
- If in doubt or missing from this file please ask, although files using the
- tab based indentation can be considered canon.
- [K] <https://www.freebsd.org/cgi/man.cgi?query=style>
- [L] <https://www.kernel.org/doc/Documentation/CodingStyle>
- The code has a mix of an old coding style being phased out and the new
- style. New files should use the new style, changes to files with the old
- style should switch the code being touched except for the indentation level,
- which should be preserved to match (2 spaces).
- Code should generally strive for clarity. Monster functions should be split
- into logical and small pieces.
- Variable and function names should be generally descriptive, not needed
- for variables commonly used (for example an index inside a loop, etc),
- acronyms should only be used if they are widely known externally or
- inside the project. The names should separate logical concepts within
- with underscores.
- On comments use UTF-8 characters for quotes, copyright symbols, etc.
- On strings in code use simple or double quotes «''» «""». Not the unpaired
- ones «`'». Strings marked for translation, should only be fixed if there's
- other changes to be done on them, otherwise we get unneeded fuzzies.
- <https://www.cl.cam.ac.uk/~mgk25/ucs/quotes.html>
- Code documentation
- ~~~~~~~~~~~~~~~~~~
- Public declarations should be documented using JavaDoc style comments.
- Documentation should always be close to its definition (usually in the .c
- or .cc files) and not its declaration, so that when the code changes it's
- less likely that they will get out of sync. For data types, macros and
- similar where there's only declarations, the documentation will usually
- go instead in the header files.
- For enum values and struct members, the documentation should usually be
- one-line comments, preceding the item.
- The comment title should be on the second line on its own, and the long
- description on its own paragraph.
- Examples:
- /**
- * This is the enum title.
- */
- enum value_list {
- /** Value doing foo. */
- VALUE_A,
- /** Value doing bar. */
- VALUE_B,
- };
- /**
- * This is the title.
- *
- * This is the long description extending several lines, explaining in
- * detail what this item does.
- *
- * @param a This is the a parameter.
- * @param b This is the b parameter.
- *
- * @return This is the return value.
- */
- Indentation, alignment and spacing
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Lines should be 80 chars max. Indentation is done with hard tabs (which
- should be considered to take 8 spaces width). Aligning with spaces:
- static void
- function(void *ptr, int value)
- {
- void *ref_ptr = get_ref(ptr);
- int ref_value = get_value(ref);
- if (value > 10)
- do_something(GLOBAL_MACRO, ptr, value, "some-string",
- ref_ptr, ref_value, "other-string",
- "extra-string");
- }
- When wrapping, logical operators should be kept on the preceding line:
- if (really_long_variable_to_be_checked_against_a &&
- really_long_variable_to_be_checked_against_b)
- foo();
- Spaces between operators:
- if (a && (b || c) && c == d)
- break;
- a = (b + 4 * (5 - 6)) & 0xff;
- Spaces between assignments:
- a += b;
- Spaces after comma:
- foo(a, b);
- Space after keywords (for, while, do, if, etc, but sizeof should be
- treated like a function):
- for (i = 0; i < 10; i++)
- foo(i);
- memcpy(dst, src, sizeof(src));
- Definition of local variables, related code blocks, functions bodies
- should be split with blank lines:
- int
- function(void)
- {
- int a;
- foo();
- bar();
- quux();
- return 0;
- }
- Braces
- ~~~~~~
- Braces should be placed on the same line as the keyword, but on a new line
- for the function body. No braces should be used for unambiguous one line
- statements:
- if (a > 0) {
- foo(a);
- bar(a);
- } else {
- quux(0)
- bar(0);
- }
- for (;;) {
- foo();
- bar();
- }
- do {
- foo();
- bar();
- } while (quux());
- switch (foo) {
- case a:
- bar();
- break;
- case b:
- default:
- baz();
- break;
- }
- Code conventions
- ~~~~~~~~~~~~~~~~
- Prefer assigning outside of conditionals:
- n = read_items();
- if (n < 100)
- foo();
- String comparisons should use comparison operators to make it easier to
- see what operation is being done:
- if (strcmp(a, b) == 0)
- foo();
- if (strcmp(a, b) < 0)
- foo();
- Dpkg Perl coding style 2015-05-20
- ======================
- General
- ~~~~~~~
- In general you should follow the conventions listed in perlstyle(1).
- All the code should run with the “use strict” and “use warnings” pragmas.
- Code documentation
- ~~~~~~~~~~~~~~~~~~
- Public modules should be documented with POD (see perlpod(1)). Private
- code doesn't have to use POD, simple comment lines (starting with "#") are
- enough. Public scripts are documented in their corresponding manual pages.
- Indentation, alignment and spacing
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Lines should be 80 chars max. The indentation level is 4 characters, and
- indentation is done with hard tabs (which should be considered to take 8
- spaces width) and spaces.
- if ($foo) {
- if ($bar) {
- print "Hello\n";
- unless ($baz) {
- print "Who are you?\n";
- }
- }
- }
- Perl version
- ~~~~~~~~~~~~
- We don't want to impose a too-recent Perl version, so only use features
- supported by the Perl version that is currently in Debian oldstable when
- possible. Currently that means Perl 5.14.2.
- Object methods
- ~~~~~~~~~~~~~~
- Use a single line to retrieve all the arguments and use $self as name
- for the current object:
- sub do_something {
- my ($self, $arg1, $arg2, %opts) = @_;
- ...
- }
- Supplementary optional arguments should be named and thus stored in a
- hash.
|