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
All zero-argument methods that return a value should be computed properties instead #7
Comments
I could be convinced of this this for things like |
Ah, interesting. I haven’t heard that argument before, that computed properties are reserved for things that don’t change every time you call them. Is that officially documented somewhere? |
I'm not sure if that's documented explicitly, but my understanding is that this is a motivation for differentiating between "properties" and "methods". If I were to guess, this is a holdover from the days of C, where "properties" were really members of structs (like ivars in Objective-C), and so looking them up is an I would also say that, philosophically, |
The only real hesitation I would have about making this change is that, since the rest of Chronology's implementation is still undecided, I'm not sure I'd ever need variations on a |
I agree that There's nothing stopping you from adding a |
The Swift API Design Guidelines encourage developers to document when computed properties involve significant computation. Personally, I think it comes down to the usage mindset you want. If you are “doing” something, it should be a method. If you are “using” or “reading” something, it should be a computed property.
|
I've made a decision on this: If a value has a method such that repeated invocations would produce the same value every time, then it will be a property. If repeated invocations will produce potentially different values, then it will be a method. For example:
|
What would you think about changing the method signatures such that as a general rule, all zero-argument methods that return a value were replaced with computed properties? For example,
func now() -> Instant
would be replaced byvar now: Instant
. I think this would make code that called those methods significantly more concise and easier to read.The text was updated successfully, but these errors were encountered: