1 GENIVI Boot Manager Coding Style
2 ================================
4 This document intends to give information about the coding style to be
5 used when contributing code to the GENIVI Boot Manager. It does not
6 claim to be complete. Parts of it are taken or inspired from the Clutter
9 In the following, the most important requirements for writing consistent
10 code for the GENIVI Boot Manager are explained.
15 * Indentation and Braces
16 * Functions and Braces
18 * Variable Declarations
22 * Loops and Loop Termination
28 The maximum line width for source files is 90 characters. This limit may
29 be exceeded when there is no way around it. Accepted ways to wrap long
30 lines caused by function calls are
32 result = some_function_with_a_very_long_name (first_parameter,
38 long_result_variable =
39 some_function_with_a_very_long_name (first_parameter,
43 where the result variable name is too long to fit the function call
44 into the 90 characters limit even when wrapping the parameters.
46 Do not separate the function name from its arguments like this:
49 some_function_with_a_long_name
50 (long_argument_name1, long_argument_name2);
52 Instead, consider using shorter variable names as aliases:
55 short1 = long_argument_name1;
56 short2 = long_argument_name2;
57 some_function_with_a_long_name (short1, short2);
59 The line width limit of 90 characters does not apply to header files.
60 However the alignment and parameter indentation rules explained in the
61 section "Functions and Braces" still apply to header files.
67 Always insert a space before a parenthesis but never after or
68 between the opening or closing parenthesis and a parameter.
72 foo (argument1, argument2);
76 foo(argument1, argument2);
80 foo ( argument1, argument 2 );
83 Indentation and Braces
84 ======================
86 Use spaces only, tabs are not allowed. The indentation for each level is
87 2 spaces in addition to the previous level. Braces add another level of
88 indentation. Valid indentations and uses of braces are:
90 Single-line statements:
94 single_line_statement ();
102 another_statement ();
105 Multiple and single statements:
111 another_statement ();
115 one_more_statement ();
142 do_something_else ();
145 do_whatever_you_need_to_do ();
167 case FOO: do_something ();
179 Nested if statements:
184 /* here the same rules as on the top level apply again */
185 if (another_condition)
187 else if (yet_another_condition)
188 another_single_statement ();
191 Do not put curly braces into the same line as the condition:
198 Do not asymmetrically use and not use braces:
203 /* multiple statements */
208 If there are multiple conditions in a single if statement spread across
209 more than one line, always use curly braces:
215 /* multiple or single statement(s) */
222 Braces in function definitions are not indented. Parameters are to be
223 wrapped and aligned so that the end results looks like this:
225 Function declarations:
228 static gchar *some_type_your_function (SomeType *object,
229 int another parameter,
230 gchar **and_another_one);
231 static gboolean some_type_some_longer_function_name (SomeType *object);
233 Function definitions:
237 some_type_your_function (SomeType *object,
238 int another_parameter,
239 gchar **and_another_one)
246 Do not declare functions like this:
249 static gchar *some_type_your_function (SomeType *object,
250 int another parameter,
251 gchar **and_another_one);
252 static gboolean some_type_some_longer_function_name (SomeType *object);
257 static gchar *some_type_your_function (SomeType *object, int another parameter, gchar **and_another_one);
258 static gboolean some_type_some_longer_function_name (SomeType *object);
264 Between declarations of groups of structs, enums, functions, static
265 variables and macros there have to be three empty lines to make the
266 different items easier to spot and distinguish. There also have to be
267 three empty lines between function definitions.
269 Also, when declaring data structures, use newlines to separate logical
270 sections of member variables:
272 struct _LucHandlerService
274 /* Current LUC content */
275 GHashTable *current_content;
278 /* NSM shutdown consumer */
279 ShutdownConsumer *shutdown_consumer;
281 /* protection against threads */
286 Variable Declarations
287 =====================
289 Variables may only be declared at the top of a function. Variable
290 declarations in blocks (code surrounded by braces) are not allowed.
292 Declarations follow special alignment and sorting rules. The sorting
293 order of declarations is determined by:
295 1. number of characters of the variable type
296 2. ascending alphabetical order of the type (case-insensitive)
297 3. ascending alphabetical order of the variable name
299 Here is an example of how a variable declaration sequence has to
302 GbmgrLegacyAppHandlerService *service;
303 NSMShutdownConsumer *consumer;
304 const gchar *object_name;
313 In order to make it easier to detect broken code paths, assertions in
314 the form of g_return_if_fail() and g_return_val_if_fail() statements are
315 used in almost all methods. When implementing new methods in your code,
316 please make sure to check the input parameters for type incompatiblities
317 or memory corruption.
323 Do not check boolean values for equality like this:
326 if (condition == TRUE)
329 Instead, just do it like this:
335 Be explicit when checking pointers however:
338 if (some_pointer == NULL)
342 if (some_pointer != NULL)
345 Do not simply do it like this:
351 If you have so many conditions in an if statement that you need to split
352 them up into multiple lines, the logical operatiors should always be
353 placed at the beginning of the line, like this:
363 Don't place the logical operators at the end of the line:
377 Header files should always include the following code in addition to the
378 license header (example for gbmgr-data-structure.h):
380 #ifndef __GBMGR_DATA_STRUCTURE_H__
381 #define __GBMGR_DATA_STRUCTURE_H__
383 #include <something-that-also-includes-glib-object.h>
391 #endif /* !__GBMGR_DATA_STRUCTURE_H__ */
395 Loops and Loop Termination
396 ==========================
398 When writing loops, try to avoid break statements. Instead of breaking
399 on some condition move the condition into the loop header to make more
400 clear when the loop is supposed to be terminated.
405 for (n = 0; n < some_value; ++n)
407 if (some_other_condition)
416 for (n = 0; !some_other_condition && n < some_value; ++n)
421 If the loop header exceeds the 90 character limit per line, split it up
422 into multiple lines (in which case you are required to add curly braces
427 !some_other_condition && n < some_value;
433 Try to avoid while loops where you can. Some GLib data structures
434 such as iterators encourage the use of while loops. In those cases it's
435 ok not to use for loops.