Functions vs Computed property — What to use?

Many times while development we caught in a situation on what to use?

Aaina jain
Swift India
3 min readNov 22, 2018

--

Credit: unsplash.com

If any call is considered to be expensive, can throw an error, or returns different results when invoked multiple times — a function is preferred. If a call is cheap, does not throw errors, returns the same result or caches the result of its first invocation — a computed property will probably suit your needs.

— — — — — — — — — — — — — — OR — — — — — — — — — — — — — —

If your code performs an action and returns, for example, a description of the outcome of that action, then you should prefer function. If your code calculates a property but from the user’s point of view this could have been a stored property, or maybe a stored property that requires updating some cached value first, then you should prefer calculated property.

For instance, We have CookieFactory class to bake some cookies. We would need it to look at the ingredients and decide what kind of cookies to make. It would need to check how many ingredients were available to it in order to determine a batch size. It would then need to portion and create the cookies. Sounds complex, so a function would be the way to go here.

Follow the Uniform Access Principle

All services offered by a module should be available through a uniform notation, which does not betray whether they are implemented through storage or through computation.

— Bertrand Meyer

A property expresses an inherent quality of an instance, while a method performs an action.

  • Methods have parameters; properties don’t. Prefer methods for any call with side effects. If a method does something (for example, it loads, parses, toggles, or prints) or has a verb name, it should not be a property.
  • Prefer properties for simple values that you can get and/or set.
  • Properties should express a semantic intrinsic quality of a type instance.

Examples of bad computed properties:

- Random values

- today’s date

- a value from another object or singleton

- formatting a date

- fetching from server

A big difference: What happens if you call the function or the calculated property twice? For a calculated property I expect that x = property; y = property has exactly the same behavior as x = property; y = x except it might run a tiny bit slower. For functions, I wouldn’t be surprised if the behavior was different.

It also depends on the complexity of calculation vs. usage frequency.

  • If it’s O(1) / *, then use computed property.
  • If it’s O(N)+ / rare-use, then use function.
  • If it’s O(N)+ / frequent-use, think whether in the future you might decide to use caching or other “smart” techniques to compensate for the complexity, if “yes” then use property, if “no--no, it’s just heavy” then use function.

In general, use computed property only in following cases:

  • doesn’t throw any Exceptions
  • has a O(1) complexity
  • is cheap to calculate (or caсhed on the first run)
  • returns the same result over invocations

If any call is considered to be expensive, can throw an error, or returns different results when invoked multiple times — a function is preferred.

If a call is cheap, doesn’t throw errors, returns the same result or caches the result of its first invocation — a computed property will probably suit your needs.

Thanks for reading article. If you have any doubt please add in below comment section.

You can catch me at:

Linkedin: Aaina Jain

Twitter: __aainajain

--

--