Can you tell me what this code is doing?
function convert( amount ) {
return amount * 100;
}
You may say it is multiplying a number by 100. But where is that 100 coming from?
Well, that is the conversion from Naira to Kobo. But nowhere in this code do we know that this is happening.
Some people will try to add a comment above the function:
**// Convert Naira to Kobo**
function convert( amount ) {
return amount * 100;
}
Or they will likely change the function name or add variable names to make it more clear.
// Convert Naira to Kobo
function **convertToKobo**( **naira** ) {
return **naira** * 100;
}
But there is still some ambiguity around this 100. The best way is to make it explicit because right now there is still what I call a Magic Money AKA Magic Number.
A Magic Number is a number in a code that has no clear and defined meaning. And it doesn't convey any intention. This makes code difficult for others to understand and maintain.
Instead of 100, we should convert it to a variable and use that instead. By doing this we are going to have a code that looks like this:
**const NAIRA_TO_KOBO = 100;**
function convertToKobo( naira ) {
return naira * **NAIRA_TO_KOBO**;
}
There we have a variable which is specifically named NAIRA_TO_KOBO. And we have it all in uppercase because that is the standard for all global variables that don't change at all. And we are setting it to the value of 100.
So we can clearly see and understand exactly what our code is doing, instead of seeing the magic number 100.
It is important you avoid ambiguity at all costs when building a fintech application. Every number in a fintech application is money. A clear understanding of what each number does will help you avoid miscalculation.