Open3D style guide

Coding style

Consistent coding style is an important factor of code readability. Some principles:

  1. Code itself is a document. Name functions and variables in a way they are self explanatory.

  2. Be consistent with existing code and documents. Be consistent with C++ conventions.

  3. Use common sense.

We generally follow the Google C++ Style Guide, with a few modifications:

  • Use 4 spaces for indent. Use two indents for a forced line break (usually due to the 80 character length limit).

  • Use #pragma once for header guard.

  • All Open3D classes and functions are nested in namespace open3d.

  • Avoid using naked pointers. Use std::shared_ptr and std::unique_ptr instead.

  • C++11 features are recommended, but C++14 and C++17 are also accepted.

We also recommend reading the C++ Core Guidelines.

For Python, please use Google style guidelines, as shown here.

Style checker

Open3D’s CI checks for code formatting based on the style specified in .clang-format for C++ files and .style.yapf for Python files. Please build the check-style and apply-style CMake target before submitting a pull request, or use your editor’s clang-format and yapf integration to format the source code automatically.

Different clang-format versions may produce slightly different formatting results. For standardization, clang-format version 10 shall be used.

Install clang-format

By default, the make system tries to detect either clang-format-10 or clang-format from PATH.

Ubuntu

# Ubuntu 18.04
sudo apt update
sudo apt install clang-format-10
clang-format-10 --version

macOS

# Install from official brew formula.
brew install clang-format
clang-format --version

# (Optional) If you previously have a tagged version (e.g. clang-format@5)
# of clang-format installed, unlink the tagged version and link the new version.
brew unlink clang-format@5
brew link clang-format
clang-format --version

# (Optional) In case brew updates to a newer clang-format version, we also
# provide a tagged clang-format@10 backup formula.
curl https://raw.githubusercontent.com/intel-isl/Open3D/master/3rdparty/clang-format/clang-format%4010.rb -o $(brew --repo)/Library/Taps/homebrew/homebrew-core/Formula/clang-format@10.rb
brew install clang-format@10
clang-format --version

Alternatively, you may also download the clang-10 macOS package from LLVM Download Page, unzip and add the directory containing clang-format to PATH.

Windows

Download LLVM version 10 Windows package from LLVM Download Page. During installation, select the option which allows adding clang toolchains to PATH. After installation, open a CMD terminal and try

clang-format --version

Checking clang-format version

After installation, check clang-format’s version with:

# In most cases
clang-format --version

# Or, when installed as clang-format-10, e.g. on Ubuntu
clang-format-10 --version

Install YAPF

We use YAPF for Python formatting. Different YAPF versions may produce slightly different formatting results, thus we choose version 0.30.0 as the standard version to be used.

Install YAPF with

# For Pip
pip install yapf==0.30.0

# For conda
conda install yapf=0.30.0

You can also download YAPF and install it from source.

Checking and applying format

Ubuntu & macOS

After CMake config, to check style, run

# For c++/cuda/python/ipynb files
make check-style

# Or, only for c++/cuda files
make check-cpp-style

After CMake config, to apply proper style, run

# For c++/cuda/python/ipynb files
make apply-style

# Or, only for c++/cuda files
make apply-cpp-style

Windows

After CMake config, to check style, run

# For c++/cuda/python/ipynb files
cmake --build . --target check-style

# Or, only for c++/cuda files
cmake --build . --target check-cpp-style

After CMake config, to apply the proper style, run

# For c++/cuda/python/ipynb files
cmake --build . --target apply-style

# Or, only for c++/cuda files
cmake --build . --target apply-cpp-style