Coding Conventions

CACAO uses a coding style similar to the Linux kernel coding style.

Important differences are:

   1 if (first) {
   2     ...
   3 }
   4 else if (second) {
   5     ...
   6 }
   7 else {
   8     ...
   9 }

Comments

/!\ Please comment your code! Not after writing it, but while you write it. /!\

Comments should explain why the code does something a certain way. Also, comments should give an overview over sections of code, so for example you only have to read one comment line to know what happens in the following block of ten C statements.

Comments should be formatted with a blank line above and below them:

   1 statement;
   2 
   3 /* Do ... because ... */
   4 
   5 work, work, work;
   6 yadda;
   7 yadda;
   8 yadda;
   9 

Comments on data structures are very important! Please comment each field of each struct, in addition to a general comment on the struct:

   1 /* lock_record_t ***************************************************************
   2 
   3    Lock record struct representing an inflated ("fat") lock.
   4 
   5 *******************************************************************************/
   6 
   7 struct lock_record_t {
   8         struct threadobject *owner;          /* current owner of this monitor */
   9         s4                   count;          /* recursive lock count          */
  10         pthread_mutex_t      mutex;          /* mutex for synchronizing       */
  11         lock_waiter_t       *waiters;        /* list of threads waiting       */
  12         lock_record_t       *nextfree;       /* next in free list             */
  13 };

Every function should be preceeded by a comment explaining the intent of the function (in imperative form) and the detailed meaning of in/out parameters and the return value. Error handling semantics should also be clarified here:

   1 /* resolve_class_from_typedesc *************************************************
   2  
   3    Return a classinfo * for the given type descriptor.
   4   
   5    IN:
   6        d................type descriptor
   7        checkaccess......if true, access rights to the class are checked
   8        link.............if true, guarantee that the returned class, if any,
   9                             has been linked
  10    OUT:
  11        *result..........set to result of resolution, or to NULL if
  12                         the reference has not been resolved
  13                         In the case of an exception, *result is
  14                         guaranteed to be set to NULL.
  15   
  16    RETURN VALUE:
  17        true.............everything ok 
  18        false............an exception has been thrown
  19 
  20    NOTE:
  21        This function always resolves eagerly.
  22    
  23 *******************************************************************************/
  24 
  25 bool resolve_class_from_typedesc(typedesc *d, bool checkaccess, bool link, classinfo **result)

Naming

Boilerplate Code

Every source file should have the following boilerplate code at the bottom:

   1 /*
   2  * These are local overrides for various environment variables in Emacs.
   3  * Please do not remove this and leave it at the end of the file, where
   4  * Emacs will automagically detect them.
   5  * ---------------------------------------------------------------------
   6  * Local variables:
   7  * mode: c
   8  * indent-tabs-mode: t
   9  * c-basic-offset: 4
  10  * tab-width: 4
  11  * End:
  12  * vim:noexpandtab:sw=4:ts=4:
  13  */

cacaowiki: CodingConventions (last edited 2006-05-17 11:08:31 by EdwinSteiner)