Skip to content
Pedro A. Hortas edited this page Apr 25, 2015 · 17 revisions

Style

Indentation

  • Do not indent more than 4 times.
  • Use '\t' instead of white-spaces.
  • Refer to the following subsections to see the indentation style used on the project.

Functions

/* A function with a few arguments */
int func_args_few(int arg1, char *arg2, const char *arg3) {
        ...;

        /* Check for errors */
        if (error)
                return -1;

        /* All good */
        return 0;
}

/* A function with lots of arguments */
int func_args_many(
        int arg1,
        int arg2,
        char *arg3,
        const char *arg4,
        long arg5,
        unsigned long long arg6)
{
        ...;

        /* Check for errors */
        if (error) {
                return -1;
        }

        /* All good */
        return 0;
}

Switch / Case

switch (opt) {
        case ONE: {
                ...;
        }; break;
        case TWO: {
                ...;
        }; break;
        default: {
                ...;
        }; break;
 }

If / Else

/* Single */
if (cond1)
        ...;

/* Multiple */
if (cond1) {
        ...;
} else if (cond2) {
        ...;
} else {
        ...;
}

For / While

while (cond) {
        ...;
}

for (i = 0; cond; i ++) {
        ...;
}

Increments / Decrements

i ++;
-- i;

Assignments

var = value;

Compare

a < b
b > a
a <= b
b >= a
a == b
b != a

Arithmetic

a + b
b += a * c
d = (a + b) * c

Portability

Endianess

  • Always use stdint.h types (uint16_t, uint32_t, etc) on variables that can possibly be transmitted to other systems.
  • Always use htons(3), htonl(3), ntohs(3) and ntohl(3) on uint16_t and uint32_t type variables that are intended to be transmitted to other systems.

Standards

C99 / C11

  • Grant that your code conforms to C99 and/or C11 (pay special attention to strict aliasing rules).

POSIX.1-2001

  • Grant that your code conforms to, at least, POSIX.1-2001. If, for some good reason, a non-POSIX compliant optimization is used, that portion of code must be optional (and determined at compile time whether it'll be used or not).

External Libraries

  • The usage of external libraries (aside from uCodev libraries, libc and some other POSIX libraries) must be optional. Core features of the project must NOT depend on non-standard libraries.
  • If a new, non-core, feature is to be implemented and requires to be linked to non-standard libraries (or any other third-party objects), this feature must be optional and disabled by default.
Clone this wiki locally