How do you implement magic numbers and why are they a problem?

I have been trying to figure out what is meant by a magic number and how they should be implemented, but I can’t get my head wrapped around it. I understand that magic numbers are values that are implemented by a variable, but not how this should be done. I also understand that they are numbers in your code that are above 2. Why is it necessary top worry about them?

Thanks for the help!

1 Like

A magic number is a constant number that is you could use through out the program. Because some numbers can be used for different meanings in the same game like a card game where the number 4 can mean number of suits in a deck of cards, max number of players, max time in minutes to play turn. If you wanted to change your program so that you have 6 players you would need to look at all those 4’s used through out your program to see which ones represent the number of max players and which one’s don’t. If you use a constexpr you change it once and it will change the correct places only.

3 Likes

Thanks, that makes sense!

That made it a lot more understandable for me, too. Thanks a bunch, Landan.

Privacy & Terms