summaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
authorColin Walters <walters@verbum.org>2014-12-07 17:19:42 -0500
committerColin Walters <walters@verbum.org>2014-12-07 17:19:42 -0500
commitb8a06c11f64c319b54dee833dd68c598534596c9 (patch)
tree6efc04f6692e96921d33e940acbe22a532830de1 /README.md
parent22ed7d0fbf357b640521f8557d28312145b2c84c (diff)
downloadostree-b8a06c11f64c319b54dee833dd68c598534596c9.tar.gz
README.md: Add a quick blurb on style
Diffstat (limited to 'README.md')
-rw-r--r--README.md95
1 files changed, 95 insertions, 0 deletions
diff --git a/README.md b/README.md
index 50273c02..ba6929ed 100644
--- a/README.md
+++ b/README.md
@@ -31,3 +31,98 @@ To run just ostree's tests:
gnome-desktop-testing-runner -p 0 ostree/
+Coding style
+------------
+
+Indentation is GNU. Files should start with the appropriate mode lines.
+
+Use GCC `__attribute__((cleanup))` wherever possible. If interacting
+with a third party library, try defining local cleanup macros.
+
+Use GError and GCancellable where appropriate.
+
+Prefer returning `gboolean` to signal success/failure, and have output
+values as parameters.
+
+Prefer linear control flow inside functions (aside from standard
+loops). In other words, avoid "early exits" or use of `goto` besides
+`goto out;`.
+
+This is an example of an "early exit":
+
+ static gboolean
+ myfunc (...)
+ {
+ gboolean ret = FALSE;
+
+ /* some code */
+
+ /* some more code */
+
+ if (condition)
+ return FALSE;
+
+ /* some more code */
+
+ ret = TRUE;
+ out:
+ return ret;
+ }
+
+If you must shortcut, use:
+
+ if (condition)
+ {
+ ret = TRUE;
+ goto out;
+ }
+
+A consequence of this restriction is that you are encouraged to avoid
+deep nesting of loops or conditionals. Create internal static helper
+functions, particularly inside loops. For example, rather than:
+
+ while (condition)
+ {
+ /* some code */
+ if (condition)
+ {
+ for (i = 0; i < somevalue; i++)
+ {
+ if (condition)
+ {
+ /* deeply nested code */
+ }
+
+ /* more nested code */
+ }
+ }
+ }
+
+Instead do this:
+
+ static gboolean
+ helperfunc (..., GError **error)
+ {
+ if (condition)
+ {
+ /* deeply nested code */
+ }
+
+ /* more nested code */
+
+ return ret;
+ }
+
+ while (condition)
+ {
+ /* some code */
+ if (!condition)
+ continue;
+
+ for (i = 0; i < somevalue; i++)
+ {
+ if (!helperfunc (..., i, error))
+ goto out;
+ }
+ }
+