Difference between revisions of "Coding style"

From collectd Wiki
Jump to: navigation, search
m (Reverted edits by 83.234.245.140 (Talk) to last revision by Octo)
(C99 features: Added note about _Bool.)
Line 36: Line 36:
  
 
* Please do not mix variable declarations and code. Declare all variables at the beginning of a block.
 
* Please do not mix variable declarations and code. Declare all variables at the beginning of a block.
* Please do not use C++-style comments.
+
* Please do not use C++-style comments. Use <code style="color: navy; white-space: nowrap;">/* … */</code> instead.
 
* Please do not use <em>flexible array members</em> (FAM).
 
* Please do not use <em>flexible array members</em> (FAM).
* Please use the integer types found in <code>&lt;stdint.h&gt;</code> if you need a fixed size integer, e. g. for parsing binary network packets and the like. If you need to print such a variable, please use the printing macros provided by <code>&lt;inttypes.h&gt;</code>.
+
* Please use the integer types found in <code style="color: maroon;">&lt;stdint.h&gt;</code> if you need a fixed size integer, e.g. for parsing binary network packets and the like. If you need to print such a variable, please use the printing macros provided by <code style="color: maroon;">&lt;inttypes.h&gt;</code>.
 +
* Feel free to use the <code style="color: darkgreen;">_Bool</code> type. Assign only the numeric values <code>1</code> (true) and <code>0</code> (false) to these variables (i.e. ''don't'' use any defines, such as <code style="text-decoration: line-through;">true</code> from <code>&lt;stdbool.h&gt;</code>.
  
 
== Miscellaneous ==
 
== Miscellaneous ==

Revision as of 06:43, 9 May 2013

While we're very liberal when it comes to coding style, there are a few rules you ought to stick to. They're mostly obvious, but I don't write this down because I'm bored either..

Please note that these are more guidelines than a fixed set of rules. If you have a good reason for breaking one of these points, go ahead. But you may be bothered to explain why you did so.

There is one absolutely unbreakable rule though:

Programs must be written for people to read, and only incidentally for machines to execute.

— Abelson / Sussman

Function and variable names

  • Names should follow the normal C naming scheme, e. g.: "submit_value", "temperature_current". Mixed-case names, as popular with Java developers, should not be used. So "submitValue" is evil. Not quite as bad but still bad is "submitvalue". Naturally, using non-ASCII characters is off-limits.
  • Names should be as long as necessary - not longer, but not shorter either. If in doubt, use the longer name.
  • All-capital names are reserved for, and should be used by, defines, macros and enum-members.
  • If several variables or functions with similar meaning exist, such as minimum, average and maximum temperature, the common part should be in front, e. g. "temp_max", "temp_min" and so on.
  • Non-static functions must be declared in a header file that has the same base name as the .c file defining the function. static functions should not have a forward declaration.

Plugins

  • All functions within a plugin should be declared static. The obvious exception is the "module_register" function (see plugin architecture).
  • The behavior of a plugin should not depend on compile time settings. If this cannot be guaranteed, for example because the library a plugin uses must be a certain version for an optional feature, this has to be documented in the collectd.conf(5) manual page.

Standard functions

  • Only reentrant- and thread-safe functions and libraries may be used.

Fixed size buffers

  • The functions strcpy, strcat, strtok and sprintf must not be used.
  • Instead of strncpy and snprintf use sstrncpy and ssnprintf. These functions assure a null byte at the end of the buffer.
  • Only explicitly give the size of the buffer when declaring it. Use the sizeof operator or the STATIC_ARRAY_SIZE macro after that. Please don't hide the actual size of the buffer in some define.

C99 features

  • Please do not mix variable declarations and code. Declare all variables at the beginning of a block.
  • Please do not use C++-style comments. Use /* … */ instead.
  • Please do not use flexible array members (FAM).
  • Please use the integer types found in <stdint.h> if you need a fixed size integer, e.g. for parsing binary network packets and the like. If you need to print such a variable, please use the printing macros provided by <inttypes.h>.
  • Feel free to use the _Bool type. Assign only the numeric values 1 (true) and 0 (false) to these variables (i.e. don't use any defines, such as true from <stdbool.h>.

Miscellaneous

  • Do not compare int and size_t without a cast. Those two types cannot be cast to one another automatically on many platforms.
  • Use the %zu format when printing size_t.

License information and copyright notice

  • All source files must begin with a short license note including a copyright statement. It is recommended to copy and adapt the comment from another file. Please note that most files in collectd are licensed under the terms of the GPLv2 only, not the otherwise widely used "GPLv2 or later" schema. If you want to permit the use of your code under the terms of the GPLv3, please adapt the header. Of course, any other GPL-compatible, OSI-approved free, open-source license is acceptable as well.
  • Please spell your name in the copyright notice as it should be written according to your native language. If you need non-ASCII characters for this, make sure the file is encoded using the UTF-8 character set.