Computed var vs. non-void function with no arguments
Opened this issue · 2 comments
paulrolfe commented
Which is better?
var heightForHeader: Float {
return selectedSegment == .Upcoming ? 25 : 0
}
v.s.
func heightForHeader() -> Float {
return selectedSegment == .Upcoming ? 25 : 0
}
giuggioRapido commented
I vote for computed properties!
alexpersian commented
My vote goes to computed properties so long as they are simple. I came across this blurb from the Kotlin documentation that relates to this, and I think it applies well to Swift too.
Prefer a property over a function when the underlying algorithm:
- does not throw
- has a O(1) complexity
- is cheap to calculate (or caсhed on the first run)
- returns the same result over invocations