Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming constants in C++ [closed]

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;
like image 751
Daniel Avatar asked Nov 04 '14 14:11

Daniel


People also ask

What are the 3 constants used in C?

Primary constants − Integer, float, and character are called as Primary constants. Secondary constants − Array, structures, pointers, Enum, etc., called as secondary constants.

Does C use snake case or camelCase?

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.

What is the naming convention for constants?

Constants should be written in uppercase characters separated by underscores. Constant names may also contain digits if appropriate, but not as the first character.


2 Answers

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).

like image 52
utnapistim Avatar answered Oct 03 '22 05:10

utnapistim


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.

like image 36
Non-maskable Interrupt Avatar answered Oct 03 '22 03:10

Non-maskable Interrupt