~ubuntu-branches/ubuntu/vivid/clutter-1.0/vivid-proposed

« back to all changes in this revision

Viewing changes to CODING_STYLE

  • Committer: Bazaar Package Importer
  • Author(s): Emilio Pozuelo Monfort
  • Date: 2010-07-18 17:21:49 UTC
  • mfrom: (1.2.1 upstream) (4.1.3 experimental)
  • Revision ID: james.westby@ubuntu.com-20100718172149-j6s9u4chocaoykme
Tags: 1.2.12-1
* New upstream release.
* debian/libclutter-1.0-0.symbols,
  debian/rules:
  - Add a symbols file.
* debian/rules,
  debian/source/format:
  - Switch to source format 3.0 (quilt).
* debian/control.in:
  - Standards-Version is 3.9.0, no changes needed.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
Clutter Coding Style
2
 
--------------------
3
 
 
4
 
This document is intended to be a short description of the preferred
5
 
coding style to be used for the Clutter source code.
6
 
 
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.
12
 
 
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.
16
 
 
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.
19
 
 
20
 
When in doubt, check the surrounding code and try to imitate it.
21
 
 
22
 
+ Line width
23
 
 
24
 
The maximum line width is 80 characters, whenever possible.
25
 
 
26
 
Longer lines are usually an indication that you either need a function
27
 
or a pre-processor macro.
28
 
 
29
 
+ Indentation
30
 
 
31
 
Each new level is indented 2 or more spaces than the previous level:
32
 
 
33
 
  if (condition)
34
 
    single_statement ();
35
 
 
36
 
This can only be achieved using space characters. It may not be achieved
37
 
using tab characters alone, or using a combination of spaces and tabs.
38
 
 
39
 
Do not change the editor's configuration to change the meaning of a
40
 
tab character (see below); code using tabs to indent will not be accepted
41
 
into Clutter.
42
 
 
43
 
Even if two spaces for each indentation level allows deeper nesting than
44
 
8 spaces, Clutter favours self-documenting function names that can take
45
 
quite some space. For this reason you should avoid deeply nested code.
46
 
 
47
 
+ Tab characters
48
 
 
49
 
The tab character must always be expanded to spaces. If a literal
50
 
tab must be used inside the source, the tab must always be interpreted
51
 
according to its traditional meaning:
52
 
 
53
 
        Advance to the next column which is a multiple of 8.
54
 
 
55
 
+ Braces
56
 
 
57
 
Curly braces should not be used for single statement blocks:
58
 
 
59
 
  if (condition)
60
 
    single_statement ();
61
 
  else
62
 
    another_single_statement (arg1);
63
 
 
64
 
In case of multiple statements, curly braces should be put on another
65
 
indentation level:
66
 
 
67
 
  if (condition)
68
 
    {
69
 
      statement_1 ();
70
 
      statement_2 ();
71
 
      statement_3 ();
72
 
    }
73
 
 
74
 
If the condition or the arguments of the single statement need to be
75
 
split on multiple lines, like:
76
 
 
77
 
  if (condition_1 &&
78
 
      (condition_2 || condition_3))
79
 
    {
80
 
      single_statement ();
81
 
    }
82
 
  else
83
 
    {
84
 
      another_single_statement (very_long_argument_1,
85
 
                                argument_2,
86
 
                                &return_argument_1,
87
 
                                &return_argument_2);
88
 
    }
89
 
 
90
 
In general, new blocks should be placed on a new indentation level,
91
 
like:
92
 
 
93
 
  int retval = 0;
94
 
 
95
 
  statement_1 ();
96
 
  statement_2 ();
97
 
 
98
 
  {
99
 
    int var1 = 42;
100
 
    gboolean res = FALSE;
101
 
 
102
 
    res = statement_3 (var1);
103
 
 
104
 
    retval = res == TRUE ? -1 : 1;
105
 
  }
106
 
 
107
 
While curly braces for function definitions should rest on a new line
108
 
they should not add an indentation level:
109
 
 
110
 
  /* valid */
111
 
  static void
112
 
  my_function (int argument)
113
 
  {
114
 
    do_my_things ();
115
 
  }
116
 
 
117
 
  /* invalid */
118
 
  static void
119
 
  my_function (int argument) {
120
 
    do_my_things ();
121
 
  }
122
 
 
123
 
  /* invalid */
124
 
  static void
125
 
  my_function (int argument)
126
 
    {
127
 
      do_my_things ();
128
 
    }
129
 
 
130
 
Curly braces must not be placed on the same line as a condition:
131
 
 
132
 
  if (condition) {
133
 
    statement_1 ();
134
 
    statement_2 ();
135
 
  }
136
 
 
137
 
+ Functions
138
 
 
139
 
Functions should be declared by placing the returned value on a separate
140
 
line from the function name:
141
 
 
142
 
  void
143
 
  my_function (void)
144
 
  {
145
 
  }
146
 
 
147
 
The arguments list must be broken into a new line for each argument,
148
 
with the argument names right aligned, taking into account pointers:
149
 
 
150
 
  void
151
 
  my_function (some_type_t     type,
152
 
               another_type_t *a_pointer,
153
 
               final_type_t    another_type)
154
 
  {
155
 
  }
156
 
 
157
 
The alignment also holds when invoking a function without breaking the
158
 
80 characters limit:
159
 
 
160
 
  align_function_arguments (first_argument,
161
 
                            second_argument,
162
 
                            third_argument);
163
 
 
164
 
To respect the 80 characters limit do not break the function name from
165
 
the arguments:
166
 
 
167
 
  /* invalid */
168
 
  a_very_long_function_name_with_long_parameters
169
 
    (argument_the_first, argument_the_second);
170
 
 
171
 
  /* valid */
172
 
  first_a = argument_the_first;
173
 
  second_a = argument_the_second;
174
 
  a_very_long_function_name_with_long_parameters (first_a, second_a);
175
 
 
176
 
+ Whitespace
177
 
 
178
 
Always put a space before a parenthesis but never after:
179
 
 
180
 
  /* valid */
181
 
  if (condition)
182
 
    do_my_things ();
183
 
 
184
 
  /* valid */
185
 
  switch (condition)
186
 
    {
187
 
    }
188
 
 
189
 
  /* invalid */
190
 
  if(condition)
191
 
    do_my_things();
192
 
 
193
 
  /* invalid */
194
 
  if ( condition )
195
 
    do_my_things ( );
196
 
 
197
 
A switch() should open a block on a new indentation level, and each case
198
 
should start on the same indentation level as the curly braces, with the
199
 
case block on a new indentation level:
200
 
 
201
 
  /* valid */
202
 
  switch (condition)
203
 
    {
204
 
    case FOO:
205
 
      do_foo ();
206
 
      break;
207
 
 
208
 
    case BAR:
209
 
      do_bar ();
210
 
      break;
211
 
    }
212
 
 
213
 
  /* invalid */
214
 
  switch (condition) {
215
 
    case FOO: do_foo (); break;
216
 
    case BAR: do_bar (); break;
217
 
  }
218
 
 
219
 
If a case block needs to declare new variables, the same rules as the
220
 
inner blocks (see above) apply; the break statement should be placed
221
 
outside of the inner block:
222
 
 
223
 
  switch (condition)
224
 
    {
225
 
    case FOO:
226
 
      {
227
 
        int foo;
228
 
 
229
 
        foo = do_foo ();
230
 
      }
231
 
      break;
232
 
 
233
 
    ...
234
 
    }
235
 
 
236
 
When declaring a structure type use newlines to separate logical sections
237
 
of the structure:
238
 
 
239
 
  struct _ClutterActorPrivate
240
 
  {
241
 
    /* fixed position */
242
 
    ClutterUnit fixed_x;
243
 
    ClutterUnit fixed_y;
244
 
 
245
 
    ClutterRequestMode request_mode;
246
 
 
247
 
    /* requisition sizes */
248
 
    ClutterUnit request_width_for_height;
249
 
    ClutterUnit request_min_width;
250
 
    ClutterUnit request_natural_width;
251
 
    ClutterUnit request_height_for_width;
252
 
    ClutterUnit request_min_height;
253
 
    ClutterUnit request_natural_height;
254
 
 
255
 
    ClutterActorBox allocation;
256
 
 
257
 
    ...
258
 
  };
259
 
 
260
 
Do not eliminate whitespace and newlines just because something would
261
 
fit on 80 characters:
262
 
 
263
 
  /* invalid */
264
 
  if (condition) foo (); else bar ();
265
 
 
266
 
Do eliminate trailing whitespace on any line, preferably as a separate
267
 
patch or commit. Never use empty lines at the beginning or at the end of
268
 
a file.
269
 
 
270
 
Do enable the default git pre-commit hook that detect trailing
271
 
whitespace for you and help you to avoid corrupting Clutter's tree with
272
 
it. Do that as follows:
273
 
 
274
 
  chmod a+x .git/hooks/pre-commit
275
 
 
276
 
You might also find the git-stripspace utility helpful which acts as a
277
 
filter to remove trailing whitespace as well as initial, final, and
278
 
duplicate blank lines.
279
 
 
280
 
+ Headers
281
 
 
282
 
Headers are special, for Clutter, in that they don't have to obey the
283
 
80 characters limit. The only major rule for headers is that the functions
284
 
definition should be vertically aligned in three columns:
285
 
 
286
 
  return value          function_name           (type   argument,
287
 
                                                 type   argument,
288
 
                                                 type   argument);
289
 
 
290
 
The maximum width of each column is given by the longest element in the
291
 
column:
292
 
 
293
 
  void                  clutter_type_set_property (ClutterType  *type,
294
 
                                                   const gchar  *value,
295
 
                                                   GError      **error);
296
 
  G_CONST_RETURN gchar *clutter_type_get_property (ClutterType  *type);
297
 
 
298
 
Public headers should never be included directly:
299
 
 
300
 
  #if !defined(__CLUTTER_H_INSIDE__) && !defined(CLUTTER_COMPILATION)
301
 
  #error "Only <clutter/clutter.h> can be included directly."
302
 
  #endif
303
 
 
304
 
Public headers should also have inclusion guards (for internal usage)
305
 
and C++ guards:
306
 
 
307
 
  #ifndef __CLUTTER_HEADER_H__
308
 
  #define __CLUTTER_HEADER_H__
309
 
 
310
 
  #include <clutter/clutter-actor.h>
311
 
 
312
 
  G_BEGIN_DECLS
313
 
 
314
 
  ...
315
 
 
316
 
  G_END_DECLS
317
 
 
318
 
  #endif /* __CLUTTER_HEADER_H__ */
319
 
 
320
 
+ GObject
321
 
 
322
 
GObject classes definition and implementation require some additional
323
 
coding style notices.
324
 
 
325
 
Typedef declarations should be places at the beginning of the file:
326
 
 
327
 
  typedef struct _ClutterActor          ClutterActor;
328
 
  typedef struct _ClutterActorPrivate   ClutterActorPrivate;
329
 
  typedef struct _ClutterActorClass     ClutterActorClass;
330
 
 
331
 
This includes enumeration types:
332
 
 
333
 
  typedef enum
334
 
  {
335
 
    CLUTTER_REQUEST_WIDTH_FOR_HEIGHT,
336
 
    CLUTTER_REQUEST_HEIGHT_FOR_WIDTH
337
 
  } ClutterRequestMode;
338
 
 
339
 
And callback types:
340
 
 
341
 
  typedef void (* ClutterCallback) (ClutterActor *actor,
342
 
                                    gpointer      user_data);
343
 
 
344
 
Instance structures should only contain the parent type and a pointer to a
345
 
private data structure:
346
 
 
347
 
  struct _ClutterRectangle
348
 
  {
349
 
    ClutterActor parent_instance;
350
 
 
351
 
    ClutterRectanglePrivate *priv;
352
 
  };
353
 
 
354
 
All the properties should be stored inside the private data structure, which
355
 
is defined inside the source file.
356
 
 
357
 
The private data structure should only be accessed internally using the
358
 
pointer inside the instance structure, and never using the
359
 
G_TYPE_INSTANCE_GET_PRIVATE() macro or the g_type_instance_get_private()
360
 
function.
361
 
 
362
 
Always use the G_DEFINE_TYPE(), G_DEFINE_TYPE_WITH_CODE() macros, or
363
 
their abstract variants G_DEFINE_ABSTRACT_TYPE() and
364
 
G_DEFINE_ABSTRACT_TYPE_WITH_CODE().
365
 
 
366
 
+ Memory allocation
367
 
 
368
 
When dynamically allocating data on the heap either use g_new() or,
369
 
if allocating multiple small data structures, g_slice_new().
370
 
 
371
 
Public structure types should always be returned after being zero-ed,
372
 
either explicitly, or by using g_new0() or g_slice_new0().
373
 
 
374
 
+ Macros
375
 
 
376
 
Try to avoid macros unless strictly necessary. Remember to #undef them
377
 
at the end of a block or a series of functions needing them.
378
 
 
379
 
Inline functions are usually preferable to macros.