Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Computed var vs. non-void function with no arguments #22

Open
paulrolfe opened this issue Jul 29, 2016 · 2 comments
Open

Computed var vs. non-void function with no arguments #22

paulrolfe opened this issue Jul 29, 2016 · 2 comments

Comments

@paulrolfe
Copy link

Which is better?

var heightForHeader: Float {
    return selectedSegment == .Upcoming ? 25 : 0
}

v.s. 

func heightForHeader() -> Float {
    return selectedSegment == .Upcoming ? 25 : 0
}
@giuggioRapido
Copy link

I vote for computed properties!

@alexpersian
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants