Don’t use magic numbers

Quick note to start a new category on this blog: Good practices/Training

Everyone who doesn’t know what a “Magic number” in coding is, please quickly read this: http://stackoverflow.com/questions/47882/what-is-a-magic-number-and-why-is-it-bad

In some rare cases it might be allowed to use magic numbers (like 0/1 or in a circles calculation you may use 90/180/270/360 if it is clear why it’s being used). But in all other cases you have to explain such numbers – and the best way to do that is naming a variable or constant.

Example:

double angle = currentAngle + (angleSize / 3);

Why is this divided by 3?

angle = currentAngle + (angleSize / 3.5);

Why is this divided by 3.5?

The very least you can do here is add a comment with a short explanation.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.