Replace Magic Number with Symbolic Constant



Problem

Your code uses a number or a string that has a certain meaning to it.



Why Refactor ?

The use of Magic Numbers and string literals makes it hard to understand the code's logic, and modifying them requires changes in multiple places. Refactoring the code would make it easier to read and understand, reduce the complexity of the code, and improve its maintainability. This “anti-pattern” makes it harder to understand the program and refactor the code



Benefits

1: The symbolic constant can serve as live documentation of the meaning of its value.


2. It’s much easier to change the value of a constant than to search for this number throughout the entire codebase, without the risk of accidentally changing the same number used elsewhere for a different purpose


3. By using Symbolic Constants, the code can be reused in other parts of the application.



How to Refactor?

1: Declare a constant and assign the value of the magic number to it.


Criteria

1)All the test should be passing
2)No Magic Number/String should be in the code.

Loading...

Open browser consoleTests

Code Smells

Replace Magic Number