Constants
A constant is defined as a variable that can't be changed.
Constants are in ALL_UPPERCASE_WITH_UNDERSCORES (capitalized snake case)
Constants always have static final
public static final String THIS_IS_A_CONSTANT = "Constantly following the rules!";
Constants can be macros or const
variables. It is highly recommended to make constants constexpr
and inline
as well.
#define MACE_CONSTANT_INOPLE 3.45921321
namespace mc {
const constexpr int SPECIAL_NUMBER = 342131;
}
If using a macro as a constant, always prefix it with an underscore afterwards. This prefix should be unique to your project.
Functional macros follow the same rules as a normal constant. They must also be verbs.
#define MACE_STRINGIFY(name) #name
#define MACE_SUM(a, b) a + b
Functional macros with multiple lines must be wrapped in a do...while
block
#define MACE_DO_MULTILINE() do{ ... }while(0)
Global Constants
Try to avoid non-constant global variables.
Magic Constants
A magic constant is a literal in your code that doesn't have any meaning or explanation.
Instead of embedding literals into code, create a constant at the top of the file to reduce spatghetti code and allow for more configurability.
You can use #define
or const constexpr
constants.
BAD:
Renderer::getModel("my-model!");
GOOD:
#define MACE_MY_MODEL_LOCATION "my-model!"
...
Renderer::getModel(MACE_MY_MODEL_LOCATION);
Last updated
Was this helpful?