Skip to content
Mateusz Piotrowski edited this page Mar 30, 2017 · 5 revisions

Functions

Arguments

The order of assertions

The assertions checking the arguments should be the same as the order of the arguments themselves.

int
fun(void *p, void *q, struct rstruct *r)
{
        assert(p != NULL);
        assert(q != NULL);
        assert(r != NULL);
        assert(r->rs_val != 0);
}

Declarations

Header files

Use tabs.

void    *function_a(const struct verylong_struct_name_here *vsnh, int a,
            int b);
int      function_b(void);

Source files

Use spaces.

static void *function_a(const struct verylong_struct_name_here *vsnh, int a,
    int b);
int function_b(void);

Headers

Order

  1. Kernel header files.
  2. Standard C Library header files.
  3. Other libraries header files.
  4. Local header files.

Local variables

  • All the local variables should be declared at the beginning of the function.

Order

char[1024]
struct linau_record
uintmax_t
uint64_t
void *
size_t
uint32_t
au_id_t
au_asid_t
uid_t
gid_t
pid_t
long
int
char
  • pid_t is int32_t.
  • uid_t is __uint32_t.

Indentation

Breaking too long lines

  • Break lines as far as you can.

  • Good

PJDLOG_VERIFY(au_close_buffer_tm(aud, aueid, buf, buflenp, tm) == 0 &&
    fun(aud));
  • Bad
PJDLOG_VERIFY(
    au_close_buffer_tm(aurecordd, aueventid, buf, buflenp, tm) == 0);
  • Break before the last two arguments if it improves readability.
PJDLOG_VERIFY(au_close_buffer_tm(aurecordd, aueventid, buf,
    buflenp, tm) == 0);

Newlines

Generally, never enter two newlines one after another like this:

#define ABC 1


static void localfun(int a, int b);

i++ vs ++i

Use i++ only. Never use it like this:

while (str2[j] != '\0')
    str1[i++] = str2[j++]

I use 8 spaces instead of tabs in this documentsince Markdown can mess up the indenetaion.