ELI5: Explain Like I'm 5

Magic number (programming)

In programming, a magic number is a number that is used in a piece of code but it doesn't have any meaning on its own. It's like having a secret code that only makes sense to the person who wrote the code.

For example, imagine you're writing a program for a game where the player has to collect 10 coins in order to win. Instead of writing "if (coins == 10)" (which makes sense to anyone reading the code), you might write "if (coins == 0x0A)" (which looks like gibberish to most people).

The problem with using magic numbers is that if you want to change the number (let's say you want to make it so the player has to collect 20 coins instead), you have to search through the code to find every instance where the number is used and change it manually.

To avoid this problem, it's better to define a constant variable (like "COINS_TO_WIN") and use that variable instead of a magic number. That way, if you need to change the number, you only have to update the value of the variable in one place, instead of all throughout the code.

In short, using magic numbers in programming is like having a secret code that only makes sense to the person who wrote the code. It's better to define a constant variable instead so that anyone reading the code knows what it means and it's easier to make changes in the future.