General discussion

Locked

My Operator Overloading Manifesto

By jslarochelle ·
With the rise of languages that support operator overloading like Ruby, Groovy, Scala and C# one is justified to wonder if libraries will become loaded with unreadable <q>APL like</q> code.
I have already seen signs of the potential chaos that could come from the abuse of operator overloading. In the Scala actor library tutorial for example I have seen the following:<br>
<pre>producer ! Next</pre><br>
I am totally unable to guess the meaning of this.<br>
This example is from a Ruby library:<br>
<pre>(aobject / 'a string')</pre><br>
In this case because I watched the full presentation I know that the slash is actually an alias for a search method.
The speaker in the presentation was calling this cool. I call it stupid. It is the archetypal example of a bad use of operator overloading. The presenter himself said he was puzzled and could not understand what the code was doing at first. Definitely not cool.
Since I'm looking at switching to Scala as a main language I thought I needed to think about what kind of rule I would put into our code convention document under the section
<q>Operator Overloading</q>. I thought I would share this with others to get inputs and hopefully constructive comments.<br>
For me, the best applications of operator overloading makes the code easier to understand.
Here are some example of this:<br>
<dl>
<dt><i>Mathematical operations</i> (+, -, *, /)</dt>
<dd>This is the best application for operator overloading and as long as you don't
start doing stupid things like using operators in a way that conflicts with established
conventions you should be OK.</dd>
<dt><i>Logical operations</i> (&. |, ||, ..)</dt>
<dd>This should be used on boolean values. One acceptable extension of this is to
use them in cases where we have implicit conversion to boolean. They don't really look like their textbook equivalent but they have been in use long enough in enough languages to be used safely.</dd>
<dt><i>Comparisons</i> (>, <, ..)</dt>
<dd>Use those on any set of ordered elements. The meaning should be obvious. For example: <pre>myWeight > aWeight</pre> Beware of things like: <pre>myDog > otherDog</pre> where we don't exactly know how things are being compared.</dd>
<dt><i>Operations that are metaphorically related to a mathematical or logical equivalent</i></dt>
<dd>Using the + when you want to add a string to another for example.</dd>
<dt><i>Operators used as part of method names</i></dt>
<dd>Of course this is not operator overloading but since it is another use of operators that can lead to abuse and unreadable code I include it here. In those language that allow this, using ? as a suffix for queries for example is OK. The meaning is clear and makes the query stand out. In this category I think that % and $ could be used if allowed by the language. The only other case that I can think of is the exclamation point as a warning that a method call might have side effects. This last one is not as obvious and is at the limit of what is acceptable for me.</dd>
<dt><i>Operators that are already used in the core libraries of a language</i></dt>
<dd>If those operators have been around long enough and it is too late to remove them from the standard library then we have no choice but to use them.</dd>
</dl>
All other uses of operator overloading is suspicious. The worst offenders of course are operators used as meaningless abbreviations for method names.<br>
In some cases you will have to watch for compiler quirks and language peculiarities. WIth C# for example when you define the ++ operator on a class it has the same semantic when used as a prefix (pre increment) or
suffix (post increment). In both cases this works like a pre increment operation. This is a bug factory.
In this case I think the compiler should give an error when ++ is used as a post increment operation because it will not have the expected result. You get the same thing with the -- operator.<br>
How would you modify this and what would you add to it ?
JS

This conversation is currently closed to new comments.

7 total posts (Page 1 of 1)  
Thread display: Collapse - | Expand +

All Comments

Collapse -

Never liked it myself

by Tony Hopkinson In reply to My Operator Overloading M ...

The WTF vs WOW ratio is too high.

Collapse -

For me the abuse started with "C++" >> and << for I/Os

by jslarochelle In reply to Never liked it myself

To this day I still sometimes hesitate between << and >>.
I miss operator overloading for math libraries though and logical operation.
JS

Collapse -

Mathematical it can make a greate deal of sense

by Tony Hopkinson In reply to For me the abuse started ...

particularly where there is conversion involved.
Matrices, BCD , complex numbers.

Comparisons on structures and objects. But it should a conpletely obvious.

If MyCow < MyDog when it's actually comparing their mass or age even value, is and abysmally stupid thing to do. You have to open a whole shelf of black boxes to figure out what < means. Violates the entire thinking behind overloading.

Collapse -

Thanks Tony. I think I will steal your example and expand

by jslarochelle In reply to Mathematical it can make ...

..some of the sections with more examples.
:)
JS

Collapse -

You know, Rochelle

by santeewelding In reply to My Operator Overloading M ...

You are inanely narrow and particular.

Open it up, if you know articulatedly how.

I follow, desperately, a lot of it, and only because I applied myself a while back with a whole lot of sustained reading. It is that reading that tells me your voice comes as though from a conduit under the street.

Tony tried, with his left hand.

Collapse -

One can only reply in context

by Tony Hopkinson In reply to You know, Rochelle

It's a very particular topic, and far from inane, if you have to deal with it.

The thrust of the manifesto was basically, just because you can, doesn't mean you should..

A universal truth, particularly when making opaque responses...

Collapse -

Since English is a second (mostly self taught) language ...

by jslarochelle In reply to You know, Rochelle

..for me I sometimes don't know how to respond to this kind of reply. I think I can handle myself pretty good in technical conversations but humor and/or sarcasm is sometimes more difficult.
I suppose this kind of reply might be referring in part to my less than perfect English. I can only answer that I will keep trying to improve this.
JS

Back to Web Development Forum
7 total posts (Page 1 of 1)  

Related Discussions

Related Forums