[vtk-developers] Coding Standards and the C preprocessor
Brad King
brad.king at kitware.com
Tue Feb 26 13:14:22 EST 2008
Sean McBride wrote:
> What are the coding standards when it comes to the C preprocessor?
> <http://www.vtk.org/Wiki/VTK_Coding_Standards> is silent on the issue.
>
> Specifically, can we clarify:
>
> 1) indentation. Should #if/#end blocks be indented? Whitespace before
> of after the #? I've seen:
>
> #ifdef foo
> #pragma bar
> #endif
>
> #ifdef foo
> # pragma bar
> #endif
>
> #ifdef foo
> #pragma bar
> #endif
>
> even in the same file. (My vote is for the 3rd because most code editors
> have a command to intent/outdent text, and that's how they do it.)
I always put # in the first column:
- it is easy to see which lines are preprocessing directives
- emacs does better syntax highlighting
- emacs does better indentation
(indenting a preprocessing region always puts # in the first column)
- rectangle-open can be used to update the indentation of large blocks
Of course this is heavily biased towards emacs (with my indentation
rules). Does anyone have comments on what other editors do in each case?
I also tend to use 1-character increments for indentation behind the #
to reduce cases when a line continuation character is needed:
#if condition1
# if condition2
# define def1 val1
# else
# define def1 val2
# endif
#else
# define def1
#endif
IMO the regularity of the # characters is visually pleasing and helps
delimit the entire block.
For comparison, this code:
#if condition1
#if condition2
#define def1 val1
#else
#define def1 val2
#endif
#else
#define def1
#endif
actually took me longer to indent than the version above. Automatically
re-indenting the entire source file un-indents all the lines.
> 2) do we prefer "#ifdef FOO" or "#if defined (FOO)"
I prefer the latter because the condition can be updated with less of a
change, and quickly disabled for temporary testing:
#if defined(FOO) && 0
-Brad
More information about the vtk-developers
mailing list