2 -------------------------------------------------------------------------------
4 This document is intended to be a short description of the preferred
5 coding style to be used for the Clutter source code.
7 Coding style is a matter of consistency, readability and maintainance;
8 coding style is also completely arbitrary and a matter of taste. This
9 document will use examples at the very least to provide authoritative
10 and consistent answers to common questions regarding the coding style,
11 and will also try to identify the allowed exceptions.
13 The examples will show the preferred coding style; the negative examples
14 will be clearly identified. Please, don't submit code to Clutter that
15 looks like any of these.
17 Part of the rationales for these coding style rules are available either
18 in the kernel CodingStyle document or in Cairo's CODING_STYLE one.
20 When in doubt, check the surrounding code and try to imitate it.
22 Clutter provides an Uncrustify configuration file that tries to match
23 this document. Since automated tools are not a substitute for human eye,
24 they should not be entirely relied upon - but they can give an initial
25 layout for contributors.
29 The maximum line width for source files is 80 characters, whenever possible.
30 Longer lines are usually an indication that you either need a function
31 or a pre-processor macro.
35 Each new level is indented 2 or more spaces than the previous level:
40 This can only be achieved using space characters. It may not be achieved
41 using tab characters alone, or using a combination of spaces and tabs.
43 Do not change the editor's configuration to change the meaning of a
44 tab character (see below); code using tabs to indent will not be accepted
47 Even if two spaces for each indentation level allows deeper nesting than
48 8 spaces, Clutter favours self-documenting function names that can take
49 quite some space. For this reason you should avoid deeply nested code.
53 The tab character must always be expanded to spaces. If a literal
54 tab must be used inside the source, the tab must always be interpreted
55 according to its traditional meaning:
57 Advance to the next column which is a multiple of 8.
58 [ these two lines should be aligned ]
62 Curly braces should not be used for single statement blocks:
67 another_single_statement (arg1);
69 In case of multiple statements, curly braces should be put on another
79 The "no block for single statements" rule has only three exceptions:
81 ① if the single statement covers multiple lines, e.g. for functions with
82 many arguments, and it is followed by else or else if:
87 a_single_statement_with_many_arguments (some_lengthy_argument,
88 another_lengthy_argument,
93 another_single_statement (arg1, arg2);
95 ② if the condition is composed of many lines:
99 (condition2 && condition3) ||
101 (condition5 && (condition6 || condition7)))
103 a_single_statement ();
106 ③ Nested if's, in which case the block should be placed on the
112 if (another_condition)
115 another_single_statement ();
120 if (another_condition)
122 else if (yet_another_condition)
123 another_single_statement ();
125 In general, new blocks should be placed on a new indentation level,
135 gboolean res = FALSE;
137 res = statement_3 (var1);
139 retval = res ? -1 : 1;
142 While curly braces for function definitions should rest on a new line
143 they should not add an indentation level:
147 my_function (int argument)
154 my_function (int argument) {
160 my_function (int argument)
165 Curly braces must not be placed on the same line as a condition:
175 Do not check boolean values for equality:
178 if (condition == TRUE)
182 if (another_condition)
185 Even if C handles NULL equality like a boolean, be explicit:
188 if (some_pointer == NULL)
192 if (some_other_pointer)
195 In case of conditions split over multiple lines, the logical operators should
196 always go at the end of the line:
209 (condition3 || (condition4 && condition5)))
216 Functions should be declared by placing the returned value on a separate
217 line from the function name:
224 The arguments list must be broken into a new line for each argument,
225 with the argument names right aligned, taking into account pointers:
228 my_function (some_type_t type,
229 another_type_t *a_pointer,
230 final_type_t another_type)
234 The alignment also holds when invoking a function without breaking the
237 align_function_arguments (first_argument,
241 To respect the 80 characters limit do not break the function name from
245 a_very_long_function_name_with_long_parameters
246 (argument_the_first, argument_the_second);
249 first_a = argument_the_first;
250 second_a = argument_the_second;
251 a_very_long_function_name_with_long_parameters (first_a, second_a);
255 Always put a space before a parenthesis but never after:
274 A switch() should open a block on a new indentation level, and each case
275 should start on the same indentation level as the curly braces, with the
276 case block on a new indentation level:
292 case FOO: do_foo (); break;
293 case BAR: do_bar (); break;
316 It is preferable, though not mandatory, to separate the various cases with
333 The 'break' statement for the default: case is not mandatory.
335 If a case block needs to declare new variables, the same rules as the
336 inner blocks (see above) apply; the break statement should be placed
337 outside of the inner block:
352 When declaring a structure type use newlines to separate logical sections
355 struct _ClutterActorPrivate
361 ClutterRequestMode request_mode;
363 /* requisition sizes */
364 ClutterUnit request_width_for_height;
365 ClutterUnit request_min_width;
366 ClutterUnit request_natural_width;
367 ClutterUnit request_height_for_width;
368 ClutterUnit request_min_height;
369 ClutterUnit request_natural_height;
371 ClutterActorBox allocation;
376 Do not eliminate whitespace and newlines just because something would
377 fit on 80 characters:
380 if (condition) foo (); else bar ();
382 Do eliminate trailing whitespace on any line, preferably as a separate
383 patch or commit. Never use empty lines at the beginning or at the end of
386 Do enable the default git pre-commit hook that detect trailing
387 whitespace for you and help you to avoid corrupting Clutter's tree with
388 it. Do that as follows:
390 chmod a+x .git/hooks/pre-commit
392 You might also find the git-stripspace utility helpful which acts as a
393 filter to remove trailing whitespace as well as initial, final, and
394 duplicate blank lines.
398 Headers are special, for Clutter, in that they don't have to obey the
399 80 characters limit. The only major rule for headers is that the functions
400 definition should be vertically aligned in three columns:
402 return value function_name (type argument,
406 The maximum width of each column is given by the longest element in the
409 void clutter_type_set_property (ClutterType *type,
412 const gchar *clutter_type_get_property (ClutterType *type);
414 It is also possible to align the columns to the next tab:
416 void clutter_type_set_prop (ClutterType *type,
418 gfloat clutter_type_get_prop (ClutterType *type);
419 gint clutter_type_update_foobar (ClutterType *type);
421 Public headers should never be included directly:
423 #if !defined(__CLUTTER_H_INSIDE__) && !defined(CLUTTER_COMPILATION)
424 #error "Only <clutter/clutter.h> can be included directly."
427 Public headers should also have inclusion guards (for internal usage)
430 #ifndef __CLUTTER_HEADER_H__
431 #define __CLUTTER_HEADER_H__
433 #include <clutter/clutter-actor.h>
441 #endif /* __CLUTTER_HEADER_H__ */
445 Clutter source files should never include the global clutter.h header, but
446 instead include the individual headers that are needed. Every file must
447 include config.h first, then its own header, then other Clutter headers
448 that it needs, then system and third-party headers that it needs.
453 #include "clutter-foo.h"
455 #include "clutter-actor.h"
456 #include "clutter-container.h"
464 GObject classes definition and implementation require some additional
465 coding style notices.
467 Typedef declarations should be placed at the beginning of the file:
469 typedef struct _ClutterActor ClutterActor;
470 typedef struct _ClutterActorPrivate ClutterActorPrivate;
471 typedef struct _ClutterActorClass ClutterActorClass;
473 This includes enumeration types:
476 CLUTTER_REQUEST_WIDTH_FOR_HEIGHT,
477 CLUTTER_REQUEST_HEIGHT_FOR_WIDTH
478 } ClutterRequestMode;
482 typedef void (* ClutterCallback) (ClutterActor *actor,
485 Instance structures should only contain the parent type and a pointer to a
486 private data structure, and they should be annotated as "private":
488 struct _ClutterRectangle
491 ClutterActor parent_instance;
493 ClutterRectanglePrivate *priv;
496 All the properties should be stored inside the private data structure, which
497 is defined inside the source file - or, if needed, inside a private header
498 file; the private header filename must end with "-private.h" and must not be
501 The private data structure should only be accessed internally using the
502 pointer inside the instance structure, and never using the
503 G_TYPE_INSTANCE_GET_PRIVATE() macro or the g_type_instance_get_private()
506 Always use the G_DEFINE_TYPE(), G_DEFINE_TYPE_WITH_CODE() macros, or
507 their abstract variants G_DEFINE_ABSTRACT_TYPE() and
508 G_DEFINE_ABSTRACT_TYPE_WITH_CODE().
510 Avoid forward declaration for functions: use the G_DEFINE_* macros right
511 after the private types, variables and macros declarations.
513 Interface types should always have the dummy typedef for cast purposes:
515 typedef struct _ClutterFoo ClutterFoo;
517 The interface structure should have "Iface" postfixed to the dummy typedef:
519 typedef struct _ClutterFooIface ClutterFooIface;
521 Interfaces must have the following macros:
523 - Macro: - Expands to:
524 • CLUTTER_TYPE_<iface_name> <iface_name>_get_type
525 • CLUTTER_<iface_name> G_TYPE_CHECK_INSTANCE_CAST
526 • CLUTTER_IS_<iface_name> G_TYPE_CHECK_INSTANCE_TYPE
527 • CLUTTER_<iface_name>_GET_IFACE G_TYPE_INSTANCE_GET_INTERFACE
531 When dynamically allocating data on the heap either use g_new() or,
532 if allocating multiple small data structures, g_slice_new().
534 Public structure types should always be returned after being zero-ed,
535 either explicitly for each member, or by using g_new0() or g_slice_new0().
539 Try to avoid private macros unless strictly necessary. Remember to #undef
540 them at the end of a block or a series of functions needing them.
542 Inline functions are usually preferable to private macros.
544 Public macros should not be used unless they evaluate to a constant.
548 Avoid exporting variables as public API, since this is cumbersome on some
549 platforms. It is always preferable to add getters and setters instead.
553 Non-exported functions that are needed in more than one source file
554 should be named "_clutter_...", and declared in a private header file.
556 Underscore-prefixed functions are never exported.
558 Non-exported functions that are only needed in one source file
559 should be declared static.
563 All public APIs must have gtk-doc comments. For functions, these should
564 be placed in the source file, directly above the function.
569 * @actor: a #ClutterActor
571 * Gets the flow of an actor.
573 * Note that flows may be laminar or turbulent...
575 * Return value: (transfer none): the flow of @actor
578 clutter_get_flow (ClutterActor *actor)
583 Doc comments for macros, function types, class structs, etc should be
584 placed next to the definitions, typically in headers.
586 Section introductions should be placed in the source file they describe,
587 after the license header:
591 * SECTION:clutter-align-constraint
592 * @Title: ClutterAlignConstraint
593 * @Short_Description: A constraint aligning the position of an actor
595 * #ClutterAlignConstraint is a #ClutterConstraint that aligns the position
596 * of the #ClutterActor to which it is applied to the size of another
597 * #ClutterActor using an alignment factor
602 To properly document a new function, macro, function type or struct,
603 it needs to be listed in the clutter-sections.txt file.
605 To properly document a new class, it needs to be given its own section
606 in clutter-sections.txt, needs to be included in clutter-docs.xml, and the
607 get_type function needs to listed in clutter.types.
611 It is ok to update the style of a code block or function when you
612 are touching it anyway, but sweeping whitespace changes obscure the
613 git history and should be avoided.