Changes between Initial Version and Version 1 of CodingStyle


Ignore:
Timestamp:
Feb 11, 2011, 8:15:13 AM (14 years ago)
Author:
Suren A. Chilingaryan
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • CodingStyle

    v1 v1  
     1== [Proposal] Coding Style ==
     2
     3In general, the [http://kerneltrap.org/files/Jeremy/CodingStyle.txt coding standards] used in the Linux kernel are versatile, comprehensive and quite good looking. They are boiling down to the following guide lines:
     4
     5 Indentation:: Hardtabs
     6 Spaces:: Spaces around keywords but not in inner parenthesis.
     7 Brace placement:: '{' at end of line except for function definition and '}' on its own line except in do-while-loops.
     8 Naming:: Descriptive for global functions and usually lower case with underscores.
     9 Macros:: Upper case for constants, lower case with underscores for macros resembling functions.
     10 Structures:: No typedefs, although this is debatable. For shared data structures reference counting should be implemented.
     11
     12I would add the following:
     13
     14 Position of C-pointer star:: At variable name not type.
     15 const:: Wherever possible.
     16 White space:: Avoid at all costs.
     17 C99:: Enable it and declare variables only when needed.
     18
     19There are still some open questions:
     20
     21* Number of white lines to separate functions?
     22* Comments: C vs. C++ style, usage of '*'?
     23* Alignment of multiple assignments?
     24* Breaking of long lines, especially argument lists?
     25* #define indentation? Commenting each #endif with the corresponding #define name makes the code hard to read in my opinion.
     26* Documentation tools? ([http://www.stack.nl/~dimitri/doxygen/ Doxygen], [http://sphinx.pocoo.org/ Sphinx], [http://www.naturaldocs.org/ Natural Docs])
     27* Name prefixes? (e.g. 'g_' for global)
     28
     29In essence, a bit of code would look like that:
     30
     31{{{
     32#!c
     33#define PI 3.14159265
     34#define min(a,b) ((a) < (b) ? (a) : (b))
     35
     36struct string {
     37    char *content;
     38    size_t length;
     39    int refcount;
     40};
     41
     42struct string* clone_string(const char *s)
     43{
     44    if (s == NULL)
     45        return NULL;
     46
     47    struct string *clone = (struct string *) malloc(sizeof(struct string));
     48    const int len = strlen(s);
     49    clone->content = (char *) malloc(len);
     50    clone->length = len;
     51    clone->refcount = 1;
     52    strncpy(clone->content, s, len);
     53
     54    return clone;
     55}
     56}}}