× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Walden H. Leverich wrote:
Operator overloading is not that questionable. Overloading the =
operator may be though. :) It basically boils down to a question of
whether your class should exhibit value semantics or reference
semantics.
But there is no question. In most OO languages that I'm familiar with, objects exhibit reference semantics, or at least default to it. (C++ is a unique language in that it allows just about every programming concept known to man, so it supports both reference *and* value semantics.) But regardless of the perceived benefit, if overloading the = operator makes your code work differently than everything else while still looking the same, I still put that squarely in the "clever" category.

After all given:

int x=7;
int y=x;
x = x + 1;

you'd expect x to equal 8, and y to still equal 7, no?\
Yes, because x and y are primitives. Because objects are not primitives, they don't exhibit the same behavior as primitives, nor should they. And at the very least, *whatever* they do, they should do consistently. Overriding operators that have established meanings is a bad practice. This is my opinion, but I'm not alone nor am I likely to change my mind.

Operator
overloading would allow a fix to the silly problem with decimals. I
could simply add two decimals and get the right answer, because while
the BigDecimal is an object, it should exhibit value semantics.
Eh. Sure. What would really fix it is to have a decimal primitive. Any language that doesn't support a decimal primitive is not a business language. Again, my opinion. Again, hard to convince me otherwise.


It falls in the category of clever coding, and can lead to absolutely undebuggable code.

Sure can. But it can also make code more readable.
Okay. But only to *you*. It will make it less readable to somebody who doesn't know you're overloading the operator. They'll assume that = does what = always did. That's my point; if it already has a distinct behavior, changing that behavior to something completely different is not more readable, except to the person who knows about the change.

Like everything else, it's a tool. Used the right way it helps, used the wrong way it hurts.
Overloading operators isn't a tool so much as a technique. And there are bad techniques. Multiplying a date by 100.0001 is a bad technique. It might have been cool at the time, it might even have been accepted, but sober reflection indicates that it really isn't a good idea.

Of course, since Java doesn't support operator overloading you'd expect
pro-java people to take the position that it's not needed wouldn't you.
:)
Bad assumption. Before I was a Java programmer I was a C++ programmer, and I learned my dislike for operator overloading back then, for exactly the reason I explained: in my opinion such techniques ultimately reduce maintainability.

Okay, this is clearly an opinion thread, and I've stated mine pretty distinctly. No more for me!

Joe

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.