14

In A Tour of C++ by Bjarne Stroustrup, some advice is listed at the end of each chapter. At the end of the first chapter one of them reads:

Avoid ‘‘magic constants;’’ use symbolic constants;

What are magic and symbolic constants?

somerandomdude
  • 337
  • 3
  • 15

3 Answers3

23
somethingElse = something * 1440;           // a magic constant
somethingElse = something * TWIPS_PER_INCH; // a symbolic one

The first is an example of the magic constant, it conveys no other information other than its value.

The latter is far more useful since the intent is clear.

Using symbolic constant also helps a great deal if you have multiple things with the same value:

static const int TWIPS_PER_INCH = 1440;
static const int SECTORS_PER_FLOPPY = 1440; // showing my age here :-)

That way, if one of them changes, you can easily identify which single 1440 in the code has to change. With magic 1440s scattered throughout the code, you have to change it in multiple places and figure out which are the twips and which are the sectors.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 1
    I just stumbled over "...`1440; // showing my age here :-)`". 1440 years - impossible, 1440 days - too less. But then I realized: some in the community might even never have used a floppy. Oops, now, I outed my age also... – Scheff's Cat May 13 '17 at 09:13
  • 1
    @Scheff - you kids and your new-fangled 3.5" floppies. [old man voice] Back in my day floppies were 5.25" inches had 13 sectors per track and 35 tracks for 140k and if you weren't careful you could roll over the disk with your chair when you dropped it and ruin it. [old man voice] – Scott Thompson May 13 '17 at 09:26
  • @ScottThompson Well, but 3,5" floopies are much cooler. Beside of the increased capacity, they have this switch to change whether they are read-only or not... (Even, some of my (older) USB sticks adopted this nice idea.) – Scheff's Cat May 13 '17 at 09:30
7

A magic constant would be a numeric value that you just type into some code with no explanation about why it is there. Coming up with a good example is challenging. But let's try this:

float areaOfCircle(float radius) {
    return radius * radius * 3.14159
}

Here I've used a "magic constant" of 3.14159 without any explanation of where it comes from. It would be better style to say

const float pi = 3.14159
float areaOfCircle(float radius) {
    return radius * radius * pi;
}

Here I've given the person reading the code some idea about where the constant came from and why it was used... it didn't seem to "magically" appear out of nowhere.

Scott Thompson
  • 22,629
  • 4
  • 32
  • 34
  • Personally don't think pi is a good example as pi is actually a value not a name. – Logman May 13 '17 at 08:46
  • I don't disagree. I had a hard time coming up with a better example that wasn't equally as banal. – Scott Thompson May 13 '17 at 08:49
  • Even worse would be (and I've seen this) `355 / 113` - you can wonder about that for a great deal of time until you actually work out what it is :-) – paxdiablo May 13 '17 at 12:03
4

Magic:

int DeepThought() { return 42; }

Symbolic:

const int TheAnswerToTheUltimateQuestionOfLifeTheUniverseAndEverything = 42;
int DeepThought() { return TheAnswerToTheUltimateQuestionOfLifeTheUniverseAndEverything; }
L. F.
  • 19,445
  • 8
  • 48
  • 82
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 1
    That's the simple answer _to the ultimate question_ of Life, the Universe and Everything. – Scott Thompson May 13 '17 at 08:41
  • You would probably be better off calling the *function* `theMeaningOf...` rather than the more obscure `Q`, which pretty much has the same problem as magic numbers (no clarity on what it's supposed to be) :-) – paxdiablo May 13 '17 at 12:05
  • @paxdiablo `Q` is intentionally obscure, because it turned out, that it always was in question what the question really is ;-). Besides that _literary background_ , you're correct of course. – πάντα ῥεῖ May 13 '17 at 12:08