Magic Numbers – Should You Use Them in Computer Programming?

Jun 10, 2021

Have you ever heard the term magic number? Well, if you have read any books or seen videos relating to computer programming, you must be familiar with magic numbers. What are actually the magic numbers? Are the numbers really magical? Unfortunately, magic or magicians have nothing to do with computer programming’s magic numbers.

Magic numbers refer to the direct use of numbers in a computer code without stating the meaning of the number. They are hard-coded values that can be easily changed any time later. They are generally used in place of named consonants. However, the use of magic numbers in coding is usually known as poor coding practice. So, it is always advisable to avoid using them in computer programming as much as possible.

If you are wondering as to why not to use the magic numbers in computer programming, take a look at the reasons below.

Readability

Readability is one of the most prominent reasons why you should not use magic numbers in computer programming. The use of magic numbers often creates a lot of confusion and makes the simple codes hard to read and understand. When you as a programmer use magic numbers, you might understand the meaning of the number.

But when an outsider views the same programme, he or she might get confused and start worrying about the meaning of the number. However, the elimination of the magic codes helps the computer programming code to be self-documenting and improves readability.

In order to understand this better, let’s take an example.

public class Foo {

    public void setPassword(String password) {

         // a magic number [9] is used, which is a poor practice

         if (password.length() > 7) {

              throw new InvalidArgumentException("password");

         }

    }
[Program 1]

public class Foo {

    public static final int MAXIMUM_PASSWORD_SIZE = 9; 

    public void setPassword(String password) {

         if (password.length() > MAXIMUM_PASSWORD_SIZE) {

              throw new InvalidArgumentException("password");

         }

    }
[Program 2]

In program 1, you can see that a magic number 9 has been used. It actually indicates that the maximum length of a password can be 9 only. It cannot go beyond it. While the programmer can easily understand the meaning, it can be quite difficult for an outsider to make sense of the numerical digit 9.

Therefore, in order to ensure better readability, you can consider first storing the number in a meaningful variable and later use the number in the code. For instance, in Program 2, when the same digit 9 is stored in the variable “Maximum Password Size” and then used in the code, it becomes easy for outsiders to easily understand the meaning of the number.

Refactorability

Refactorability is another valid reason why it is advised not to use magic numbers in computer programming. Using the magic numbers in computer programming makes it quite challenging to maintain the code. However, eliminating the magic numbers can enable easy refactoring and ensures that the code is less prone to potential bugs. In order to understand this better, let’s take the example of program 1 and program 2 again.

In the code, the numeric digit 9 is used to indicate that the maximum length of the password is 9. Suppose, for a particular reason; you need to change that number to 7. What if the number is mentioned multiple times in the code? You have to change the number in all the places throughout the code. Moreover, in case you miss out on a single place, the programming may not get executed properly and will show errors. This can take up a lot of time and can be frustrating at times.

In order to avoid all such problems, it is always better to use the approach as program 2, where there is no magic number. The number is stored in the meaningful variable and used in a convenient way. In this case, if you want to make any changes, all you need to do is change the number only once in the variable “MAX_PASSWORD_SIZE.” Only a single change is required, and the program will run in the desired way without any problem. This can save you a great deal of time and enable you to avoid all trouble much easily.

Easy Detection of Typos

When you use magic numbers in computer programming, and it is used multiple times in the code, there are chances of typing errors. In place of 22, you may mistakenly enter 12 at one place. Often, such typos go undetected and result in ineffective programming. However, storing the number in a variable can ensure detecting such typing errors much easily.

In addition, avoiding the use of magic numbers in computer programming also helps in reducing duplicate use of numbers as well as strings in a code. It is especially vital when a value is quite long and complicated.

Winding Up

Using magical numbers in computer programming may seem to be very straightforward and obvious for you. However, it can create a lot of trouble for you at the time of debugging. If you want to be productive and avoid any kind of confusion in your code, make sure to avoid the use of magical numbers.

Magical numbers are simply the numbers used in the codes that do not have any clear meaning for the readers or programmers. When writing a code, try replacing the magic numbers with named constants. It will help in effectively increasing the readability as well as maintainability of the code. Eliminating the magic numbers also helps in enhancing the cleanliness of the computer programme and makes it appealing to the readers.

While it may take some time for you to develop the good practice of computer programming, make sure to keep trying. With regular practice, you will easily be able to use the named constants and avoid using the magic numbers. Moreover, if you are planning to make a successful career in the field of IT, learning to avoid magic numbers is a must. This will help you advance and achieve higher growth and success in your future career.

 

Image Credit : freepik.com

Sudipto Das

Sudipto writes educational content periodically for LearnPick USA 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 more than 5 years of experience in digital marketing, SEO and graphic designing.

Related Posts