## Wednesday, February 1, 2017

### Write tests

Welcome to another post about how to improve our code.

Today we are trying to prove that the quality of the code improves when we write tests.

We, as developers, hate to write tests. Why? Simple. It brings extra work without no real immediate advantage. Also, because we are the ones writing the code, we expect it to be perfect. So, why test a perfect code?

Our managers hate the activity as well. Why? Because it costs them extra money and time without adding new features for the customers.

Then why do it? From the same reason, you test any equipment you buy from any store. You expect it to work, right? They probably test it before bringing it to the shop. And yet you thoroughly test it because you know, from experience, that some do fail.

When we write code we consider only some specific scenarios that we call sometimes happy paths. Then we implement the code according to only those.

It does not matter how you test your code. Ideally, you should use a framework (like XCTest), but you might as well write your own framework.

Let's take an example.

Assuming you work at a bank and you need a function to calculate the maximum interest rate a client can pay. As input, you have the mortgage he needs to pay per year and his available funds.

So assuming a client needs to pay \$11 000 per year and his available funds, after deducting all the expenses from his salary, are \$10 000, it is obvious that the maximum rate he can pay is 10%.

To calculate it we use the following formula

Rate = ((available funds - mortgage per year) / mortgage per year) * 100

so (11 000 - 10 000) / 10 000 * 100 = 10

let's quickly write the function to calculate that

let availableFunds = 11000
let mortgage = 10000
func computeMaximumInterestRate(availableFunds: Double, mortgage: Double) -> Double {
return (availableFunds - mortgage) / mortgage * 100
}

Everything looks great. Let’s use this method:

computeMaximumInterestRate(availableFunds: 11000, mortgage: 10000)

All is good, it returns 10 meaning 10% as expected.

Now let’s take the tester hat and go crazy.

computeMaximumInterestRate(availableFunds: 10000, mortgage: 11000)

This returns 0. It means the customer can pay 0%. It might be okay, but in this case, the function should show that the user cannot really take this mortgage.

computeMaximumInterestRate(availableFunds: 9000, mortgage: -10000)

This returns -10. Is this a valid result? Not really. This means that if the client takes the mortgage, the bank will actually pay him some money.

This is similar to the famous bug Amazon had when they launched. If a user entered a negative number of items, the website will actually send some money to his credit card.

And finally:

computeMaximumInterestRate(availableFunds: 0, mortgage: 0)

This will crash the app.

We will not go into details about how to fix each of this problems, but we know that we need to fix them. Our function should differentiate between a valid result (like 10%) and an invalid one (like -10%) and maybe not return it at all, just raise an exception.

That’s it for now. See you next time!