summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2015-04-20 20:56:17 +0200
committerLennart Poettering <lennart@poettering.net>2015-04-21 00:58:56 +0200
commitba780c116fc919c58fad07f45f4e800a062af63e (patch)
tree6e323746e5367a95b9749869e5ea178ed12c727f
parent81bd37a85fed090350a84db1f0741125582d160e (diff)
CODING_STYLE: document how destructors should work
-rw-r--r--CODING_STYLE16
1 files changed, 16 insertions, 0 deletions
diff --git a/CODING_STYLE b/CODING_STYLE
index feb1a9dd67..a295ca77f2 100644
--- a/CODING_STYLE
+++ b/CODING_STYLE
@@ -239,3 +239,19 @@
2, i.e. stdin, stdout, stderr, should those fds be closed. Given the
special semantics of those fds, it's probably a good idea to avoid
them. F_DUPFD_CLOEXEC with "3" as parameter avoids them.
+
+- When you define a destructor or unref() call for an object, please
+ accept a NULL object and simply treat this as NOP. This is similar
+ to how libc free() works, which accepts NULL pointers and becomes a
+ NOP for them. By following this scheme a lot of if checks can be
+ removed before invoking your destructor, which makes the code
+ substantially more readable and robust.
+
+- Related to this: when you define a destructor or unref() call for an
+ object, please make it return the same type it takes and always
+ return NULL from it. This allows writing code like this:
+
+ p = foobar_unref(p);
+
+ which will always work regardless if p is initialized or not, and
+ guarantees that p is NULL afterwards, all in just one line.