06: Variables

In programming there's a concept of a "magic number". This is a number that's some constant or setting which is simply sitting there in the code with no explanation as to what it is, or why it is that number. Programmers over the years have learned to hate magic numbers for three reasons:

  1. Magic numbers don't explain what they are or why the exist. You just see 0xDEADCAFE and think, "What? Why did someone use this number here."
  2. Magic numbers are difficult to update and fix later. Imagine you now have to be 100% positive you've replaced all 0xDEADCAFE numbers with 0x5637CC. Now you have to search and replace all over, but what if 0xDEADCAFE is used for more than one thing? Now you can't just replace them, you have to check each one.
  3. Magic numbers simply don't scale as the code and team of people grows. Cognitively it's difficult to keep all of the magic numbers in one person's head, let alone correctly share this information with others reliably. As the quantity of magic numbers increases, your ability to remember each one diminishes.

The solution programmers have for magic numbers is to simply give them name like this:

image_tag = 0xDEADCAFE

Learn JavaScript Today

Register today for the Beginner Basics course and get the new releases by just paying the difference. There's also special discounts for existing customers when new courses are released.


Still Not Sure? Check out more curriculum!