Overloading and Smooth Operators
An anonymous reader writes "IBM DeveloperWorks has an interesting article on operator ad hoc polymorphism (operator overloading for the uninitiated). With the increase in Java popularity and their banning of operator overloading (among other things) the author decides to show some of the great benefits that operator overloading can bring, as long as it is served with a 'healthy dose of caution.'"
Sure, there are lots of things you can do with operator overloading, but there are good reasons to avoid them. Ultimately, it comes down to some basic questions:
Will the use of overloading operators...
*) reduce developement time?
*) reduce the number of bugs?
*) improve maintainability?
In most cases, the answer is at best murky. Sure, if you're doing mathematical programming, adding complex numbers, rational numbers (tracking numerators and denominators instead of using floats), or something like that, then it's intiutively a good thing. But in most cases, it's not intuitive. When someone else comes to the project and tries to figure out what's going on, it's like having a bunch of extra macros for them to look up. Function calls make it much more obvious what's going on.
Contrary to the article that states that a++ and ++a can be aliased to one call a.next() this is not how it should be done: ...)
a++ needs to return the value of a BEFORE it is incremented
++a needs to return the value of a AFTER it is incremented
Sorry for the rant, I've just spent too long working with programmers that didn't know the difference
(Not at my current job mind you
Search your logs like the web: splunk!
Operator overloading is too easy to abuse because others won't necessarily understand what a + or - or x, etc. means. I think this post says it all:
//now tell me: //is (to me) much clearer than: // or even than: // or isn't it?
-= cobnet -= wrote:
>
> Now I wonder why Java doesn't allow overriding of operator, because I think
> it makes things a lot easier...
>
> For example... presume you have written a class Point() and that you test
> this class:
> Point p1 = new Point (2,3);
> Point p2 = new Point (46,4);
> Point p3 = new Point (5,8);
> Point p4 = new Point (5,5);
>
> Point p = p1 + p2 + p3 +p4;
>
> Point p = p1.add((p2.add(p3)).add(p4);
>
> Point [] pArray = { p2, p3, p4};
> Point p = p1.add(pArray);
>
It isn't. That seems to be a classic example of abusing operator
overloading. Adding two points together makes absolutely no sense. If
the intent is to create a collection of points, then a Collection should
be used, along with the associated add() methods.
>
> So what made java didn't allow this overriding of operators??
>
A plethora of code similar to your example.
Jim S.
Those who open their minds too far often let their brains fall out.
Used poorly, it results in crap. But does that mean that we should block off entire development tool from programmers?
A properly designed and applied overloaded operator can be a great tool for development.
A poorly designed or inappropriatly applied overloaded operator can create a mess of code and a maintenance nightmare.
Now, replace "overloaded operator" with a blank and fill it in with what ever you like. "data layer", "abstract class", "architexture", etc... But if the powers that be decide that programmers need to be protected from data access, inheritance, and design fundamentals, what the hell are we left with?
-Rick
"Most people in the U.S. wouldn't know they live in a tyrannical state if it walked up and grabbed their junk." - MyFirs
Will the use of overloading operators... *) reduce development time? *) reduce the number of bugs? *) improve maintainability?
If the answer is yes to the above (This is an all or nothing test), then use them, if not don't use them.
There is a good reason that most examples of operator overloading are complex numbers - using overloaded operators for complex numbers reduces development time, reduces the number of bugs, and improves maintainability.
There is a good reason examples of operator overloading never use shapes. While I can design an shape interface where
circle + square
is legal, and gives some useful result. However this will increase development time, increase the number of bugs, and reduce maintainability. Therefore anyone use overloads operators for shape classes is a fool.
Operator overloading is often abused. I rarely find it useful to overload operators, but in those few cases where I overload an operator I make my code better.
You're entirely wrong. Groovy is another language that targets the JVM. Groovy is either compiled to Java bytecode (and from there, JIT compiled to machine code by the JVM), or it's compiled at runtime in a JIT fashion. There is no interpreter written in Java that runs Groovy code. Groovy is just another language that targets the JVM.
The benefit from all this is that in Groovy, you can interface to Java classes with literally no overhead. It's a brilliant idea for a new language because all the libraries are already there! This skips past one of the biggest stumbling blocks for new lanaguages.
All in all, Groovy's a pretty interesting language that's worth looking into, but apparently you can't be bothered to do a Google search.
Yeah, that's a lot more readable.
This is precisely why operator overloading is both unneeded and unwelcome. Like an earlier post said, the benefit doesn't even come close to outweighing the price. "<=>" is a perfect, classical example of how programmers get positively drunk with freedom and treat operator overloading like a shiny new toy. Code full of that instead of "compareTo" is an instant eyesore. I'd rather spend my time developing, not deciphering.
The Internet is full. Go away.
I love the smell of hypocrisy in the morning. It smells like... coffee.
Seriously, operator overloading is a powerful technique that, done right, allows you to write clear, expressive, maintainable code. Done wrong it allows you to write foul spaghetti, but any language allows you to write foul spaghetti --- Flon's axiom: There does not now, nor will there ever, exist a programming language in which it is the least bit hard to write bad programs.
Not allowing operator overloading (except when the language authors break their own rules) has no effect other than to reduce the options available to the programmer. It means that while you can't use them in cases where operator overloading is not useful, it also means that you can't use them in cases where operator overloading is useful and would produce better code. It means you can't, for example, create an imaginary number class that works the same way as int or long. This is not the mark of a good, extensible, expressive language.
Leave the operators as they are because they are useful (or let people overload them if they don't work as desired) but allow for defining new operators. [...] Allow for defining completely arbitrary syntax that way.
:-)
As usual, there already is a language that allows this, and (as usual) it's called Lisp.
First, in Lisp, + is not a special case in the syntax: it's the name of a function, like anything else. If you want to define a function called *#@!*, there's nothing stopping you. (You can name them pretty much anything, IIRC, but if it has something crazy like whitespace in the function name, you'd obviously have to escape/quote it.)
A lot of people don't like writing some things in prefix notation, so: Second, Lisp (at least Common Lisp) has "reader macros", which let you define your own syntax for the reader. This is possible due to a few (related) reasons: Lisp doesn't really have a syntax -- you're basically writing the ASTs directly -- and all the language is available all the time -- so you can write code to write code at read-time. You can write a reader macro to parse arithmetic in infix notation (like C), for example, and then embed that in your Lisp program.
If you're doing a lot of network transfers and think a special syntax for that would be handy, you can write a function to rewrite your own syntax into Lisp (at read-time, before it gets to the compiler, so it's still compiled).
This is exhibit #473 in "Why isn't Lisp dead yet? Isn't it older than dirt?".