Common mistakes no one bothers about – Explicitness in Swift

Coding practices at their worst. Rant about implicitness in Swift function and property calls.

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.

(1 votes, average: 5.00 out of 5)