[java] Java No Operator Overloading Why?

Started by
28 comments, last by GameDev.net 19 years, 4 months ago
Quote:Original post by Anonymous Poster
Quote:
BigInteger num = a + b - c * d / e;


Ha! You fell into the trap ;) :P. The second one is better, because it has explicit binding. The first one would need to be written:

num = a + (b - (c * d / e) )

in order to be as informative as the second.


No, not at all. It's called "order of operations."

And I dispute your claim that good OOP shouldn't use operator overloading. Really what all that comes down to is what your definition of "good" OOP is.
Advertisement
Quote:Original post by Strife
Quote:Original post by Anonymous Poster
Quote:
BigInteger num = a + b - c * d / e;


Ha! You fell into the trap ;) :P. The second one is better, because it has explicit binding. The first one would need to be written:

num = a + (b - (c * d / e) )

in order to be as informative as the second.


No, not at all. It's called "order of operations."

And I dispute your claim that good OOP shouldn't use operator overloading. Really what all that comes down to is what your definition of "good" OOP is.
Don't bother, good little OOP coders cringe at using operators on primitive types anyway.

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

Operator overloading can certainly be abused, but you're looking at some extremely basic examples. For the record, Direct3DX 9 has a C++ implementation that uses operator overloading.

Anyways operator overloading goes far beyond writing "+" instead of "add()". It starts to get cool when combined with templates (for example, any list storing objects with a < operator can be sorted using "sort", or unique/associative using "set" or "map").

If you want an advanced example of operator overloading in the context of template metaprogramming, check out LibSH at http://www.libsh.org.

However since Java does not have templates, then yes: you lose a lot of the advantages of operator overloading.
Quote:for example, any list storing objects with a < operator can be sorted using "sort"


correct me if i'm wrong, but isnt that just the equivalent of implementing the Comparable interface in java? your example of overloading is still debatably useful syntactic sugar...and what youre really talking about is templates anyway.

personally i think operator overloading can be nice, but its unnecessary and can often obfuscate more than what it is replacing...especially, as the AP pointed out, when symbols can mean completely different things to different people.

i like being more explicit, but thats just my style. just set up a preprocessor if it's not yours.
Quote:Original post by justo
Quote:for example, any list storing objects with a < operator can be sorted using "sort"


correct me if i'm wrong, but isnt that just the equivalent of implementing the Comparable interface in java? your example of overloading is still debatably useful syntactic sugar...and what youre really talking about is templates anyway.

personally i think operator overloading can be nice, but its unnecessary and can often obfuscate more than what it is replacing...especially, as the AP pointed out, when symbols can mean completely different things to different people.

i like being more explicit, but thats just my style. just set up a preprocessor if it's not yours.

because by defining an operator instead of implementing an interface, you can write containers that can hold both primitive types and class types. If you implement an interface, then you will have to box those primitive data types. ugly code ensues.

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

Quote:Original post by justo
correct me if i'm wrong, but isnt that just the equivalent of implementing the Comparable interface in java? your example of overloading is still debatably useful syntactic sugar...and what youre really talking about is templates anyway.

Agreed, it is mainly the templates that give operator overloading power, although they can also do some very cool things when combined with recursion :) As capn_midnight noted as well, it allows more flexible containers without needing to "wrap" any existing objects in this interface.

Quote:Original post by justo
personally i think operator overloading can be nice, but its unnecessary and can often obfuscate more than what it is replacing...especially, as the AP pointed out, when symbols can mean completely different things to different people.

Agreed. Like most programming language features, it can be misused. However, that isn't a point for its global disutility.

There are also other things that you can do with operator overloading that are somewhat cool. For example, you can make a data type interchangable with a primative. I've seen people who code "float" classes that compile down to using the primative type directly, but give them the flexability to change between float and double, rounding modes, implementations of "_ftol", etc. It can be pretty powerful actually, and allow another level of abstraction.

Of course it isn't necessary to accomplish anything, but neither is OOP. Both are just tools that in some cases can make the management of larger programs easier, and reduce bugs. Of course both when misused can do the opposite!
Quote:Original post by Anonymous Poster
Ha! You fell into the trap ;) :P. The second one is better, because it has explicit binding. The first one would need to be written:

num = a + (b - (c * d / e) )

in order to be as informative as the second.

As was mentioned by Strife, the order of operations comes into play and naturally implies what will be done first. Yes, operator overloading is just syntactic sugar, but when you get down to it, so is every other part of any high-level language. It's all just sugar that wraps the underlying processor commands. Even assembly could be concidered syntactic sugar, as it uses words (ADD, SUB, MUL, DIV, MOV, etc.) to hide the 1's and 0's that are actually getting fed to the processor. Our lives would be a lot harder without syntactic sugar.

Quote:Original post by Anonymous Poster
*

Has 5 standard meanings, just in basic programming, just off the top of my head. (multiply, cross-product, any-char, any-char-except-dot, FSM-0-or-more). I'm sure if I sat around all day I could think of 3 or 4 more common meanings.

The word 'multiply' has six standard meanings, just looking in my dictionary:

multiply1
1. To increase the quantity, amount, or degree of.
2. Math. To determine the product of by multiplication.
3. To become more in number, amount, or degree; increase.
4. Math. To determine the product by multiplication.
5. To grow in number by procreation; propagate.

multiply2
So as to be multiple; in many ways.

I won't go into the word 'add', as I would have to sit here most of the day typing.

Quote:Original post by Anonymous Poster
Answer: all of them, if you're writing parsers. Because the "standardization" on the first operator used in grammars is so weak that all the above are used frequently :(. They also mean 6 different things, if you're writing parsers. Confused? You will be...

Speaking for myself, the symbols didn't confuse me anywhere near as much as the arguments against them. (No offense.)

Quote:Original post by Anonymous Poster
It's a nice idea to think that the use of simple operators is "easier to read", but it's an idea that any professional programmer ought to soon realise is a pipedream: After more than 2000 years of history, Mathematicians *still* haven't managed to standardize on symbols, and they have a lot more to play with than programmers do. IMHO it is naive to think that a symbol means the same thing to different readers, and it's blatantly not true.

Unlike mathematical symbols, programming languages can easily be standardized, though that decision hasn't been made yet for Java. If you think about it, the mathematical symbols we use are much more universal than the words used to represent their meaning. Pick out at random a programmer who doesn't speak English and ask him or her to interpret the following:

num = a + b - c * d / e

Then ask him/her to interpret this:

num = a.add(b.subtract(c.multiply(d).divide(e)))

Which one do you think s/he will understand quicker? In my opinion, it's naive to think that an English word will mean the same thing to different readers.

I have nothing against the decision to avoid operator-overloading in Java - it makes sense for this language. The technique itself, though, can be a very powerful tool and shouldn't be dismissed as something reserved for people with "poor OOP skill".
It's too bad you can't extend the Java language as you wish, like you can in Lisp.
“[The clergy] believe that any portion of power confided to me, will be exerted in opposition to their schemes. And they believe rightly: for I have sworn upon the altar of God, eternal hostility against every form of tyranny over the mind of man” - Thomas Jefferson
Yes operator overloading, yet again.
Operators are for example:
new
)
>
:
=
,

and imagine overloading this ";"

Of course Java does have operator overloading. Did you ever seen this? System.out.println(" line "+variable) ;
It's operator overloading described in the Java language specification. Java, however doesn't alow to define your own extremely "clever" ways how to ... It's one of big advantages, if you have hunger for operator overloading use an advanced IDE and do your worst. (Just the output files would be readable by any standard compiler, and by some programmers.)

So if you'd like to overload add for vectors, then it might be interesting how your code would look if you'd use some unstandard add in the mix.
And how would look operator for dot and cross product?
Quote:Original post by TheBluMage
The word 'multiply' has six standard meanings, just looking in my dictionary:


c.f. below for my comment re: suitability of such words. I would never use them as method names (c.f. my original points: the advantage of not op-overloading is your ability to use rich method names. Using overly generic words like "multiply" is throwing away that advantage).

As noted previously, you can embellish the word until it's precise enough. Embellishing operators is much much harder, because our vocabulary to describe an operation through operators is very very poor (compared to our vocabulary to describe something in our native language).

Quote:
Speaking for myself, the symbols didn't confuse me anywhere near as much as the arguments against them. (No offense.)


Ah...the confusion comes when you see 3 of them used at once in the same equation. Which means which? Or ... when you see 3 separate equations that use 3 separate operators to mean the same thing. Or do they? Do they mean different things? How do you know? There isn't a standard... (Well, there is, but this is the point: it's a standard honoured more by it's avoidance than it's adherence)

Quote:
Unlike mathematical symbols, programming languages can easily be standardized, though that decision hasn't been made yet for Java.


! The moment you do that, you have removed operator overloading.

Java standardization of operator meaning == exact opposite of adding operator overloading into the syntax of the language.

You can't have it both ways...

Quote:
Pick out at random a programmer who doesn't speak English


That is a great point. Unfortunately, the way the world works, you have to speak english to be a programmer, or else suffer massive restrictions on your employability. If API's tended to be i18N'd then you would be going somewher; however, at the same time, different nationalities are considerably more likely to disagree on symbol meaning than same nationalities: this is the fault of the mathematicians, because over the centuries symbols have been divided along teacher/student groupings AND national boundaries. e.g. there are common symbols which German mathematicians "tend" to use where English ones "tend" to use a noticeably different one.

Quote:
In my opinion, it's naive to think that an English word will mean the same thing to different readers.


Point taken. But...as I said earlier (I think it got lost in the discussion going on here) the word "add" or "multiply" is a bad method name anyway (ask any good OOP practitioner) - you'd typically use 3 words for the method name (c.f. my earlier examples of appropriate names in a 3D engine).

By that point, the English words are going to mean the same things to different readers, because of the increased amount of context, but ... some may need a dictionary to work out what they mean. As noted above, this is sad, but the way the world works.

This topic is closed to new replies.

Advertisement