-
Notifications
You must be signed in to change notification settings - Fork 33
Style and Coding Standard
oktal3700 edited this page Oct 12, 2023
·
3 revisions
TODO: Need examples.
- Indent with 4 spaces. Don't use tabs.
- Don't indent braces. Indent code inside braces.
- Put braces on a separate line.
- Exception: closing brace of a lambda, braced-initializer, or do while block hugs the code on its right side.
- Exception: braces surrounding a braced-initializer or a single-statement block can go all on a single line.
- When braces are not on a separate line, put a space between them and adjacent tokens.
- No spaces around parentheses, square brackets, or angle brackets.
- Put a space between a compound statement keyword (if, for, while, etc.) and its opening parenthesis.
- Always use braces in a compound statement, even if they only enclose zero or one statement. (This helps avoid terrible mistakes.)
- Put a space between a binary operator and its operand. No space between a unary operator and its operand.
- When declaring a pointer or reference, put a space before the
*or&but not after. - Write
const charnotchar const.
- Use camel case.
- Namespaces, classes, and enums start with a capital letter. Functions and variables starts with a lower-case letter.
- Classes begin with a
C, except traits classes which begin with aT. - Data members begin with
m_. Static data members begin withs_. Const static data members begin withc_. Avoid underscores elsewhere. - Use American English spelling.
- Use
//!and/*!style Doxygen comments. - Each header and source file has a
//! \filedirective. - Document classes and non-member functions with
/*!. Document members with//!. - Document everything that is public or protected.
- Exception: it is not necessary to document a function that overrides a documented virtual function.
- Use
\cond,\private, or\internalfor undocumented implementation details at namespace scope. - Use
\copydocand/or grouping to avoid duplication. - Take note of syntax peculiarities when grouping.
-
astylecan be used with the following options:
--style=allman --indent=spaces=4 --indent-cases --indent-namespaces --indent-labels
--indent-preprocessor --min-conditional-indent=2 --pad-oper --pad-header --unpad-paren
--keep-one-line-blocks --keep-one-line-statements --convert-tabs --align-pointer=name
- Headers have include guards that are uppercase transformations of the header path with non-alphanumeric characters transformed to underscores.
- Include all needed headers before any substantive line of code. Include files in order from most specific to most generic. The first header included in a source file must be its corresponding header file.
- Include swift headers with
#include "". Include other headers with#include <>. Always use the full path of swift headers. - Use forward-declaration to avoid excessive includes.
- Don't
using namespacein headers. - Don't optimize prematurely. Don't pessimize prematurely.
- Utilize Qt and the standard library liberally. Prefer Qt containers.
- Use the Qt5 pointer-to-member syntax for connecting signals to slots.
- Respect
constcorrectness. - Avoid top-level const on by-value parameter types and return types of function declarations.
- Fix all compiler warnings.
- Pass small, trivial arguments by value. Pass large or non-trivial arguments by
constreference. - Don't allow uninitialized variables.
- Use RAII effectively.
- Use C++ features that are supported by all of our compilers (currently means C++17).
- Don't use
NULL. Usenullptrinstead. - Use
autoat your discretion. - Use
overridewhen overriding a virtual function. - Don't use
dynamic_castor C-style casts. - Avoid global variables. Static member variables are global variables.
- Avoid excessive coupling. Follow SOLID principles. Favor simplicity.
- Follow the rule of three/five/one/zero.
- A constructor should be
explicitif it does anything more than just initialise members from arguments. - If a class has a virtual function, its destructor must be virtual or protected.
- Avoid long-distance
friends. - Self-assignment must be safe.
-
mutableimposes a responsibility to ensure thread-safety. - Avoid virtual functions in class templates. They cause long compile times.
- If they are implemented, move and swap must be
noexceptif possible.
- Report errors and warnings to the user with
CLogMessageandCStatusMessage. - Be exception-agnostic. Avoid throwing exceptions in general, except for particular use cases where they are well suited.
- Validate data coming from outside the executable.
- Use
Q_ASSERTto document and enforce unrecoverable logical invariants. - Use
BLACK_VERIFYto document and enforce recoverable logical invariants. - Use
BLACK_AUDITto document and monitor recoverable invariants of external data.
TODO: Section missing
- Home
- API documentation (Doxygen generated)
- Future of swift
- Style and Coding Standard
- Release Checklist
- Build swift
- Run swift as a developer
- Knowledgebase
- External resources
- Open Research Questions
- Aviation
- Programming
- Simulation
- Architecture