Skip to content

Instantly share code, notes, and snippets.

@daerich
Last active July 29, 2022 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daerich/12e6e8d6586b31f370e8bf2c0ed1bee1 to your computer and use it in GitHub Desktop.
Save daerich/12e6e8d6586b31f370e8bf2c0ed1bee1 to your computer and use it in GitHub Desktop.
Coding Style

DaErich's coding style

For general advice see: Linux kernel coding style

The following amendments / clarifications are to be considered additionally:

  • Kernel-Normal-Form for Function-Blocks Within function blocks the protypes are to be formatted like in KNF Indentions are four spaced:
int
main(int argc, char** argv)
{
    return 0;
}
  • No C99 Comments I do not like them. They do not stand out and seem "hide" between regular code. They are ugly. Deal with it:
int crazyInt = 0 /* Perfectly stands out */
  • Local variables are to be declared AND initialized before statements in their respective block (as in C89) They start their live on entry of the block anyways, so why restricting yourself with scoping rules:
int 
myfunction(void)
{
    char buf[12] = {0};
    struct *statbuf = NULL;
    
    global = "ouf";
    call();
    return 1; 
}

(PSA: Initializing prevents undefined behavior) Also they are to be seperated by one newline from the rest of the control flow.

  • In contrast to the Linux kernel code, when expression are of moderate complexity, please spare the variable. I deem it elegant not to litter one's code with various steps of a calculation.
/* DO AVOID THIS */
int *res = ptr++;
*res = 5;
/* Just inline it */
*(ptr++) = 5;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment