eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

coding-style.txt (9370B)


      1 Background
      2 ==========
      3 
      4 Every project has its coding style, and the Wireless daemon is not an
      5 exception.  This document describes the preferred coding style for the
      6 Wireless daemon code, in order to keep some level of consistency among
      7 developers so that code can be easily understood and maintained, and also
      8 to help your code survive under maintainer's fastidious eyes so that you
      9 can get a passport for your patch ASAP.
     10 
     11 First of all, the Wireless daemon coding style must follow every rule for
     12 Linux kernel (http://www.kernel.org/doc/Documentation/process/coding-style.rst).
     13 There also exists a tool named checkpatch.pl to help you check the compliance
     14 with it. Just type "checkpatch.pl --no-tree --no-signoff patch_name" to
     15 check your patch.
     16 
     17 In theory, you need to clean up all the warnings and errors.  In certain
     18 circumstances one can ignore the 80 character per line limit.  This is
     19 generally only allowed if the alternative would make the code even less
     20 readable.
     21 
     22 Besides the kernel coding style above, the Wireless daemon has special
     23 flavors for its own. Some of them are mandatory (marked as 'M'), while
     24 some others are optional (marked as 'O'), but generally preferred.
     25 
     26 
     27 M1: Blank line before and after an if/while/do/for statement
     28 ============================================================
     29 There should be a blank line before if statement unless the if is nested and
     30 not preceded by an expression or variable declaration.
     31 
     32 Example:
     33 1)
     34 a = 1;
     35 if (b) {  // wrong
     36 
     37 2)
     38 a = 1
     39 
     40 if (b) {
     41 }
     42 a = 2;	// wrong
     43 
     44 3)
     45 if (a) {
     46 	if (b)  // correct
     47 
     48 4)
     49 b = 2;
     50 
     51 if (a) {	// correct
     52 
     53 }
     54 
     55 b = 3;
     56 
     57 The only exception to this rule applies when a variable is being allocated:
     58 array = g_try_new0(int, 20);
     59 if (array)	// Correct
     60 	return;
     61 
     62 
     63 M2: Multiple line comment
     64 =========================
     65 If your comments have more then one line, please start it from the second line.
     66 
     67 Example:
     68 /*
     69  * first line comment	// correct
     70  * ...
     71  * last line comment
     72  */
     73 
     74 
     75 M3: Space before and after operator
     76 ===================================
     77 There should be a space before and after each operator.
     78 
     79 Example:
     80 a + b;  // correct
     81 
     82 
     83 M4: Wrap long lines
     84 ===================
     85 If your condition in if, while, for statement or a function declaration is too
     86 long to fit in one line, the new line needs to be indented not aligned with the
     87 body.
     88 
     89 Example:
     90 1)
     91 if (call->status == CALL_STATUS_ACTIVE ||
     92 	call->status == CALL_STATUS_HELD) {  // wrong
     93 	ofono_dbus_dict_append();
     94 
     95 2)
     96 if (call->status == CALL_STATUS_ACTIVE ||
     97 		call->status == CALL_STATUS_HELD) {  // correct
     98 	ofono_dbus_dict_append();
     99 
    100 3)
    101 gboolean sim_ust_is_available(unsigned char *service_ust, unsigned char len,
    102 	num sim_ust_service index) // wrong
    103 {
    104 	int a;
    105 	...
    106 }
    107 
    108 4)
    109 gboolean sim_ust_is_available(unsigned char *service_ust, unsigned char len,
    110 					enum sim_ust_service index) // correct
    111 {
    112 	int a;
    113 	...
    114 }
    115 
    116 If the line being wrapped is a function call or function declaration, the
    117 preferred style is to indent at least past the opening parenthesis. Indenting
    118 further is acceptable as well (as long as you don't hit the 80 character
    119 limit).
    120 
    121 If this is not possible due to hitting the 80 character limit, then indenting
    122 as far as possible to the right without hitting the limit is preferred.
    123 
    124 Example:
    125 
    126 1)
    127 gboolean sim_ust_is_available(unsigned char *service_ust, unsigned char len,
    128 		enum sim_ust_service index); // worse
    129 
    130 2)
    131 gboolean sim_ust_is_available(unsigned char *service_ust, unsigned char len,
    132 						enum sim_ust_service index);
    133 						// better
    134 
    135 
    136 M5: Git commit message 50/72 formatting
    137 =======================================
    138 The commit message header should be within 50 characters. And if you have
    139 detailed explanatory text, wrap it to 72 character.
    140 
    141 
    142 M6: Space when doing type casting
    143 =================================
    144 There should be a space between new type and variable.
    145 
    146 Example:
    147 1)
    148 a = (int *)b;  // wrong
    149 2)
    150 a = (int *) b;  // correct
    151 
    152 
    153 M7: Don't initialize variable unnecessarily
    154 ===========================================
    155 When declaring a variable, try not to initialize it unless necessary.
    156 
    157 Example:
    158 int i = 1;  // wrong
    159 
    160 for (i = 0; i < 3; i++) {
    161 }
    162 
    163 
    164 M8: Use g_try_malloc instead of g_malloc
    165 ========================================
    166 When g_malloc fails, the whole program would exit. Most of time, this is not
    167 the expected behavior, and you may want to use g_try_malloc instead.
    168 
    169 Example:
    170 additional = g_try_malloc(len - 1);  // correct
    171 if (additional == NULL)
    172 	return FALSE;
    173 
    174 
    175 M9: Follow the order of include header elements
    176 ===============================================
    177 When writing an include header the various elements should be in the following
    178 order:
    179 	- #includes
    180 	- forward declarations
    181 	- #defines
    182 	- enums
    183 	- typedefs
    184 	- function declarations and inline function definitions
    185 
    186 
    187 M10: Internal headers must not use include guards
    188 =================================================
    189 Any time when creating a new header file with non-public API, that header
    190 must not contain include guards.
    191 
    192 
    193 M11: Naming of enums
    194 ====================
    195 Enums must have a descriptive name.  The enum type should be small caps and
    196 it should not be typedef-ed.  Enum contents should be in CAPITAL letters and
    197 prefixed by the enum type name.
    198 
    199 Example:
    200 
    201 enum animal_type {
    202 	ANIMAL_TYPE_FOUR_LEGS,
    203 	ANIMAL_TYPE_EIGHT_LEGS,
    204 	ANIMAL_TYPE_TWO_LEGS,
    205 };
    206 
    207 If the enum contents have values (e.g. from specification) the formatting
    208 should be as follows:
    209 
    210 enum animal_type {
    211 	ANIMAL_TYPE_FOUR_LEGS =		4,
    212 	ANIMAL_TYPE_EIGHT_LEGS =	8,
    213 	ANIMAL_TYPE_TWO_LEGS =		2,
    214 };
    215 
    216 
    217 M12: Enum as switch variable
    218 ====================
    219 
    220 If the variable of a switch is an enum, you must not include a default in
    221 switch body. The reason for this is: If later on you modify the enum by adding
    222 a new type, and forget to change the switch accordingly, the compiler will
    223 complain the new added type hasn't been handled.
    224 
    225 Example:
    226 
    227 enum animal_type {
    228 	ANIMAL_TYPE_FOUR_LEGS =		4,
    229 	ANIMAL_TYPE_EIGHT_LEGS =	8,
    230 	ANIMAL_TYPE_TWO_LEGS =		2,
    231 };
    232 
    233 enum animal_type t;
    234 
    235 switch (t) {
    236 case ANIMAL_TYPE_FOUR_LEGS:
    237 	...
    238 	break;
    239 case ANIMAL_TYPE_EIGHT_LEGS:
    240 	...
    241 	break;
    242 case ANIMAL_TYPE_TWO_LEGS:
    243 	...
    244 	break;
    245 default:  // wrong
    246 	break;
    247 }
    248 
    249 However if the enum comes from an external header file outside ofono
    250 we cannot make any assumption of how the enum is defined and this
    251 rule might not apply.
    252 
    253 
    254 M13: Check for pointer being NULL
    255 =================================
    256 When checking if a pointer or a return value is NULL, use the shorter check
    257 with "!" operator rather than explicitly compare to NULL.
    258 
    259 Example:
    260 1)
    261 array = g_try_new0(int, 20);
    262 if (!array)	// Correct
    263 	return;
    264 
    265 2)
    266 array = g_try_new0(int, 20);
    267 if (array == NULL)	// Wrong
    268 	return;
    269 
    270 
    271 M14: Always use parenthesis with sizeof
    272 =======================================
    273 The expression argument to the sizeof operator should always be in
    274 parenthesis, too.
    275 
    276 Example:
    277 1)
    278 memset(stuff, 0, sizeof(*stuff));
    279 
    280 2)
    281 memset(stuff, 0, sizeof *stuff); // Wrong
    282 
    283 
    284 M15: Use void if function has no parameters
    285 ===========================================================
    286 A function with no parameters must use void in the parameter list.
    287 
    288 Example:
    289 1)
    290 void foo(void)
    291 {
    292 }
    293 
    294 2)
    295 void foo()	// Wrong
    296 {
    297 }
    298 
    299 
    300 M16: Don't use hex value with shift operators
    301 ==============================================
    302 The expression argument to the shift operators should not be in hex.
    303 
    304 Example:
    305 
    306 1)
    307 1 << y
    308 
    309 2)
    310 0x1 << y	// Wrong
    311 
    312 
    313 M17: Avoid forward-declaration of static functions
    314 ==================================================
    315 
    316 Functions that are static should not be forward-declared.  The only exception
    317 to this rule is if a circular dependency condition exists, and the forward
    318 declaration cannot be avoided.
    319 
    320 M18: Use appropriate logging levels
    321 ===================================
    322 An appropriate log level should be used depending on the type of message
    323 being logged. Logging is done using the l_log APIs in ELL:
    324 
    325 l_error   An unexpected condition occurred. These are generally fatal to the
    326           current connection/protocol that is running but not generally to IWD's
    327           overall operation.
    328 l_warn    An unexpected, but non-fatal condition occurred
    329 l_notice  Should not be used directly. This log level is reserved for special
    330           event type notifications which is handled by iwd_notice().
    331 l_info    Information that is expected during normal operation. l_info's use
    332           should be very limited so non-debug logs are concise
    333 l_debug   General debugging. These can be used relatively freely but should
    334           provide some piece of useful information.
    335 
    336 O1: Shorten the name
    337 ====================
    338 Better to use abbreviation, rather than full name, to name a variable,
    339 function, struct, etc.
    340 
    341 Example:
    342 supplementary_service  // too long
    343 ss  // better
    344 
    345 
    346 O2: Try to avoid complex if body
    347 ================================
    348 It's better not to have a complicated statement for if. You may judge its
    349 contrary condition and return | break | continue | goto ASAP.
    350 
    351 Example:
    352 1)
    353 if (a) {  // worse
    354 	struct voicecall *v;
    355 	call = synthesize_outgoing_call(vc, vc->pending);
    356 	v = voicecall_create(vc, call);
    357 	v->detect_time = time(NULL);
    358 	DBG("Registering new call: %d", call->id);
    359 	voicecall_dbus_register(v);
    360 } else
    361 	return;
    362 
    363 2)
    364 if (!a)
    365 	return;
    366 
    367 struct voicecall *v;
    368 call = synthesize_outgoing_call(vc, vc->pending);
    369 v = voicecall_create(vc, call);
    370 v->detect_time = time(NULL);
    371 DBG("Registering new call: %d", call->id);
    372 voicecall_dbus_register(v);