Chi-Tech
|
In general we follow the Google C++ style guide. We now will dictate a few specific items. Refer to the image below for different text case-namings:
Let us say it here explicitly, all variable-, function-, method- names should be descriptive, avoid abbreviation, rather sacrifice horizontal space for readability.
-
in file names/usr/include
http_server_logs.h
rather than logs.h
. A very common case is to have a pair of files called, e.g., foo_bar.h
and foo_bar.cc
, defining a class called FooBar
.h
extension, c/c++ code files have a .cc
extension.math/Discretization/PWL
struct
members do not need trailing underscores. See more about this below.cell.centroid_
.struct
members should NOT be followed by an underscore.private/protected
unlessconst
.chi_mesh::cell
does not have accessor functions because cell.vertex_ids_
is faster than cell.GetVertexIDs()
private/protected
members should be obtained with a method call. A recommended convention is starting the method call with Get
, e.g., object.member
should be referenced with object.GetMember()
. This is only guidance. It is perfectly fine to have object.Member()
or anything that makes sense for the given use case.const
and non-const
getters judicially. Favor using setters over non-const reference if possible, e.g., setting a single value can be done with GetSingleValue
/SetSingleValue
, however, vector manipulation might be better done with GetVector
followed by manipulations.In order to save horizontal space standard indents are to be 2 spaces instead of 4 space equivalent tabs. Other than this the convention is flexible.
Curly braces, parentheses and block braces does not have a specific requirement other than being used in a sense that is optimal with respect to readibility.
Generally we require that code span a maximum of 80 characters. This is not a hard rule but will greatly enhance code reliability. Especially in split-screen configurations.
Constants should look like macros. Constants can be defined in macros, enumerations or within code segments.
Be consistent in using comments. Comments should be clear and concise and convey the algorithm. Every class, structure or function should be supplied with doxygen comment styles at the top of the item.
MAXIMIZE SCOPE MINIMIZE SPACE. Class declerations must always happen within header files and should aim to provide the maximum amount of scope within the minimum amount of vertical space. Member variables/structures should be on the top portion of the class decleration and methods should be on the bottom.
Header files are primarily used to make function/class/struct declarations available to other compile units without the need to compile the associated definitions. Unless you have a good reason to do so, do not define code within header files. It results in extra compile times and is considered not good practice. Of course there are exceptions like templating but in general code definitions should be contained in .cc files, header files are for code declaration only.
All header files should have #define guards to prevent multiple inclusion. The format of the symbol name should be _<CATEGORY>_<FILE>H. To guarantee uniqueness, they should be based on the full path in a project's source tree. For example, the file foo/src/bar/baz.h in project foo should have the following guard:
Avoid using forward declarations where possible. Just #include the headers you need.
Don't use the "using namespace" directive. Rather rename groups of namespaces. Use "typedef" where appropriate.
Each line of text in your code should be at most 80 characters long. A line may exceed 80 characters if it is
When you have a boolean expression that is longer than the standard line length, be consistent in how you break up the lines.
In this example, the logical AND operator is always at the end of the lines:
Constructor initializer lists can be all on one line or with subsequent lines indented four spaces.
The acceptable formats for initializer lists are:
Spacing between operators is flexible.
In general there are three variants of casting; C-style casting, static_cast
and dynamic_cast
.
The difference between the 3 are as follows: C-style casting is a brute-force way of casting one type into another. In most, if not all cases, the compiler will not provide any protection against casts that will be illegal. The better alternative is to use static_cast
which will allow the compiler to determine whether the cast would be legal in a broad scope sense (i.e. casting parent pointer to child pointer). If the user needs to check for a valid cast, it is often better to use dynamic_cast
which will return null if the cast is illegal, however, be aware that this uses RTTI and therefore has some cost. The following conventions therefore apply:
static_cast
or dynamic_cast