wiki:Hacking

Version 8 (modified by angel_il, 13 years ago) (diff)

--

Code Style

  • Maximum Line Width is 100 Characters. The reason is not about people with low-res screens but rather sticking to 100 columns prevents you from easily nesting more than one level of if statements or other code blocks.
  • Use 4 Space Tabs to Indent. With whitespace fillers.
  • No Trailing Whitespace
  • Follow the GNU-Style guidelines. We won't go through all of them here.

Please use the same indentation as other developers. Follow the style used in GNU Midnight Commander:

indent --gnu-style --format-first-column-comments --indent-level4 --brace-indent0 --line-length100 --no-tabs --blank-lines-after-procedures

or in short notation:

indent -gnu -fc1 -i4 -bli0 -nut -bap -l100

Programming Tips

Comments

Comments should always use the standard C syntax. C++ style comments are not currently allowed.

The lines before a comment should be empty. If the comment directly belongs to the following code, there should be no empty line after the comment, except if the comment contains a summary of multiple following code blocks.

This is right:

    int i;

    /*
     * This is a multi line comment,
     * Delete '\n' char.
     * Note that edit_delete() will not corrupt anything if called while
     *   cursor position is EOF.
     */
    (void) edit_delete (edit);

     /*  This is a one line comment. Allocate additional memory.  */
     mem = (char *) malloc (memneed);

    /**
     * @brief This is a doxygen comment.
     *
     * This is a more detailed explanation of
     * this simple function.
     *
     * @param[in]   param1     The parameter value of the function.
     *
     * @param[out]  result1    The result value of the function.
     *
     * @return              0 on success and -1 on error.
     */
    int example(int param1, int *result1);

This is wrong:

    //This is a one line comment.

    /*This is a one line comment.*/

    /* This is a multi line comment,
       with some more words...*/

typical structure of *.c file

/*** global variables ****************************************************************************/
/*** file scope macro definitions ****************************************************************/
/*** file scope type declarations ****************************************************************/
/*** file scope variables ************************************************************************/
/*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */