Swift: Common mistakes no one bothers about — Explicitness in property and function calls

IDAP
IDAP
2 min readJan 20, 2017

--

Hello there, my sweet fellow developers,

One thing I wanted to mention is, that Swift introduced implicitness in function and property calls (you can avoid calling self):

We had such a thing for ivars in ObjC, but the illness has spread everywhere as of now. There are several points against using that language feature:

  • The code without syntax highlighting is unreadable;
  • Even with the syntax highlighting, if you don’t know the code base perfectly, if there are global functions and variables and local functions and variables with same names as globals, it’s hard to derive, what is being called;
  • When used with properties, developers tend to forget, that multiple calls to properties should be cached in local variables, as they just don’t perceive the calls to properties as duplication without the explicit call.

So, my advice to everyone is: don’t use shortcuts. Explicit and deduplicate the code instead of hiding the duplication visually, without hiding it logically. And yep, burying your head in sand like an ostrich, won’t do you any good, as properties are not ivars, no matter what you think, they could be or could become computationally expensive. So, just to rehearse, be explicit:

That’s all, folks. Have a great day and stay DRY, no matter, where you are.

By Oleksa Korin, CTO @ IDAP Group

Connect with me on LinkedIn, Facebook, Twitter

Follow IDAP on LinkedIn, Facebook, Twitter

Originally posted @ IDAP Blog: Common mistakes no one bothers about — Explicitness

--

--

IDAP
IDAP
Editor for

We are a Ukrainian full-service software development company. We build native applications for the iOS, Android, and Web from square one.