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

SOLVED: Feedback - Checkpoint 6 (Multiple POVs ok)

Forums > Swift

Link to Checkpoint 6

Dear people,

I request your constructive feedback for my solution below, for Checkpoint 6. Please feel free to include multiple POVs (for example, how an equally experience (or more) developer might differe from you in this solution).

I understand this must be quite simple for you; but at this point, I don't know what I don't know. But I'm sure this could be made to be more efficient, use lesser energy, and possibly even read (or work) better.

So here's the code; please tell me if it is good. — and also if I got it right / did what Paul intended us to do with Checkpoint 6 :)

// Checkpoint 6

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

    mutating func gearShift(_ gear: Int) {
        if gear < 0 || gear > 5 {
            print("Stop! That really grinds my \(model)'s gears!")
        } else if gear == 0 {
            print("Why are we in Neutral? Are we ready to go?")
        } else {
            currentGear = gear
            print("Driving my \(model) in gear \(gear).")
        }
    }

    mutating func gearUp() {
        if currentGear < 5 {
            currentGear += 1
        } else {
            "Stop! You're breaking the car!"
        }
    }

    mutating func gearDown() {
        if currentGear > 0 {
            currentGear -= 1
        } else {
            "Stop! You're breaking the car!"
        }
    }

    init(model: String, seats: Int) {
        self.model = model
        self.seats = seats
    }
}

var firstCar = Car(model: "Tesla", seats: 2)
firstCar.model
firstCar.seats

firstCar.gearShift(2)
firstCar.currentGear

firstCar.gearUp()
firstCar.currentGear

firstCar.gearDown()
firstCar.gearDown()
firstCar.currentGear

firstCar.gearShift(0)

var stolenCar = Car(model: "Pinto", seats: 4)
stolenCar.gearShift(6)
stolenCar.currentGear

stolenCar.gearUp()
stolenCar.currentGear

stolenCar.gearUp()
stolenCar.gearUp()
stolenCar.gearUp()
stolenCar.gearUp()
stolenCar.currentGear

Looking forward to your replies!

Thank you very much :)

PS: My Swift knowledge is limited to this, Day 11.

2      

@aradroid starts the journey in first gear!

I request your constructive feedback for my solution for Checkpoint 6

Code works? CHECK!
Mutating functions? CHECK!
Several test cases? CHECK!
Cheeky humour? CHECK!

I'd say that for an early project you completed this well.

Save this code and revisit it on Day 50, or even Day 70. You'll find several ways you can improve it. Come back and add your own updates a few weeks from now. Leave a message for your PastSelf and share what your FutureSelf has learned.

In the meantime, consider this one suggestion. Can you switch gears from second gear directly to fifth gear? 2--> 5 ?

Is this a legitimate Use Case or would your design team consider this an Error Condition?

How would you handle this requirement from a paying customer?

Keep Coding!

Be sure to code every day.

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.