TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Checkpoint 4

Forums > 100 Days of SwiftUI

I was experimenting with functions trying to understand them better and I've created function like this for checkpoint 4:

import UIKit

func SquareRootThisNumber (_ number: Int) {

switch number {
case 1...10_000:

    for i in 1...100 {
        if number == i * i {
            print("The squared \(number) is \(i)")
            return
        }
    }
    print("\(number) is not a perfect square between 1 and 10_000")

default:
    print("You are out of the loop")
}

}

SquareRootThisNumber(25)

I was only experimenting I know the two better solutions for this problem but if I would write this function myself I would write it probably like this...

This code looks littlebit cleaner then throwing functions and creating do try catch block, but are there reasons why should I not use this code I have few in my mind but I would like to hear yours opinion.

   

Nice! The code that you made yourself is always a step forward in your learning curve, especially if it works and shows the result. Just a small note, you might think about efficiency of code as well. Also in Swift it is a convention to give a name to the functions with no capital letter.

In your code if you try to find square root of 10_000, loop will run so many times to find the result... mmmm maybe there is a more better solution to this... but in any case your code works and you find the solution yourself, which is really wonderful and this is how we all learn something new. So keep coding!!!

func squareRootThisNumber (_ number: Int) {
    switch number {
    case 1...10_000:

        for i in 1...100 {
            print(i) // see the print console and how many times code runs to get to the number
            if number == i * i {
                print("The squared \(number) is \(i)")
                return
            }
        }
        print("\(number) is not a perfect square between 1 and 10_000")

    default:
        print("You are out of the loop")
    }
}

squareRootThisNumber(10000)

1      

are there reasons why should I not use this code

The biggest one in my mind (ignoring that the whole point of Checkpoint 4 is to learn about throwing functions) is that this function really doesn't do anything. Sure, it prints out the square root of the given number, but so what?

It's far more useful if it returns the square root and then the code calling the function can actually do something with the result. And for that, a throwing function is your best bet. You could return some magic value (e.g., returning 0 indicates no root found and -1 indicates an out of bounds condition), but that's ugly and you would have to document what those values mean and then remember them at the call site. Throwing an error is a much better solution.

And just because a function throws, you don't necessarily have to use a do ... catch block to handle it. You can use try? to turn your result into an Optional (say, as part of an if let or guard let test), or try! to force unwrap your result (which I wouldn't recommend in this case). Or, you could wrap the result in a Result data type, if you were so inclined.

1      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.