thoughtbot/Tropos

Temperature.celsiusValue should be a computed property

Opened this issue · 1 comments

The problem with a lazy var is you can overwrite it:

lazy var celsiusValue: Int = {
return Int(round(Float(self.fahrenheitValue - 32) * 5.0 / 9.0))
}()

let temp = Temperature(fahrenheitValue: 20)
temp.celsiusValue = 20

The math isn't particularly expensive, so using a computed variable is probably good enough. If we really want to lazily load the celsius value, we can do so with a private lazy var, which the computed variable calls/returns.

Interesting find. Computed variable makes sense to me.