k9core/STYLE.org

3.3 KiB

k9core coding style

k9core coding style

Pretty similar to the GNU code standards.

Functions definition

Functions have to begin with return type, breakline, and functionname, also brackets will be in a new line:

int
main(int argc, char *argv[])

Variables

Naming style for variables is snake_case.

In declaration of pointers the * is adjacent to the name, not to the type:

int* p /* wrong */
int *p /* right */
int* p, q /* Confusing, p is a pointer while y is not */
int *p, q /* Not confusing, clearly p is a pointer and y is not */

Line length

A line of code must no exced 75 characters. If it exceeds, do a breakline:

int
a_very_long_function(int a_very_long_parameter, int
                    another_very_long_parameter);

Statements

Do not do declarations inside an if block. Declarations inside while loop are OK.

int fd;
if((fd = open("file", O_CREAT)) == -1) /* Don't do this */
{
/* Whatever */
}

/* Do this instead */

int fd = open("file", O_CREAT);
if(fd == -1)
{
   /* Whatever */
}

Do not include .c files

Just don't.

Comments

Do not use C++ style comments. Comments at beggining of the line should explain what the line does. not how it does. Comments at the end of the line must be explanations of something:

// This is a wrong comment

/* This is a correct comment */

/* Makes a syscall giving a file descriptor... */

fd = open("whatever",O_RDONLY); /* WRONG */

/* Creates a file descriptor on the file whatever */

fd = open("whatever",O_RDONLY); /* Yes */

/* 420 is 644 in decimal */ <- WRONG
chmod("whatever",420)

chmod("whatever",420) /* 420 is 644 in decimal */

For commiters

Commiters may or may not be anonymous. If you want to be anonymous. Set whatever you want to git name and git email.

If you don't want to be anonymous, you should sign your commits using gpg. See https://docs.gitlab.com/ee/user/project/repository/gpg_signed_commits/

C Standard

Development on k9core is to be done in C99 with possible backwards compatiability with ANSI C. Using c11 or gnu11 or something like that is to be avoided.

Examples

This is okay to use:

#include <stdio.h>
/* Valid C99 */

int
main
{
puts("This will be executed");
return 0;
}

This is not:

#include <stdio.h>
#include <stdlib.h>
#include <stdnoreturn.h>

/* This is not valid c99 */

noreturn void
stop(int i)
{
if(i > 0) exit(i);
else      exit(1);
}

int
main()
{
puts("This will be executed");
stop(0);
puts("This will not be executed");
}