From 5ef2bdcff53f8a9b5c94f340e3debcb68a3f38b1 Mon Sep 17 00:00:00 2001
From: Darin Adler To make code written for Nautilus look and act in a predictable way,
we follow a set of guidelines that specify some details of how we write code.
To start, we follow all the guidelines outlined in the
-GNOME Programming Guidelines.
-This document covers things that are not mentioned in the GNOME
-Programming Guidelines, and things that are mentioned there, but need
-to be re-emphasized, because people don't follow them often enough.
This document covers both things that are not mentioned in the GNOME +Programming Guidelines and things that are mentioned there but need +to be re-emphasized because people don't follow them often enough.
I'm just getting started on this document. Feedback is welcome. Eventually I'd like better organization and tons of examples.
@@ -21,6 +22,8 @@ Eventually I'd like better organization and tons of examples.- Darin
+We use the most-recommended coding style from the GNOME Programming Guidelines. This means that we use the Linux kernel brace style with 8-character tabs (not the GNU brace style), we put spaces before @@ -56,10 +59,47 @@ handles errors or not.
Sometimes they become more important than clarity, but only once they are proven to be a problem. -We use for loops when they make the code easier to read. While loops -can be used for other cases. It's true that +
We use for loops when they make the code easier to read. The alternative +is usually to use a while loop. It's true that "easy to read" is a subjective thing.
+We declare local variables at the top of the function. C allows you +to declare variables at the top of any scope, like the scope in a control +flow construct like an if statement. But putting the declarations there makes +it harder to add and delete if statements, since the declarations have to move. +So we just put them all at the top of the function, usually in the order that +they are used in the function.
+ +We do not initialize local variables in their declarations. C allows you +to initialize a local variable when declaring it. But no other code can run before +this, because the other statements in a function must be after all the declarations. +If there are lines of code initializing the variables in the declarations, it can +be harder to change the function around, since code must move down from the declaration +if other code needs to run after it. To avoid this, we just don't use the ability +to initialize the variable when it's declared.
+ +We always use braces, even for one-statement "blocks". Our consensus is +to do things like this:
+ +++ ++if (list != NULL) { + g_warning ("the list isn't empty"); +} ++
Instead of this:
+ +++ ++if (list != NULL) + g_warning ("the list isn't empty"); ++
This applies to all control structures: if, while, for, do.
+We make each header "stand alone". Our concept with C header files is that each one must be written so it can be included without including another file first. To test that the header files we develop have this property, we always @@ -91,6 +131,8 @@ is the include of <config.h>, which goes first. Here's an example:
+