-
Notifications
You must be signed in to change notification settings - Fork 292
Source code formatting conventions
John W. Peterson edited this page Jan 5, 2016
·
35 revisions
-
The contrib/bin/reindent.sh script is provided to automatically control the amount and type of indentation used in libMesh source files. It isn't perfect, but it's pretty good, and if you run this script on the files you've modified before submitting them, we won't have to run it on them later, artificially changing line ownership statistics.
-
Tab characters are not allowed in indentation, use only spaces. Most editors have a way of enforcing this convention, for instance in Emacs, add the following to your .emacs file:
(setq-default indent-tabs-mode nil)
- No trailing whitespace is allowed. Again, most editors have a way of removing trailing whitespace either manually (Emacs:
M-x delete-trailing-whitespace
) or automatically
(add-hook 'c-mode-hook
(lambda () (add-to-list 'write-file-functions 'delete-trailing-whitespace)))
- Every file should end with a single newline. That is, in the output of
git diff
, you should not see:
\ No newline at end of file
- Leave one space on either side of the
*
or&
character in pointer and reference declarations, respectively. Do not leave a space when dereferencing or taking the address of a variable.
unsigned int x = 42;
unsigned int * px = &x;
unsigned int & rx = *px;
- Function prototypes
- Template declarations should appear on a separate line.
- The function parameter list should begin on the same line as the function name.
- Return type may be on a separate line if desired.
- One space between function name and opening parenthesis, no space after opening parenthesis.
- For functions with long parameter lists, use one line per parameter (otherwise all on one line).
- Indent each parameter to the beginning of the parameter on previous line.
template<typename OutputShape>
UniquePtr<FEGenericBase<OutputShape> >
build_new_fe (const FEGenericBase<OutputShape> * fe,
const Point & p,
const Real tolerance = TOLERANCE) const;
- No space after the opening parenthesis in function prototypes.
void foo( int bar, float baz, char bob ); // NO!
- One space after commas in single-line function parameter lists:
void foo(int bar, float baz, char bob);
- Indent streaming print statements (if necessary, multiple
<<
operators on a single, short line is also OK) on the stream injection (<<
) operator:
oss << std::fixed
<< std::setprecision(4)
<< std::setw(tot_time_col_width)
<< std::left
<< summed_total_time;
- 'for' loops
- Single-line
for
loops should not use curly braces. - Start curly braces on a new line (similarly for
if
andwhile
loops). - Indent curly brace two spaces. (This indentation pattern reflects the current behavior of reindent.sh, but it may be changed at some point in the future.)
- Put spaces after semi-colons in the for loop declaration.
for (unsigned int i=1; i<=N; ++i)
{
sum += i;
prod *= i;
}