Coding Guidelines
Guidelines for Submitters
General Principles
Follow the Google Style Guide
besides exceptions found in this document for all C++
code.
Be aware that existing code may not always reflect current best practices or project preferences. The codebase evolves, and what was once standard may now be outdated. Refer to this document for up-to-date guidelines.
Naming Conventions
-
File names: use
snake_case
. Use.h
for headers. Keep reasonably short. -
Variable names: use
lower_case_names
. -
Class member variables: use
m_lower_case
. -
Class and methods names: use
PascalCase
. Avoid upper case acronyms, use OcpnFoo and NmeaFoo rather than OCPNFoo and NMEAFoo. -
Constants: Pascal case + a 'k' prefix:
kSomeConstantValue
C++ Practices
Use C++ standard library components over wxWidgets and old style C equivalents:
Avoid (wxWidgets, C) | Use Instead (C++ Standard Library) | |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Memory Management
-
Do not use raw pointers and
new
/delete
. The exception is when raw pointers are required by external libraries like wxWidgets. -
Use
std::unique_ptr
orstd::shared_ptr
for better memory management.
String Handling
-
Use double-quoted strings for unicode strings.
-
Do not use
_T()
orwxT()
macros. See wxWidgets string guidelines.
Exception handling
OpenCPN is compiled with exceptions enabled to handle third party libraries. However, most of OpenCPN is not exception safe. Thus:
-
Do not throw exceptions.
-
When using code which throws exceptions these should be caught as soon as possible
Code Documentation
-
Document code in doxygen format, see doxygen manual
-
Almost everything is documented in the header files.
-
Omit
@brief
and@details
tags. The first sentence automatically becomes the brief description. -
Avoid redundant phrases like "This class is…"
-
Test generated documentation locally. See
manual
sub-directory.
For an extensive example see rest_server.h, results in the API docs
Code Formatting
C/C++
The sources have a uniform coding formatting based on the Google Style.
The style is defined in the .clang-format file which when used by clang-format produces a correctly formatted source file.
Since the 5.10 release all source file modifications must be processed by clang-format to be accepted. The recommended way to do this is using a git pre-commit hook which applies formatting when committing changes
To do this, just install the pre-commit program from https://pre-commit.com/:
$ pip install pre-commit
and then, in the project top directory
$ pre-commit install
which will pick up the configuration from .pre-commit-config.yaml which is part of the project.
CMake
Cmake file uses formatting defined by the .cmake-format.yaml which when used with cmake-format produces correctly formatted files. This has been applied to all cmake/*.cmake files and CMakeLists.txt.
Using clang-format
clang is part of the llvm tools. These are usually installed using package managers like choco (Windows), brew (MacOS) or apt (Debian/Ubuntu).
To format a C, C++ or header file with clang-format with the project’s default configuration use:
clang-format -i <filename>
Using cmake-format
Install cmake-format as described in https://github.com/cheshirekow/cmake_format. To format a single file use
cmake-format -i <filename>
Guidelines for Reviewers
Performance and Compatibility
-
Ensure changes do not reduce performance.
-
Verify existing functionality and plugin compatibility are maintained.
Cross-Platform Considerations
-
Check for consistency across different platforms (Windows, Linux, macOS).
-
Be aware of platform-specific behaviors, especially for UI elements.