NASPRO - The 'NASPRO Architecture for Sound PROcessing'

User login

Who's online

There are currently 0 users and 0 guests online.

Syndicate

Syndicate content

Valid XHTML 1.0 Strict
Valid CSS
Viewable with Any Browser

Formatting

Line length

Every single code line should not exceed the 80 characters limit, since the ISO/ANSI screen size is 80x24 characters wide and exceeding that can cause readability problems to users of such terminals.

Indentation

Indentation of code blocks is always 8 characters long and is done by tabs. The code that has to be indented is the one between braces ({, }) or that can be put, reasonably, into braces. An example:

for (i = 1; i < N; i++)
	v[i] = v[i - 1] * 2;

if (v[N - 1] > 1000)
	printf("Big number!\n");
else
  {
	printf("Little number!\n");
	v[N - 1] += 1000;
  }

As you can see, we put open braces on a line by themselves, indented by two white spaces. This rule applies for every construct apart from function bodies, where we put no indentation on the braces. It goes like this:

NACORE_PUBLIC_FUNC(int,
my_func, (int a, int b))
{
	...
}

For switches it goes like this:

switch (c)
  {
	case 'A':
		i--;
		break;
	case 'B':
		i++;
		break;
	case 'C':
		i *= 2;
		break;
	default:
		i /= 2;
		break;
  }

While this is our style for goto labels:

static int
div_positive(int a, int b)
{
	if (b <= 0)
		goto error;
	if (a < 0)
		goto error;

	return a / b;

error:
	printf("ERROR!\n");
	return -1;
}

And for preprocessor directives

#ifdef HAS_FOO
# include <foo.h>
# ifdef HAS_BAR
#  define MYLIB_BAR_DEBUG
#  incluude <bar.h>
# endif /* HAS_BAR */
#else /* HAS_FOO */
# include <none.h>
#endif /* HAS_FOO */

Vertical and horizontal spaces

In the previous examples you could already have noticed some other conventions being used for spaces. Let's summarize them:

  • when declaring functions and function prototypes, the return type, together with all keywords like extern, static, etc., should be put on a line on its own; on the second line goes the name of the function and the parameters' list;
  • when using or declaring functions, function prototypes or macros, there should be no space between the function/macro name and the bracketed list of arguments (when not using visibility macros, in which case this is impossible), as well as there should be no space between brackets and the first/last argument;
  • commas between function and/or macro arguments should be placed near the previous argument and a white space should follow them; the same is true for the semicolons in for loops;
  • when evaluating expressions, operators should be one space character away from operands (only exception: unary operators);
  • when using or declaring arrays, square brackets should be placed near to the array name;
  • when using or declaring pointers, the * should be put near to the pointer name and not to the type name; in the case of sizeofs and similar constructs, the * has to be put one white space away from the type name as well;
  • code blocks doing different things should be separated by a newline; the same goes for separating variable declarations and code inside a function's body or to separate different functions, etc.;
  • when using constructs like if, while, for, switch, a white space has to be left between the statement and the brackets.

Furthermore, sometimes it is impossible to respect the 80 characters limit. In the case of parameters, they could be split like this:

my_func(this_function, takes, a, very, long_list, of, params,
        so, we, split, them, this_way);

For strings:

printf("This printf() call prints 3 values on the screen, "
       "a: %d, b: %d, c: %d\n", a, b, c);

For comments:

/* This is an incredibly long and clueless sentece to show how
 * lines should be split in comments. */

And, in the end, for expressions:

voltage = (sigma / (2.0 * EPSILON_0))
          * (sqrt(pow(z, 2) + pow(R, 2)) - z);

Notice that the line has to be split before the operator, not after. Then you have to respect also bracket nesting, like in the following case:

voltage = (lambda / (4.0 * PI * EPSILON_0))
          * log((length
                 + sqrt(pow(length, 2)
                        + pow(distance, 2)))
                / distance);
Copyright © 2007, 2008 Stefano D'Angelo