-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Code Style
Nick Bolton edited this page Jul 31, 2024
·
52 revisions
Our code formatting is enforced via CI which runs the scripts/lint_cmake.py
and script/lint_clang.py
scripts to ensure that all files match our code style. If your IDE doesn't support formatting, you can use --format
on either of those scripts to format your code.
- CLang (C/C++/Objective-C):
.clang-format
- LLVM with a few minor tweaks. - CMake:
cmake-format.yaml
- Standard with a few minor tweaks. - Python: Use black
- Member variables should be prefixed with
m_helloWorld
- Pointers should begin with a lower case
p
, e.g.m_pHelloWorld
- Getters should not have
get
prefixed, e.g.helloWorld()
- Setters should have
set
prefixed, e.g.setHelloWorld(...)
- Enum values and constants should begin with
k
, e.g.kHelloWorld
- Class filenames should be pascal case, e.g.
HelloWorld.cpp
- Files with many classes or functions should be snake case, e.g.
hello_world.cpp
- Unit test names should follow the function-input-output pattern, e.g.
helloWorld_fooIn_barOut
- The word "deps" should be used to mean "dependencies"
- Qt controls should be named
m_p<type><description>
, e.g.m_pLabelSpecificThing
- Qt signals should indicate that something happened, e.g.
somethingHappened
- Qt slots should always begin with
on
- Manual Qt slots should not follow the autoconnection convention (
on_foo_bar
) - Manual Qt slots should include both the signal origin and signal name, e.g.
onOriginSomethingHappened
-
.cpp
files should be ordered:- Copyright
- Headers
using
- Constants
- Free functions
- Class members
- Headers should be ordered in separate groups of:
- Header file for the
.cpp
file - For Qt, the
_ui.h
file - Project header files
- 3rd party lib and system headers
- Header file for the
- Getters should be grouped together
- Setters should be grouped together
- Getters and setters should not be paired
- Use ctor dependency injection (e.g.
std::function
or aDeps
struct) - A unit test body should follow the AAA (arrange-act-assert) pattern, e.g.:
auto foo = "hello"; auto bar = "world"; auto baz = combine(foo, bar); EXPECT_EQ(baz, "hello world");
- A unit test should test only one scenario and expectation
- Split test scenarios into separate test functions
- Do not allow exceptions to propagate to Qt (instead, use
qFatal()
)- Remember:
qFatal
can be ignored on Windows, but not on macOS/Linux
- Remember:
- Do use exceptions for exceptional situations (things that should not happen)
- Do not use exceptions for validation (e.g. if a user input value is unexpected)