UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Checkpoint 6 review

Forums > 100 Days of SwiftUI

Hey friends !

I've completed checkpoint 6 and I think I have a different take than a lot of people I checked here.

The thing works but maybe it can be improved. Not sure my maxGear and minGear variables are the best way to set the gear back to something reasonable if the user types a super hight or low number.

What do you think about it?

import Cocoa

struct Car {
    let model: String
    let seats: Int
    private(set) var currentGear = 0
    private(set) var maxGear = 6
    private(set) var minGear = 0

    mutating func gearUp(increment: Int) { // I want the ger up increment to be free didn't find how to constrain it to a range tho.
        currentGear += increment
        if currentGear > 6 {
            currentGear = maxGear
            print("You can't go to gear \(currentGear + increment) it'll break the car! You are now in gear \(maxGear)")
            // I set the gear to the max and print this error message if the gear too high

        } else {
            print("Your \(model) is in gear \(currentGear)")
        }
    }

    mutating func gearDown(increment: Int) {
        currentGear -= increment
        if currentGear < 1  {
            print("You can't go to gear \(currentGear - increment) it'll break the car! You are now in gear \(minGear)")
            currentGear = minGear
            } else {
                print("Your \(model) is in gear \(currentGear)")
            }
        }
    }

var laBenz = Car(model: "Mercedes Class C", seats: 5 )
laBenz.gearUp(increment: 17)
laBenz.gearDown(increment: 12)
laBenz.gearUp(increment: 5)

2      

@wonderpig wonders about Checkpoint 6!

I've completed checkpoint 6 [...snip...]
What do you think about it?

This is a great solution. Many have submitted their Checkpoint 6 for critique. It's interesting to see how many ways this can be interpreted and solved.

You're on a journey. So bookmark this code and come and revisit it at day 50, 60, 70, etc. When you revisit this code, write down what YOU think of your Checkpoint 6 solution! I bet future you will have some great comments for past you!

For example, somewhere along the journey you'll hear about Magic Numbers. These are numbers you hard code into a solution just to get the code working, and make the compiler stop complaining. However, you'll come to recognize when you use magic numbers and you'll factor them out of your solutions.

But you may not even realize you're doing it in Checkpoint 6.

    private(set) var maxGear = 6   // <-- here you set the maximum allowable gear.
    private(set) var minGear = 0

mutating func gearUp(increment: Int) {
    currentGear += increment
    if currentGear > 6 {  // <-- here you compare the currentGear to a magic number. This may not match your maxGear value!
        currentGear = maxGear
        print("You can't go to gear \(currentGear + increment) it'll break the car! You are now in gear \(maxGear)")
}

Also, as you progress, you may find yourself refactoring code. A lot.

// Past you wrote
if currentGear > 6 {  
        currentGear = maxGear
}
// Future you might write
currentGear > maxGear ? maxGear : currentGear     // <-- Future you uses ternary operator instead

Nice job!

Keep Coding

2      

Thanks man. That's the kind of feedback I was looking for 😁

It's true that I should have refferenced the "maxGear" constant instead of hard coding it's value in the function. My bad 😅

2      

Hacking with Swift is sponsored by Superwall

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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.