How to handle "simple" magic values?

238 Views Asked by At

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.

2

There are 2 best solutions below

0
On

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:

if (list.Count != 0)

if (list.Count > 0)

if (list.Count >= 1)

Neither 0 nor 1 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 the Any LINQ operator:

if (list.Any())

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.

2
On

As Allon Guralnek stated I also would use the Any() extension methods to check if a certain collection contains items. You can also write additional extension methods like

public static class MyExtensions {
  public static bool IsNegative(this int number) {
    return number < 0;
  }

  public static bool IsPositive(this int number) {
    return number > 0;
  }
}

And then write your loop or conditions as

if (Value.IsPositive())
while (Value.IsNegative())
while (MyQueue.IsPositive())

assuming Value and MyQueue are of type int.