Historically, Blender codestyle was following the ‘macro’ naming convention (ALL_CAPS) for its constants (actual defines, and enum items).
Once C++ code started to be added to the project, part of the codebase has ‘silently’ switched to using regular variable names convention (snake_case) for some constants (in particular const expr ones), and/or the ‘class’ naming convention (PascalCase), especially for its enum class items.
There were already some discussion about this topic over one year ago, but no conclusion was reached.
We are getting more and more mixed style in the codebase, and need to solve this one way or the other at that point.
NOTE: This is somewhat a follow-up/rehashing/generalization of previous discussions like Style for enumerator values and its related PR.
Scope
This is a discussion to decide about a code style to enforce on compile-time constants (i.e. macro defines, constexpr and enum items). runtime constants (const keyword) are not considered here.
Note: each naming styles have various names, using wikipedia as reference here.
Potential Solutions
There are at least three ideas I am aware of currently:
- Go back to using
ALL_CAPS. - Move everything to
snake_case(and potentiallyUpperCamelCasefor enum items). - Use the ‘google’ style, i.e. using a prefix (
k):kCamelCase.
ALL_CAPS
This is the “historic Blender” style. All constant should use this same style.
Here is a mockup PR enforcing this style in blenkernel module.
Pros:
- Allows to immediately identify constant.
- Still followed by the majority of Blender codebase currently.
Cons:
- Does go against the C++ Core Guidelines, which reserves
ALL_CAPSto macros only.
snake_case & UpperCamelCase
These have been used in more recent Blender C++ code quite extensively. While not documented anywhere (to my knowledge), it seems to follow the following rules:
- macros and anonymous enums use
ALL_CAPS. constexprusesnake_case.- Scoped
enum classitems useUpperCamelCase.
Here is a mockup PR enforcing this style in blenkernel module.
Pros:
- Generally follows the C++ Core Guidelines.
Cons:
- Uses various styles for different types of constant.
- Does not immediately convey the ‘is a constant’ meta-information when reading code.
Google Style
This style is not used in Blender code currently. In a nutshell, it reuses the ‘Hungarian notation’ idea to mark all non-macro constant names with the k prefix, and camel case: kCamelCase.
Pros:
- Generally follows the C++ Core Guidelines.
- Allows to immediately identify constant.
Cons:
- Would be the most disruptive change in our codebase.
- Still two different notations for constant (
ALL_CAPSmacros andkCamelCasefor enum and constexpr).
Tooling
We should try to have automation to enforce these rules, once we agree on them. clang-tidy seems like a good candidate (see also the two mockup PRs linked above, which where generated using it).
This implies that we may be limited in our final style choices, by what the chosen tool can do.