I think this is fairly language-independent, but if I'm wrong, then go for C# (or C or C++).
With "simple" magic values I mean things like this:
if (Value > 0)
or
while (Value < 0)
or
while (MyQueue > 0)
While writing this (pseudo-code above) it kinda struck me that it really only applies to something being compared to 0.
Anyway, what's the best way to handle these sort of magic values (considering readability, amount of keystrokes/code to create and name)?
It feels like extreme overkill having an entire (static) class (or enum, in C#) dedicated to this.
Certain numbers are only considered "magic numbers" by their context. Some usages embody a simple concept that has nothing to do with the specific value of the number. For example, if you want to check if a list is not empty you might write one of the following statements:
Neither
0
nor1
has any meaning beyond 'nothing' and 'something', and so the above three statements should be read as "not nothing", "more than nothing" and "at least something", and therefore I wouldn’t call their usages "magic numbers". There may be other ways to perform such a check without using any numbers at all. For example, in C# you could use theAny
LINQ operator:I find this to be more descriptive and enable story-like readability of code. Other languages may have other facilities to express concepts such as 'nothing', 'something', 'empty set', 'non-empty set', etc.