Every project has its coding style, and ConnMan is not an exception. This
document describes the preferred coding style for ConnMan code, in order to keep
some level of consistency among developers so that code can be easily
understood and maintained, and also to help your code survive under
maintainer's fastidious eyes so that you can get a passport for your patch
ASAP.
First of all, ConnMan coding style must follow every rule for Linux kernel
(http://www.kernel.org/doc/Documentation/CodingStyle). There also exists a tool
named checkpatch.pl to help you check the compliance with it. Just type
"checkpatch.pl --no-tree patch_name" to check your patch. In theory, you need
to clean up all the warnings and errors except this one: "ERROR: Missing
Signed-off-by: line(s)". ConnMan does not used Signed-Off lines, so including
them is actually an error. In certain circumstances one can ignore the 80
character per line limit. This is generally only allowed if the alternative
would make the code even less readable.
Besides the kernel coding style above, ConnMan has special flavors for its own.
Some of them are mandatory (marked as 'M'), while some others are optional
(marked as 'O'), but generally preferred.
M1: Blank line before and after an if/while/do/for statement
============================================================
There should be a blank line before if statement unless the if is nested and
not preceded by an expression or variable declaration.
Example:
1)
a = 1;
if (b) { // wrong
2)
a = 1
if (b) {
}
a = 2; // wrong
3)
if (a) {
if (b) // correct
4)
b = 2;
if (a) { // correct
}
b = 3;
The only exception to this rule applies when a variable is being allocated:
array = g_try_new0(int, 20);
if (!array) // Correct
return;
M2: Multiple line comment
=========================
If your comments have more then one line, please start it from the second line.
Example:
/*
* first line comment // correct
* ...
* last line comment
*/
M3: Space before and after operator
===================================
There should be a space before and after each operator.
Example:
a + b; // correct
M4: Wrap long lines
===================
If your condition in if, while, for statement or a function declaration is too
long to fit in one line, the new line needs to be indented not aligned with the
body.
Example:
1)
if (call->status == CALL_STATUS_ACTIVE ||
call->status == CALL_STATUS_HELD) { // wrong
connman_dbus_dict_append();
2)
if (call->status == CALL_STATUS_ACTIVE ||
call->status == CALL_STATUS_HELD) { // correct
connman_dbus_dict_append();
3)
gboolean sim_ust_is_available(unsigned char *service_ust, unsigned char len,
enum sim_ust_service index) // wrong
{
int a;
...
}
4)
gboolean sim_ust_is_available(unsigned char *service_ust, unsigned char len,
enum sim_ust_service index) // correct
{
int a;
...
}
If the line being wrapped is a function call or function declaration, the
preferred style is to indent at least past the opening parenthesis. Indenting
further is acceptable as well (as long as you don't hit the 80 character
limit).
If this is not possible due to hitting the 80 character limit, then indenting
as far as possible to the right without hitting the limit is preferred.
Example:
1)
gboolean sim_ust_is_available(unsigned char *service_ust, unsigned char len,
enum sim_ust_service index); // worse
2)
gboolean sim_ust_is_available(unsigned char *service_ust, unsigned char len,
enum sim_ust_service index);
// better
M5: Git commit message 50/72 formatting
=======================================
The commit message header should be within 50 characters. And if you have
detailed explanatory text, wrap it to 72 character.
M6: Space when doing type casting
=================================
There should be a space between new type and variable.
Example:
1)
a = (int *)b; // wrong
2)
a = (int *) b; // correct
M7: Don't initialize variable unnecessarily
===========================================
When declaring a variable, try not to initialize it unless necessary.
Example:
int i = 1; // wrong
for (i = 0; i pending);
v = voicecall_create(vc, call);
v->detect_time = time(NULL);
DBG("Registering new call: %d", call->id);
voicecall_dbus_register(v);
} else
return;
2)
if (!a)
return;
struct voicecall *v;
call = synthesize_outgoing_call(vc, vc->pending);
v = voicecall_create(vc, call);
v->detect_time = time(NULL);
DBG("Registering new call: %d", call->id);
voicecall_dbus_register(v);