Ensuring termination

We are not quite done yet because our function might loop indefinitely. Imagine that you always spend more than what you earn. You will never be able to save enough to retire, even if you live a million years! Here is a unit test highlighting this:

"RetCalc.nbOfMonthsSaving" should {
"calculate how long I need to save before I can retire" in {...}

"not crash if the resulting nbOfMonths is very high" in {...}

"not loop forever if I enter bad parameters" in {
val actual = RetCalc.nbOfMonthsSavingV2(
interestRate = 0.04 / 12, nbOfMonthsInRetirement = 40 * 12,
netIncome = 1000, currentExpenses = 2000, initialCapital = 10000)
actual should === (Int.MaxValue)
}
}

We decided to use a special value Int.MaxValue to indicate that nbOfMonths is infinite. This is not very pretty, but we will see in the next chapter how we can model this better with Option or Either. This is good enough for now.

To make the test pass, we just need to add an if statement:

def nbOfMonthsSaving(interestRate: Double, nbOfMonthsInRetirement: Int,
netIncome: Int, currentExpenses: Int, initialCapital: Double): Int = {
@tailrec
def loop(nbOfMonthsSaving: Int): Int = {...}

if (netIncome > currentExpenses)
loop(0)
else
Int.MaxValue
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset