-
Notifications
You must be signed in to change notification settings - Fork 12
Coding guidelines
Indentation
Please use tabulations to indent your code, and white spaces to draw ascii art.
It means that if you want to align something to the left you must use tabulation, and if you want to aligne something to the right you must use white space.
Please never mixes tabulations and white spaces for indentation.
Good:
void function (void) {
⇨gint integer;␣␣␣␣␣// some comments
⇨gchar character;␣␣// here
⇨while (test) {
⇨ ⇨do_something();
⇨}
}
Bad:
void function (void) {
␣␣gint integer; ⇨ ⇨ // some comments
␣␣gchar character;␣␣// here
␣␣while (test) {
⇨ ⇨do_something();
␣␣}
}
Very Very Bad:
void function (void) {
␣␣gint integer; ⇨ ⇨ // some comments
⇨gchar character;␣␣// here
␣␣while (test) {
⇨␣␣do_something();
␣␣}
}
Comments
CPlusPlus-like comments beginning with //
are tolerated.
Good:
/* This is a comment,
* you see?
*/
// This is another comment.
Braces
Please put the opening left brace at the end of the previous line.
Never write a for
, while
, if
, and else
block without braces.
Never write a for
, while
, if
, else
block in only one line.
If a for statement has an empty block, please write a block with a comment in the empty block explaining the implicit behavior.
Good:
void function (void) {
gint it;
if (test) {
do_something();
}
else if (another_test) {
do_somethingelse();
}
else {
return;
}
for (it = 0; do(&it); it++) {
// 'for' does it
}
}
Bad:
void function (void)
{
gint it;
if (test)
{
do_something();
}
else if (another_test)
do_somethingelse();
else { return };
for (it = 0; do(&it); it++);
}
Very very bad:
for (i = 0; i < max; i++)
if (i == j)
return i;
Parenthesis and spacing
Please leave a white space before if
, for
and while
and the opening left parenthesis.
Please do not write white space between a function name and the opening left parenthesis.
Please do not put extra white space inside parenthesis.
Please write a white space to the left and the right of an operator.
There is a special cases with for ++
and --
operator because it can be seen as a whole operation (you can write something like increment(&a)
instead of a++
), you can't visually distinguish the addition and the affectation since it's all-in-one operation, so please see it as a whole operation.
The idea is to distinguish condition, affectation, and operation that is not an affectation.
Good:
void function(gint it) {
gpointer n = NULL;
gint m = 0;
gint p;
if (this && that) {
do(it);
}
else if (i < j) {
let(it);
}
n = f(it)->well;
m = 5 + 2;
p++;
}
Bad:
void function (gint it) {
gpointer n=NULL;
gint m=0;
gint p;
if(this && that) {
do (it);
}
else if (i<j) {
let( it );
}
n=f (it)->well;
m = 5+2;
p ++;
}
Commas, semicolons and spacing
Please leave one white space after a comma or a semicolon, but please do not write any white spaces before them.
Good:
for (i = 0; i < max; i++) {
function(i, j, k);
}
Bad:
for (i = 0 ;i < max;i++) {
function(i,j ,k);
}
Incrementation
Please write different lines for incrementation and affectation.
For the special +=
and ++
cases, please write only one affectation per line.
For the +=
case where we can't distinguish the operation and the affectation, see it primary as an affectation, and secondly as an incrementation, so please put spaces before and after +=
.
For the ++
case, please see it primary as an operation (see above), so please do not write space before ++
.
Also remember that if var++
is equivalent to var += 1
, *var++
is not equivalent to *var += 1
, the first means *(var += 1)
, the second means (*var) += 1
, first increment the pointer, second increment the value. If *var
is a pointer to a const
type, the second form is not valid. It's safe to not write code that have implicit meanings like that.
Good:
it++;
do(it);
do(more);
more--;
c = a;
a += b;
Bad:
do(++it);
do(more--);
c = a += b;
Very very bad:
*dst++ = *src++;
Splitted lines
Please do not split lines if they are not too long to be splitted two times or more (three lines or more). The 80 characters limit is an editor rendering task. Splitting can be use to distinguish significant parts (like verses in poetry), not to draw columns.
Good:
printf("My bounty is as boundless as the sea,"
"My love as deep; the more I give to thee,"
"The more I have, for both are infinite.");
printf("Do you bite your thumb at us, sir?");
this_long_variable = "has a not a very very long value";
Bad:
printf("My bounty is as boundless as the sea, My love as deep; the more I"
"give to thee, The more I have, for both are infinite.");
printf(
"Do you bite your thumb at us, sir?");
this_long_variable =
"has a not a very very long value";
Good to know
All the bad examples presented here were real before a large clean-up was done. Many are still real today.
All the code does not currently follow these guidelines, if you change something in the code and if this part does not follow these guidelines, do not hesitate to fix the appearance too.
Rules are made for humans.
Rules are not absolute.