there is something to be said for “useless” replacement of pseudomagic numbers like BYTES_PER_KBYTE, which is that there’s a single, well-labelled place you can check that you have not typoed the number, and the compiler will catch typoes in all other references to it.
that was the OP’s point; bytes_per_kbyte is not a “magic number”, it’s a constant that’s not going to change and should be obvious from context. i’m arguing that even given that, there’s value in having a compiler-checked token for it.
I actually think it’s better to have these so-called “magic number”s in your code if they’re only ever used in one place and they’re not logically related to anything else whatsoever (i.e. they’re arbitrarily chosen). I think it actually conveys their purpose better and the code becomes easier to read overall.
With these unique magic numbers, there’s no danger, really. If you need to change it, you just change it, and if you need it somewhere else, then you give it a name.
I expected a comment along these lines :) The point is, even you name your constants, you still need the same trick to discover that someone else has already done that.
there is something to be said for “useless” replacement of pseudomagic numbers like
BYTES_PER_KBYTE
, which is that there’s a single, well-labelled place you can check that you have not typoed the number, and the compiler will catch typoes in all other references to it.It also makes thing easier when the inevitable happens and the “constant” changes.
that was the OP’s point; bytes_per_kbyte is not a “magic number”, it’s a constant that’s not going to change and should be obvious from context. i’m arguing that even given that, there’s value in having a compiler-checked token for it.
Fair enough.
I actually think it’s better to have these so-called “magic number”s in your code if they’re only ever used in one place and they’re not logically related to anything else whatsoever (i.e. they’re arbitrarily chosen). I think it actually conveys their purpose better and the code becomes easier to read overall.
With these unique magic numbers, there’s no danger, really. If you need to change it, you just change it, and if you need it somewhere else, then you give it a name.
Everything starts out being used once. The trick is to discover when you are adding a 2nd instance of it.
I expected a comment along these lines :) The point is, even you name your constants, you still need the same trick to discover that someone else has already done that.
Good point.