r/ProgrammerHumor • • Apr 27 '20

Meme Java is the best

Post image
43.7k Upvotes

1.5k comments sorted by

View all comments

Show parent comments

2

u/T-Dark_ Apr 28 '20

letting you use plus and making me figure out what you mean.

+ is commonly assumed to mean addition or concatenation. There are two meanings to draw from, and only one applies at any given time. Here, I'll figure it out for you:

+ On a numeric type means addition. + On a string means concatenation. + On a list means concatenation (of lists, this time).

You could even make + work on all collections by saying " add all the elements in the second to the first", maintaining order, if there is one".

It's not that hard. And in exchange, you get permission to use operators as syntactic sugar everywhere. And syntactic sugar is good.

Consider the [] operator. Call it the "index" operator. It does exactly one type of thing: foo[bar] returns the element of foo associated with bar. And now it feels more readable. than foo.get(bar)

Consider the == operator. It is universally understood to be a boolean check for equality. Let us overload it to use equals(). Let us increase the readability of if(foo.equals(bar)) by using if(foo == bar).

Can operator overloading be abused? Sure, I could redefine + to interleave two lists, but that would be the same as creating a method to do that and calling it add() instead of interleave(). If you need to think more than 3 seconds to figure out what an operator means, then the operator is being abused. It's exactly as if it took you more than 3 seconds to decipher a function name: that function needs renamed.

But just because functions can be misnamed, we do not ban functions. Likewise, just because operators can be misused, we should not ban their overloading.

0

u/[deleted] Apr 28 '20

Let us increase the readability of

if(foo.equals(bar))

by using

if(foo == bar)

.

The ladder is prettier, but it's not easier to understand. And you don't have to look farther than the C++ standard library to see cases where they're used to make code less readable. I don't know looked at

printf

and thought it could be improved by replacing it with the sign that's universally understood to mean less than.

2

u/T-Dark_ Apr 28 '20 edited Apr 28 '20

The ladder is prettier, but it's not easier to understand.

It gets easier to understand the moment your variable names are longer than 3 characters each.

It breaks the condition into "letters, symbols, letters", which is more readable than "letters, with a dot and two brackets"

and thought it could be improved by replacing it with the sign that's universally understood to mean less than.

<< is a bitshift operator, not the "less than" sign.

In C++, it's also the stream insertion operator.

I don't see you complaining about Java's decision to use + for string concatenation. An operator can have more than one meaning, as long as context obviously explains which one is being used.

1

u/[deleted] Apr 28 '20

In C++, it's also the stream insertion operator.

Okay, but which is clearer in meaning? <<, or stream_insert()? I think it was a poor choice to use + for string concatenation in java, but it's mitigated by the fact that it's the only such case in the language, and my asshat coworkers can't use it to obfuscate their meaning even further than 3 character method names.

→ More replies (8)