What is A Magic Number (Computer Programming)?

What is A Magic Number (Computer Programming)?

In computer programming, a magic number is basically a hard-coded value that might change at a later stage. Since it has the chances of changing at a later stage, it can be said that the number is hard to update. A magic number should be avoided as much as possible in programming. We’ll go through a few reasons for which it should be avoided in programming.

 

What is the practice of using magic numbers in programming?

The use of magic numbers in programming refers to the practice of coding by using particular numbers directly in the source code. This practice isn’t recommended in programming and is considered to be a breakage of one of the oldest rules of programming. The use of magic numbers makes the program difficult to read, comprehend and debug.

The program might be correct and is in a perfect running condition as per requirement, but still, this practice is not recommended.

You can see the use of a magic number in the program shown below-

public class Foo {

    public void setPassword(String password) {

         // a magic number is used which is a poor practice

         if (password.length() > 7) {

              throw new InvalidArgumentException("password");

         }

    }

Prog. 1 (by using a magic number)

The magic number used in the program is “7”. The maximum password size is directly defined to be as 7 in the loop. That’s a poor practice as per the laws of programming even though the program is right.

Why is it a poor practice?  

The use of magic numbers in programming reduces the readability of source code. The code also becomes pretty convenient to maintain. Refer to the program that we’ve shown above. The maximum password size used is in the form of a magic number there. What happens if the maximum password size changes to say, 9? You’ll have to search for it in the program and make necessary changes.

Note- The program shown above is a simple one and has only a single place where the password size is shown to be in use. In the case of bigger and more complicated programs, this is a huge issue. There might be a program where “maximum password size” is used in repeated places. In such cases, the programmer will have to make repeated changes in those exact places where there’s the use of a magic number. That becomes a huge issue. If the programmer misses any one place by mistake, the program will be deemed faulty, and s/he will have to debug the program again from the start. It’s time-consuming as well. Therefore, it can be easily said that the use of magic numbers in computer programming isn’t a decent practice.

Another issue with magic numbers is that a programmer who’s reading the code for the first time might wonder from where that number came from. The number according to the program depicted above is 7, and it might confuse anyone who’s reading the code for the 1st time.

What is the proper practice?  

The proper practice is to use a variable so that it becomes easier to alter the value of a number. Like we said before, altering the value of a magic number makes the code prone to errors. Therefore, it’s better to change the value at one course and let the code take its toll. Example-

public class Foo {

    public static final int MAX_PASSWORD_SIZE = 7; ………………………(i)

 

    public void setPassword(String password) {

         if (password.length() > MAX_PASSWORD_SIZE) {

              throw new InvalidArgumentException("password");

         }

    }

Prog. 2 (without using a magic number)

The program in the example above (prog.2) is the same program as shown in our initial example (prog. 1) up above with a slight difference. There’s no magic number in prog. 2. The benefit of this is that if there’s a change in the maximum password size, the programmer will just have to change the value in the variable “MAX_PASSWORD_SIZE” (refer to (i) in prog. 2).The rest of the program will then work as per requirement. The alteration’s done only in one spot thereby, saving the programmer a lot of time and trouble. It’s a good practice indeed. Any programmer who’s going through the code for the first time will also have no confusion with the number because it’s already defined previously with a specific meaning as per requirement. Meaning, the new programmer will immediately come to know that “7 is the maximum password size from the variable alone.

To sum up, we will repeat the same thing again. Try to avoid magic numbers as much as possible in your code. Develop that practice from today itself so that you stick to it in future. The practice will benefit you a lot if you pick up computer science as a choice of your career. Professional tutors can definitely look into it on your behalf and help you inculcate this practice with fair ease. With that, we’ll sign off for now. Hope you had a good read.

Sudipto Das

Sudipto writes technical and educational content periodically for wizert.com and backs it up with extensive research and relevant examples. He's an avid reader and a tech enthusiast at the same time with a little bit of “Arsenal Football Club” thrown in as well. He's got a B.Tech in Electronics and Instrumentation.
Follow him on twitter @SudiptoDas1993

Leave a Comment

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