Operator overload ? Easy boy, easy

August 12, 2014

A lot of "new" things in Swift aren't that new: generics and operator overloading have been avalaible in C++ for centuries. But for iOS/Mac-only (aka Objective-C) developers, this is new. And with the discovery of a new hammer comes the transformation of everything into nails.

Generics are OK: they may make the code a little bit less readable at first but we are helping the compiler helps us.

My main concern is about operator overloading. It is of a concern because of the last NSHipster article which is on operators. Even if this article gives guideline at the end, there is one argument in my opinion that makes operator overloading a bad idea. (If you need more arguments to convince your team not to use them, you can check this article from Brent Simmons)

Of course, those things are sexy and they look like they can make us save some time. After all, with a single symbol, you can do a lot of things. But you lose expressiveness.

You do understand what an overloaded "+" does on this data model, but what about the next programmer that will have to update the code 3 months later. And what will happen when people start to add code in this operator not so logical to add in a "+" operator ?

Big troubles. Spare them. Use operator overloading only in project where you will always and forever be the only programmer ;)