CODING_STYLE revision 8d0e0ddda6501479eb69164687c83c1a7667b33a
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy- 8ch indent, no tabs
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy- Variables and functions *must* be static, unless they have a
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy prototype, and are supposed to be exported.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy- structs in MixedCase (with exceptions, such as public API structs),
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy variables + functions in lower_case.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy- The destructors always unregister the object from the next bigger
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy object, not the other way around
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy- To minimize strict aliasing violations, we prefer unions over casting
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy- For robustness reasons, destructors should be able to destruct
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy half-initialized objects, too
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy- Error codes are returned as negative Exxx. i.e. return -EINVAL. There
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy are some exceptions: for constructors, it is OK to return NULL on
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy OOM. For lookup functions, NULL is fine too for "not found".
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy Be strict with this. When you write a function that can fail due to
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy more than one cause, it *really* should have "int" as return value
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy for the error code.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy- Don't bother with error checking whether writing to stdout/stderr
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy- Do not log errors from "library" code, only do so from "main
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy program" code. (With one exception: it's OK to log with DEBUG level
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy from any code, with the exception of maybe inner loops).
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy- Always check OOM. There's no excuse. In program code, you can use
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy "log_oom()" for then printing a short message, but not in "library" code.
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy- Do not issue NSS requests (that includes user name and host name
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy lookups) from PID 1 as this might trigger deadlocks when those
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy lookups involve synchronously talking to services that we would need
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy- Don't synchronously talk to any other service from PID 1, due to
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy risk of deadlocks
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy- Avoid fixed sized string buffers, unless you really know the maximum
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy size and that maximum size is small. They are a source of errors,
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy since they possibly result in truncated strings. Often it is nicer
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy to use dynamic memory, alloca() or VLAs. If you do allocate fixed
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy size strings on the stack, then it's probably only OK if you either
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy use a maximum size such as LINE_MAX, or count in detail the maximum
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy size a string can have. (DECIMAL_STR_MAX and DECIMAL_STR_WIDTH
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy macros are your friends for this!)
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy Or in other words, if you use "char buf[256]" then you are likely
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy doing something wrong!
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy- Stay uniform. For example, always use "usec_t" for time
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy values. Don't usec mix msec, and usec and whatnot.
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy- Make use of _cleanup_free_ and friends. It makes your code much
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy nicer to read!
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy- Be exceptionally careful when formatting and parsing floating point
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy numbers. Their syntax is locale dependent (i.e. "5.000" in en_US is
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy generally understood as 5, while on de_DE as 5000.).
1d32ba663e202c24a5a1f2e5aef83fffb447cb7fJohn Wren Kennedy- Try to use this:
f38cb554a534c6df738be3f4d23327e69888e634John Wren Kennedy instead of this:
- Use "goto" for cleaning up, and only use it for that. i.e. you may
- Public API calls (i.e. functions exported by our shared libraries)