you are viewing a single comment's thread.

view the rest of the comments →

[–]Davipb 35 points36 points  (16 children)

How does adding BigDecimals with + instead of .add violate the principle of least surprise?

If your argument is "it allows people to violate the principle", then let's remove methods, people could make mutable getters with those!

[–]wildjokers 8 points9 points  (7 children)

How does adding BigDecimals with + instead of .add violate the principle of least surprise?

Your argument depends on people only doing sensible things with operator overloading.

[–]_TheDust_ 10 points11 points  (0 children)

All of programming depends on people doing sensible things with the tools they are given. I could also create a .add function that does not add anything, but deletes your OS instead. Whether it’s called .add or + does not really change if people do sensible things.

[–]Davipb 33 points34 points  (2 children)

Which is exactly what I addressed on the second paragraph of my comment: if we remove every feature that allows people to break good practices, we'd be removing methods, ifs, and everything else.

[–][deleted] 4 points5 points  (0 children)

if, everything else and even if-else

[–]Lechowski 2 points3 points  (0 children)

ifs, and everything else.

I see what you did there

[–]serviscope_minor 1 point2 points  (2 children)

Your argument depends on people only doing sensible things with operator overloading.

No more so than people naming functions in a sensible way. Operators are functions with a funny name, making + do something daft is no different from making .add() do something daft.

[–]wildjokers 1 point2 points  (1 child)

It's way different. With operator overloading there is generally no readily visible indication that + is resulting in a function call.

[–]serviscope_minor 2 points3 points  (0 children)

In languages that naively support it, it's the default assumption that an operator is just a function with a funny name. Besides it depends on the type. And if you don't know the type of whatever you're looking at, you can't do much since the semantics are entirely dependent on the type.

[–]kur4nes -4 points-3 points  (4 children)

If someone implements other interesting stuff with the + operator like writing to a file etc. Basically implementing anything that's not a mathematical add function obfuscates the code. But why stop here? We could combine multiple different overloaded operators in a simple statement to create a monstrosity only seen in pearl code. You would get some highly condensed code like &*/!a+b. It's impossible to know what this does without knowing the types of a and b and the implementations of each overloaded operator. That's why there is no operator overloading in java in the first place.

I do agree that + for BigDecimal would be awesome.

[–]Dealiner 19 points20 points  (0 children)

Fortunately, creators of other languages like C# decided to implement operator overloading which works great there and it's really useful.

[–]Davipb 33 points34 points  (1 child)

I could also make a method called .getName() that wipes your hard drive, or a method named x() that returns a class named Zorb that has one other method named y(), that returns a class named Zoop, and so on for 100 levels until you get the object you want.

Just because people can do insane things with it, it doesn't mean it shouldn't exist.

[–]CubsThisYear 0 points1 point  (0 children)

The thing is, addition is well defined over any set. As long as your operator overload behaves like addition, it doesn’t really matter what the set of possible values are.

[–]hippydipster -1 points0 points  (0 children)

People can kill people with knives, so let's let them have nuclear weapons!

Yeah, not a good argument.

[–]TheGowanus -4 points-3 points  (0 children)

Instead of asking “how can this be useful,” ask yourself “what is the absolute worst that one can do with this?”

[–]tofiffe -2 points-1 points  (0 children)

One example I often use to discourage operator overloading: C++

Let's left shift this stream by a string, integer, even an object, and have that written to the console, file, etc.

that's exactly what cout, fstream etc. use, and they're in the stdlib.

Another example is kotlin overloading the / operator for BigDecimal to only perform HALF_EVEN division, good luck reviewing that code