]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - docs/CODING_STYLE.md
dlfcn-util: add dlsym_many_and_warn() helper
[thirdparty/systemd.git] / docs / CODING_STYLE.md
index d945f8cdbe7f3fb3d59c1e29ed2d3f9586216520..f335a1012ebe238c0a5520fd77c865a27debd1a0 100644 (file)
@@ -1,5 +1,7 @@
 ---
 title: Coding Style
+category: Contributing
+layout: default
 ---
 
 # Coding Style
@@ -201,6 +203,19 @@ title: Coding Style
   array. In that case use STRLEN, which evaluates to a static constant and
   doesn't force the compiler to create a VLA.
 
+- Please use C's downgrade-to-bool feature only for expressions that are
+  actually booleans (or "boolean-like"), and not for variables that are really
+  numeric. Specifically, if you have an `int b` and it's only used in a boolean
+  sense, by all means check its state with `if (b) …` — but if `b` can actually
+  have more than two semantic values, and you want to compare for non-zero,
+  then please write that explicitly with `if (b != 0) …`. This helps readability
+  as the value range and semantical behaviour is directly clear from the
+  condition check. As a special addition: when dealing with pointers which you
+  want to check for non-NULL-ness, you may also use downgrade-to-bool feature.
+
+- Please do not use yoda comparisons, i.e. please prefer the more readable `if
+  (a == 7)` over the less readable `if (7 == a)`.
+
 ## Destructors
 
 - The destructors always deregister the object from the next bigger object, not
@@ -223,7 +238,7 @@ title: Coding Style
   p = foobar_unref(p);
   ```
 
-  which will always work regardless if `p` is initialized or not,x and
+  which will always work regardless if `p` is initialized or not, and
   guarantees that `p` is `NULL` afterwards, all in just one line.
 
 ## Error Handling
@@ -409,7 +424,7 @@ title: Coding Style
 
 ## Deadlocks
 
-- Do not issue NSS requests (that includes user name and host name lookups)
+- Do not issue NSS requests (that includes user name and hostname lookups)
   from PID 1 as this might trigger deadlocks when those lookups involve
   synchronously talking to services that we would need to start up.
 
@@ -506,7 +521,7 @@ title: Coding Style
   hence we might want to call it "big endian" right-away.
 
 - Please never use `dup()`. Use `fcntl(fd, F_DUPFD_CLOEXEC, 3)` instead. For
-  two reason: first, you want `O_CLOEXEC` set on the new `fd` (see
+  two reasons: first, you want `O_CLOEXEC` set on the new `fd` (see
   above). Second, `dup()` will happily duplicate your `fd` as 0, 1, 2,
   i.e. stdin, stdout, stderr, should those `fd`s be closed. Given the special
   semantics of those `fd`s, it's probably a good idea to avoid
@@ -528,7 +543,7 @@ title: Coding Style
   time you need that please immediately undefine `basename()`, and add a
   comment about it, so that no code ever ends up using the POSIX version!
 
-# Committing to git
+## Committing to git
 
 - Commit message subject lines should be prefixed with an appropriate component
   name of some kind. For example "journal: ", "nspawn: " and so on.