Skip to content

The Sourcecode: Formatting and Style

Christoph Zurnieden edited this page Dec 5, 2019 · 5 revisions

Formatting

Basic Formatting

The basic formatting—the placing of braces, how many spaces and where, etc.—are done by a simple make astyle. The program astyle should be available in almost all Linux and BSD distributions and if that is not the case, is available at their home at Sourceforge.

Braces

All block statements besides the case in a switch-case shall be in braces, even one-liners.

Parentheses

  • Individual statements in an if expression shall be put in parentheses. Example:
       if ((a > 0) && (a < 42) && ((a & 0x1) == 0)) ...
  • The conditional statement in a ternary operator shall be put in parentheses. Example:
       a = (b < 0) ? -1 : 1;
  • No overly excessive fencing. Examples for excessive fencing:
       a = x + (y * z);
       b = (((a + (b / c)) - (d * e)) + (3 / 2));
       c = (x - y) - z;

Exception - Error handling

The error-handling is normally done with a goto to the end. That goto has to be at the same line as the error-check itself and without the curly braces. The goto shall be aligned. Example:

if ((err = mp_add(&a, &b, &c) != MP_OKAY)        goto LTM_ERR;
if ((err = mp_incr(&c) != MP_OKAY)               goto LTM_ERR;

The error-handling is also the only place which allows an assignment inside an if() clause, this shall not be done in "regular code".

Style

All static and private functions have to have the prefix s_.

Some style checks etc. can be done via executing the helper.pl Perl script as ./helper.pl -a