Reputation: 10931
I have an example code like this, in which the literal 1
repeats several times.
foo(x - 1);
y = z + 1;
bar[1] = y;
Should I define a constant ONE
, and replace the literals with it?
constexpr int ONE = 1;
foo(x - ONE);
y = z + ONE;
bar[ONE] = y;
Would this replacement make any performance improvement and/or reduce machine code size in the favor of reducing code readability? Would the number of repeating of the literal change the answer?
Upvotes: 0
Views: 661
Reputation: 7814
It will not bring you any performance/memory improvements. However, you should try to keep your code clean from magical numbers. So, if there is a repeated constant in your code in several places, and in all those places this constant is the same from logical point of view, it would be better to make it a named constant.
Example:
const int numberOfParticles = 10; //This is just an example, it's better not to use global variables.
void processParticlesPair(int i, int j) {
for (int iteration = 0; iteration < 10; ++iteration) {
//note, that I didn't replace "10" in the line above, because it is not a numberOrParticles,
//but a number of iterations, so it is a different constant from a logical point of view.
//Do stuff
}
}
void displayParticles() {
for (int i = 0; i < numberOfParticles; ++i) {
for (int j = 0; j < numberOfParticles; ++j) {
if (i != j) {
processParticlesPair(i, j);
}
}
}
}
Upvotes: 3
Reputation: 169
In assembly, one constant value generally takes the same amount of memory as any other. Setting a constant value in your source code is more of a convenience for humans than an optimization.
In this case, using ONE in such a way is neither a performance or readability enhancement. That's why you've probably never seen it in source code before ;)
Upvotes: 0
Reputation: 942
Depends. If you just have 1
s in your code and you ask if you should replace them: DONT. Keep your code clean. You will not have any performance or memory advantages - even worse, you might increase build time
If the 1
, however, is a build-time parameter: Yes, please introduce a constant! But choose a better name than ONE
!
Upvotes: 1
Reputation: 3415
Replacing literals with named constants make only sense,
if the meaning of the constant is special. Replacing 1
with ONE
is
just overhead in most cases, and does not add any useful information
to the reader, especially if it is used in different functions (index, part of a calculation etc.). If the entry 1 of an array is somehow special, using a constant THE_SPECIAL_INDEX=1
would make sense.
For the compiler it usually does not make any difference.
Upvotes: 0
Reputation:
Should I define a constant
ONE
, and replace the literals with it?
No, absolutely not. If you have a name that indicates the meaning of the number (e.g. NumberOfDummyFoos
), if its value can change and you want to prevent having to update it in a dozen locations, then you can use a constant for that, but a constant ONE
adds absolutely no value over a literal 1
.
Would this replacement make any performance improvement and/or reduce machine code size in the favor of reducing code readability?
In any realistic implementation, it does not.
Upvotes: 0