Write tests

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.

How about:

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!

Maturity of the Developer lies in acknowledging and respecting the need that there needs to be a good Tester who would challenge the code to get the best of the Development. Good article.

To view or add a comment, sign in

More articles by Dragos Ionel

  • AIGeeks 2019 in review, plans for 2020

    As we’re wrapping 2019 up, we have high hopes for how AI will improve our lives in 2020. This year, we organized and…

    2 Comments
  • How can AIGeeks help you?

    As you know AIGeeks is a Non Profit trying to improve our world around us using AI. Other than organizing the meetup…

  • Be cautious with the ternary operator

    The operators in a programming language, in general, and in Swift, in particular, are of three types: Unary - that…

    2 Comments
  • Use long names when writting your code

    Unfortunately, we have been used to the fact that a name has nothing to do with the object it names. John, Mary, and…

  • How to name a function - Coding in Style - episode 11

    Welcome to Coding in style, a series of short videos about how to improve our code. This is Dragos and you are watching…

  • Reducing the cyclomatic complexity - Coding in style - Episode 9

    Welcome to Coding in style, a series of short videos about how to improve our coding. This is Dragos and you are…

  • To comment or not to comment

    Welcome to Coding in Style, a series of short videos about how we can improve our code. This is the episode 6 where we…

    2 Comments
  • Coding in style in 60 seconds - Comments

    Welcome to Coding in Style in 60 seconds. This is Dragos and you are watching a series of short videos about how we can…

  • Get smart by being stupid

    Do you know why programming is the best job ever? From the thousands and thousands of possible jobs out there…

  • Coding in style in 60 seconds - capitalization

    Welcome to “Coding in Style in 60 seconds”, a collection of short videos about how we can improve our code in simple…

Others also viewed

Explore content categories