I am replacing my #defines, for instance #define NUM_SLIDER_POSITIONS 5
for constant variables. Should I keep the old naming like:
const unsigned int NUM_SLIDER_POSITIONS = 5;
Or should I use something more like:
const unsigned int kNumSliderPositions = 5;
.
EDIT: The post has been put on hold, but anyway I'd like to sum up your answers:
Other option would be using underscores as a separators using lower case letters:
const unsigned int num_slider_positions = 5;
Constant identifier.
Regarding the use of a prefix as a way of identifying constants , the most common options are not using it, as it may not add relevant information:
const unsigned int num_slider_positions = 5;
Use a "k" before the name:
const unsigned int k_num_slider_positions = 5;
Or declaring the variable inside a class or namespace, in order to avoid polluting the global scope and providing a more self-explanatory name:
namespace defaults // or "config", or "settings" or something like that
{
const unsigned int num_slider_positions = 5;
}
Client code:
int slider_positions = defaults::num_slider_positions;
Primary constants − Integer, float, and character are called as Primary constants. Secondary constants − Array, structures, pointers, Enum, etc., called as secondary constants.
Each language has its own naming conventions that specify the use of programming tokens. Camel case (ex: camelCase) is usually for variables, properties, attributes, methods, and functions. Snake case (ex: snake_case) is commonly used in scripting languages like Python and as a constant in other C-styled languages.
Constants should be written in uppercase characters separated by underscores. Constant names may also contain digits if appropriate, but not as the first character.
I am replacing my #defines for constant variables.
Kudos! :)
Should I keep the old naming like: [all-caps]
If the coding conventions of your project designate constants to be in all-caps, you should (as it spares you an effort). Otherwise, you should not (because it will be confusing later, for maintenance).
Or should I use something more like: [bastardized hungarian convention]
This is up to you. Personally I do not like to add weird letters for my constants, because when reading the code - or writing it - I do not care much that they are constant (and if I try to write into them, the compiler will let me know).
My (personal) choice would be to use a namespace for providing context (instead of a prefix), along these lines:
namespace defaults // or "config", or "settings" or something like that
{
const unsigned int num_slider_positions = 5;
}
Client code:
int slider_positions = defaults::num_slider_positions;
I find this to be a superior alternative, because the context is more self-explanatory (than a "k" in front of it, or a "g" or a whatever else).
It's up to you for any name convention. But for C++ code you may also consider putting constants inside a class that use it, instead of pollute the global scope.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With