Introduction to Swift

Apr 24 2024 · Swift 5.10, iOS 17, Xcode 15

Lesson 04: Problem Solving

Crashing Demo

Episode complete

Play next episode

Next
Transcript

Writing 100 percent bug-free code is a myth. There’s no shame in having some bugs in your code, but it’s important to have them under control and not have too many of them that give your users a negative experience.

In this demo, you’ll give unexpected input to the code you completed in the previous demo. You’ll intentionally crash the code and make it produce unexpected results, and then you’ll improve the code to fix those issues.

Open Xcode on your Mac and open the playground file named Demo3 in the Starter Folder.

The two examples you’re already using from before are from zero to 10 and zero to one hundred.

Start by trying inputs with negative values. Add these two examples:

calculateSum(minValue: -10, maxValue: 10)
calculateSum(minValue: 10, maxValue: -10)

Run the playground and observe the results in the window’s right pane.

Luckily, those two give the correct result. The new implementation doesn’t have any loops and doesn’t require that minValue is actually lower than maxValue. But the old implementation would have had a problem.

Now, try another example, which is a very straightforward one:

calculateSum(minValue: 1, maxValue: 10)

Run the playground.

This results in 50, while the inputs zero and 10 give the value 55. Why did the zero make a difference?

Tracing your implementation:

10 + 0 = 10 10 / 2 = 5 5 * 11 = 55

On the other hand: 10 + 1 = 11 11 / 2 = 5.5

And because you are using Int, the 5.5 gets rounded down to 5. 5 * 10 = 50

As you can see, the reason for the wrong result is a rounding problem. Before you start fixing it, try another example:

calculateSum(minValue: 0, maxValue: Int.max)

Now build and run and this will crash immediately for obvious reasons. Int.max is the largest number Int can store; any operation that increases that number causes an overflow crash.

Both of those issues are because of the limitations of Int. The first is because you need fractions, and the other is because the numbers are larger than it can hold.

A good way to fix both is to change the type of variables you use in your code to Double.

Start by changing the type of sum to Double:

var sum: Double = 0

And change the return type of the function to Double:

func calculateSum(minValue: Int, maxValue: Int) -> Double {

Once you do that, Xcode complains that it can’t convert from Int to Double. The simplest way to avoid too much more code is to make a version of the input parameters as Double.

Add these lines right after the declaration of sum:

let doubleVal1 = Double(minValue)
let doubleVal2 = Double(maxValue)

Then, update your original code to use the new variables:

sum = (doubleVal1 + doubleVal2) / 2.0
sum *= doubleVal2 - doubleVal1 + 1.0

Run the playground after your updates.

The crash is gone, and the sum from 1 to 10 results in the correct value, 55, instead of 50.

See forum comments
Cinema mode Download course materials from Github
Previous: Guarding Against Bad Input Next: Conclusion