Overloading and Smooth Operators 75
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.'"
Useful? Doubtful. (Score:5, Insightful)
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.
Re:Useful? Doubtful. (Score:1, Flamebait)
Java disallowing C++-style constants, operator overloading, templates and everything else useful just adds to the theory that Java is C++ for stupid people.
Re:Useful? Doubtful. (Score:2)
Re:Useful? Doubtful. (Score:1, Flamebait)
Re:Useful? Doubtful. (Score:2)
Re:Useful? Doubtful. (Score:2)
Re:Useful? Doubtful. (Score:2)
Re:Useful? Doubtful. (Score:2)
Re:Useful? Doubtful. (Score:2)
Funny, it seems like there are very few actual C++ positions that are not Windoze/MFC positions. So if you want to be a Windoze Weenie, stick with C++/Visual C++ . Sure there's lots of C++ positions around, that don't pay, developing on platforms like KDE and GTK, the the paying positions seem to be few and far between.
Personally I want to get paid to code, and I don't want to code for or on Windoze, so I use Java.
I've also the garbage C++ code produced on the *NX platform. People want to move to Java
Re:Useful? Doubtful. (Score:3, Interesting)
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 n
Re:Useful? Doubtful. (Score:1)
That's what I keep hearing, but I've never actually seen anywhere dubious usage of it, such as your "circle + square" example.
Re:Useful? Doubtful. (Score:2)
That is because you are used to the cout << foo; syntax, so you don't think of it as abuse. Though it can be argued that there is no better way, I don't know if I buy that.
Re:Useful? Doubtful. (Score:1)
*) reduce developement time?
*) reduce the number of bugs?
*) improve maintainability?
In most cases, the answer is at best murky.
Then it follows that avoiding operator overloading is subject to the same test. That's not an argument against operator overloading.
I've seen lots of sucky C++ code, and operator overloading has never been one of the problems -- maybe because sucky C++ code tends to be written by either C or Java programmers ...
Re:Useful? Doubtful. (Score:1)
A++ and ++A are NOT the same (Score:4, Insightful)
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
Re:A++ and ++A are NOT the same (Score:2)
Re:A++ and ++A are NOT the same (Score:2)
++a : call a.next() and evaluate a
a++ : evaluate a and call a.next() then.
and I think we can agree that sane programs should use the same operator overloading for ++ in both cases.Re:A++ and ++A are NOT the same (Score:1)
Re:A++ and ++A are NOT the same (Score:2)
Its a reasonable argument. The problem is the variable "you". As an example, what seems obvious to programmer A might not make sense to programmer 2.
Custom object (Score:2)
or
CustomObject1 * (CustomObject2 - CustomObject3 ^ 10)
Re:Custom object (Score:1)
Languages of the ML family, for example, permit the definition of arbitrary new operators, they just don't permit you to overload existing operators. This means that when you see +, you know it will be adding two numbers, not concatenating two collections or whatever. But if you want an operator to concatenate your collections, you can define o
comp.java.lang.programmer 2001 (Score:2, Insightful)
-= 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,
Re:comp.java.lang.programmer 2001 (Score:3, Insightful)
It's extremely useful for solving, i.e. the following two problems.
Re:comp.java.lang.programmer 2001 (Score:2)
I'm not disagreeing with you, per se, but this is true only for vanishingly small values of common.
Re:comp.java.lang.programmer 2001 (Score:1)
That's one of the main reasons why huge clusters get built...to perform simulations and process physics data.
Re:comp.java.lang.programmer 2001 (Score:2)
To make a car analogy (why the hell not... it seems like a good one) it's kind of like saying bumping another car is a common technique (the word 'technique' is important to my meaning). From
Re:comp.java.lang.programmer 2001 (Score:1)
I still think it's useful, though I can't come up with any examples not solvable by other methods. But the nice thing about C++ is, like Perl, There's More Than One Way To Do It.
Also, I don't think a language should be designed around keeping a stupid programmer or project team from shooting themselves in the foot.
Re:comp.java.lang.programmer 2001 (Score:2)
I definitely agree with you there. It's not like the 'eye on safety' in Java has prevented people from writing some of the ugliest code on earth.
Re:comp.java.lang.programmer 2001 (Score:1)
Well, all languages are designed with some safety features, like type checking. There really isn't anything wrong with different languages designed for not only different problems, but different skill levels. When Java was introduced it was touted as the language "for the rest of us". That's fine, not everyone has a CS degree and wants to get down to the C or C++ level. D
Re:comp.java.lang.programmer 2001 (Score:2)
Re:comp.java.lang.programmer 2001 (Score:1, Insightful)
No, it doesn't. And you'll note that this has nothing to do with operator overloading. Without it, he's still doing p2.add(p3), which is equally meaningless but more verbose.
OTOH, if you have to do, say, fixed point math in J2ME, you end up with code that looks like the bastard child of Lisp and Perl.
There are many wrong places for operator overloading, but it's indispensible for the right places.
Re:comp.java.lang.programmer 2001 (Score:1)
Re:comp.java.lang.programmer 2001 (Score:2)
Frankly, having point/matrix/quaternion/plane/etc classes with reasonably chosen operator overloading allows me to condense a LOT of code into quite little, with as far as I can tell a general *increase* in legibility.
When I started doing 3d programming, for robotics simulation, I used a typedef'd float[4] for vertices, and float[16] for matrices. I had C functions for adding, dot products, cross products, matrix m
Use it wisely or not at all. (Score:2)
LOL "loss of a valuable shorthand" (Score:1)
Like any tool (Score:4, Insightful)
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
Re:Like any tool (Score:3, Insightful)
If you want an OO language that doesn't try to hedge the coder in with rules for his own good, you aren't programming in Java. You're using C++. If you don't care for inheretance and templates, you may even
Re:Like any tool (Score:3, Insightful)
Its one thing to protect an environment from a stupid coder. Its another to protect a stupid coder from themselves.
-Rick
Re:Like any tool (Score:2)
-Rick
What is this Groovy? (Score:1)
I have heard before that performance is no longer relevant, but I have seen few designs that take this opinion so serious
Re:What is this Groovy? (Score:5, Informative)
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.
Re:What is this Groovy? (Score:2)
This is indeed a good way to integrate seamlessly with Java.
Jython takes the same approach with Python (Python source is compiled to Java bytecodes) to allow tight Java integra
Re:What is this Groovy? (Score:2)
Have looked at Groovy. Its been around for years, and still hasn't delivered on what's promised. Its all hype, and pretty useless for anyting non-trivial.
Check JRuby [sourceforge.net] for the 90% solution.
As long as we're limited to few characters... (Score:2)
content >>>> home @ host, *) proto=UDP, *) markas=BULK;
Allow for defining completely arbitrary syntax that way. Sometimes constructs imposed by the la
Re:As long as we're limited to few characters... (Score:2)
Unfortunately I know that there exists no language (it might be possible in some scripting languages) that has such a feature. Perhaps when I get around to finishing MODL2 (my own damn language, the second) I'll add in not just operator overloading, but operation definition.
Example custom operators that would be useful:
Re:As long as we're limited to few characters... (Score:2)
Most languages with real (not preprocessor) macro support (notably LISP, Scheme, Dylan) support this.
Re:As long as we're limited to few characters... (Score:1)
Re:As long as we're limited to few characters... (Score:2)
Re:As long as we're limited to few characters... (Score:2, Insightful)
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 pr
Re:As long as we're limited to few characters... (Score:1)
The unique specialness of an operator is that it may exist in a non-LL form, typically to provide infix notation. This is what makes an operator different from a function. But the defining feature of Lisp's syntax is that it is LL. It seems to me that by definition Lisp cannot support operator overloading. Sometimes Lisp is not the answer to any question foo.
Re:As long as we're limited to few characters... (Score:2)
Re:As long as we're limited to few characters... (Score:2)
Meh. I really need to do more Haskell coding.
Blech (Score:2, Insightful)
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 ti
Re:Blech (Score:2)
Yeah, that's a lot more readable.
Well yeah, actually, it is. Take a look at the kind of domain specific mini-languages that have been developed on top of Haskell and more recently C++. For example, the Spirit parser library for C++ which is part of Boost. Once you start composing complex expressions, infix operators are virtually required to make the end result anything close to readable. Haskell is a big win over C++ in this respect because (apart from all the advantages it has as a functional language)
Java bans operator overloading? (Score:4, Informative)
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: [sysprog.net] 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.
Re:Java bans operator overloading? (Score:2)
The String is a pretty ugly hack, and the "+" operator should never have been used f
Re:Java bans operator overloading? (Score:2)
Except sometimes the compiler optimises this into creating a StringBuffer object,
Re:Java bans operator overloading? (Score:2)
Since Strings are immutable in Java, if you do "a" + "b" + "c", it will first create 3 Strings. Then it will add "a" and "b", creating a new String. Now *that* String will be formed into *another* new String by concatenating "c". That's quickly building up to the creation of a lot of String class instances. See "Effective Java" by Joshua Bloch.
Well actually, almost all java compilers would convert:
"a" + "b" + "c"
into "abc"
and "a" + s + "c"
into
new StringBuilder().Append("a").Append(s).Append("c"). ToString()
Re:Java bans operator overloading? (Score:2)
Shouldn't this be that no language makes it any harder to write bad code than to write good code? I mean, it seems to me that it really is quite hard to write a bad program (which does something useful) in Intercal, bf, or optimal IA64 ASM...
Oh, please. (Score:2)
An "Ask Slashdot" re the stupidest overloadings we've ever seen might be fun, but discussing whether the functionality is a good idea is absurd. Any discussion should center on when to use it, not whether to use it.
Re:Oh, please. (Score:2)
Re:Oh, please. (Score:2)
Interestingly, there are languages that do this. OCaml, for example, uses "+" exclusively for integers; for floating-point numbers, "+." is used instead.
Now, OCaml's not a mainstream language, but it's reasonably popular as minor languages go. There's quite a few programmers ou
Really bad examples (Score:2)
I hate it when writers are trying to explain something, and then use the most awfull programming constructs to show you how to use the feature. Even if the bad code is unrelated, it still teaches the
Re:Really bad examples (Score:2)
Security people will HATE you (Score:1)
A safe way to provide operator overloading (Score:2)
For example, to overload "==" make the software implement a routine "comparitor" that returns a structure of public elements that the runtime resolves as comparison itself. To implement String's comparitor, have it return a structure like this: { int; byte[]; }
The result is that the compiler can figure out how to compare t