Skip to main content

Coding style

F3D uses different coding styles in each component. However, there are some common rules.

C++

Common rules

Overall syntax:

  • CamelCase.
  • Avoid using using for namespaces.
  • Initialize variables in header when possible.
  • Local variables start with a lower case char.
  • Class members start with an upper case char.
  • Indents are two spaces.
  • One space between instruction and parenthesis.
  • Always add curly brace after instruction.
  • Curly braces on a new line, aligned with instruction.
  • Add //---------------------------------------------------------------------------- before any method in implementation.

Example:

//----------------------------------------------------------------------------
void class::method()
{
if (condition)
{
std::vector<int> myIntVector = { 13 };
}
}

Includes:

  • Organized by category: F3DApplication, libf3d, VTKExtensions, other deps, std, system.
  • Sorted inside category.

F3D application rules

  • Class starts with F3D
  • Method starts with an uppercase char.

libf3d rules

  • Class starts with a lower case char.
  • Method starts with a lower case char.

VTKExtensions rules

  • Follow VTK rules
  • Method starts with an uppercase char.
  • Class starts with vtkF3D if inheriting from vtkObject.
  • Class starts with F3D if not inheriting from vtkObject.

Automatic formatting

Some of the rules above are enforced using clang-format thanks to a .clang-format file. The continuous integration checks it whenever code is pushed to a pull request. To fix it locally, you can use:

  • single file: clang-format -i /path/to/file.ext
  • all files: shopt -s globstar; clang-format -i **/*.{h,cxx}

Please note there can be small discrepancy between the CI and local run of clang-format depending on the version in use. You may need to fix these manually.

Python

All Python code is formatted using Black style.

The continuous integration checks the formatting of Python code using black. You can fix it locally by running:

  • single file: black /path/to/file.py
  • all files: black --include '(\.py|\.py\.in)' .

Markdown and others

Markdown, JavaScript, JSON, HTML and YAML files are formatted using Prettier. For documentation prose, use the documentation style guide.

The continuous integration checks the formatting of all these files using prettier. You can fix them locally by running:

  • single file: prettier -w /path/to/file.ext
  • all files: shopt -s dotglob;shopt -s globstar; prettier -w **/*.{ts,tsx,js,json,md,html,yml}