Refactoring: Magic Numbers


I previously wrote this article in my blog, Think Big!.


Introduction

Another type of refactoring patterns is the Magic Number pattern.

Magic numbers are numbers with special meaning and value that usually are not obvious, such as PI. They are really nasty when you need to reference the same number many times. If the number might ever change, making the change is a nightmare. Even if you don't make a change, you have the difficulty of figuring out what is going on.

If you have a magic number into your code and you use it frequently, you can create a constant, name it after the meaning, and replace the number with it. Most languages support constants. There is no cost in performance and there is a great improvement in readability.

But, before you do this refactoring, you should always look for an alternative. Look at how the magic number is used. Often you can find a better way to use it. If the magic number is a type code, consider replace type code with a class. If the magic number is the length of an array, use anArray.length instead when you are looping through the array. Otherwise, you can go and create your constant.

Implementation


For example, the following code converts between meters and feet, and it uses a magic number that's the feet in one meter:

        public static double ConvertMetersToFeet(double meters)
        {
            return meters * 3.2808398950131233595800524934383;
        }
        public static double ConvertFeetToMeters(double feet)
        {
            return feet / 3.2808398950131233595800524934383;
        }

So, we can refactor the code to be:

        public const double MeterFeet = 3.2808398950131233595800524934383;

        public static double ConvertMetersToFeet(double meters)
        {
            return meters * MeterFeet;
        }
        public static double ConvertFeetToMeters(double feet)
        {
            return feet / MeterFeet;
        }

Up Next
    Ebook Download
    View all
    Learn
    View all