2
\label{sec:codingstyle}
5
To streamline the \dolfin{} source code and ease the job for
6
maintainers that need to read and edit large amounts of code,
7
developers should try to follow the below coding style when submitting
10
The guideline below is for C++ but may in some cases be extrapolated
13
\section{Naming conventions}
15
\subsection{Class names}
17
Use camel caps for class names:
25
\subsection{Function names}
27
Use lower-case for function names and underscore to separate words:
34
Functions returning a value should be given the name of that value,
41
/// Return size of array (number of entries)
47
In the above example, the function should be named \texttt{size} rather
48
than \texttt{get\_size}. On the other hand, a function not returning a
49
value but rather taking a variable (by reference) and assigning a value
50
to it, should use the \texttt{get\_foo} naming scheme, for example:
56
/// Retrieve all parameter keys
57
void get_parameter_keys(std::vector<std::string>& parameter_keys) const;
62
\subsection{Variable names}
64
Use lower-case for variable names and underscore to separate words:
71
\subsection{Enum variables and constants}
73
Enum variables should be lower-case with underscore to separate words:
75
enum Type {foo, bar, foo_bar};
78
We try to avoid using \texttt{\#define} to define constants, but when
79
necessary constants should be capitalized:
81
#define FOO 3.14159265358979
84
\subsection{File names}
86
Use camel caps for file names if they contain the
87
declaration/definition of a class. Header files should have the
88
suffix~\texttt{.h} and implementation files should have the
95
Use lower-case for file names that contain utilities/functions (not
98
\section{Miscellaneous}
100
\subsection{Comments}
102
Comment your code, and do it often. Capitalize the first letter and
103
don't use punctuation (unless the comment runs over several
104
sentences). Here's a good example from \texttt{TopologyComputation.cpp}:
106
// Check if connectivity has already been computed
107
if (connectivity.size() > 0)
110
// Invalidate ordering
111
mesh._ordered = false;
113
// Compute entities if they don't exist
114
if (topology.size(d0) == 0)
115
computeEntities(mesh, d0);
116
if (topology.size(d1) == 0)
117
computeEntities(mesh, d1);
119
// Check if connectivity still needs to be computed
120
if (connectivity.size() > 0)
126
\subsection{Integers and reals}
128
Use \texttt{dolfin::uint} instead of \texttt{int} (unless you really
129
want to use negative integers which is rare) and \texttt{dolfin::real}
130
instead of \texttt{double}:
135
These are typedefs for the standard C++ types \texttt{unsigned int}
136
and \texttt{double} (defined in \texttt{dolfin/common/types.h}).
138
\subsection{Placement of brackets}
140
Curly brackets following a control statement should appear in the next
141
line and not be indented:
143
for (uint i = 0; i < 10; i++)
149
\subsection{Indentation}
151
Indentation should be two spaces and it should be spaces, \emph{not}
154
\subsection{Header file layout}
156
Header files should follow the below template:
159
// Copyright (C) 2008 Foo Bar.
160
// Licensed under the GNU LGPL Version 2.1.
162
// Modified by Bar Foo, 2008.
164
// First added: 2008-01-01
165
// Last changed: 2008-02-01
173
class Bar; // Forward declarations here
175
/// Documentation of class
194
\subsection{Implementation file layout}
196
Implementation files should follow the below template:
198
// Copyright (C) 2008 Foo Bar.
199
// Licensed under the GNU LGPL Version 2.1.
201
// Modified by Bar Foo, 2008.
203
// First added: 2008-01-01
204
// Last changed: 2008-02-01
206
#include <dolfin/Foo.h>
208
using namespace dolfin;
210
//-----------------------------------------------------------
211
Foo::Foo() : // variable initialization here
215
//-----------------------------------------------------------
220
//-----------------------------------------------------------
223
The horizontal lines above should be exactly~79 characters
224
wide but have been shortened here to fit the page.
226
\subsection{Including header files}
228
Don't use \texttt{\#include <dolfin.h>} or \texttt{\#include
229
<dolfin/dolfin\_foo.h>} inside the DOLFIN kernel. Only include the
230
portions of DOLFIN you are actually using.
232
\subsection{Forward declarations}
234
Actually, try to include as little as possible and use forward
235
declarations whenever possible (in header files). Put the
236
\texttt{\#include} in the implementation file.
238
\subsection{Explicit constructors}
240
Make all constructors (except copy constructors) explicit if there is no particular
245
explicit Foo(uint i);
249
\subsection{Virtual functions}
251
Always declare inherited virtual functions as virtual in the subclasses. This makes it
252
easier to spot which functions are virtual.
258
virtual void bar() = 0;
268
\section{Use of libraries}
270
\subsection{Prefer C++ strings and streams to old C-style \texttt{char*}}
272
Use std::string instead of \texttt{const char*} and use std::istream and
273
std::ostream instead of \texttt{FILE}. Avoid \texttt{printf},
274
\texttt{sprintf} and the like.
276
There are exceptions to this rule where we need to use old C-style
277
function calls. One such exception is handling of command-line
278
arguments (\texttt{char* argv[]}).