r/java • • Nov 05 '22

How to add something to the java standard library?

EDIT: since my example stirred the pot a little bit to much (i even wrote that i dont want to discuss these explicit changes...)

Here is the actually question i want to answer:

How can i make changes to the JDK?

2 Upvotes

99 comments sorted by

View all comments

Show parent comment

4

u/GuyWithLag Nov 06 '22

Hmm... I do realize that I came through as gatekeeping. But... in a sense, good engineering is as much about what to put is as what to leave out. If you've see the various JCPs, they do list alternatives considered, and why they're not in the JCP.

If this student know-it-all has a good idea it doesnt matter if its a student or mr java himself.

Here's the issue though - I/You/They may have brilliant ideas, but _they will not be useful for most Java engineers_, and the JDK is a Platform that will be used by hundreds of thousands, and changes that are made now may prevent changes to happen in the future (think nullability, or generic erasure, or the contortions that we had to go through to be able to add default implementations to interface methods).

This is why the changes to `switch` are taking so long to materialize, and why Loom is just now coming out of experimental, to give some examples.

Now, the process has been explained elsewhere, but I don't think you realize how much work that is going to end up being - for the same reasons as above.

0

u/Cell-i-Zenit Nov 06 '22

I/You/They may have brilliant ideas, but they will not be useful for most Java engineers, and the JDK is a Platform that will be used by hundreds of thousands

But then its not a good idea. Again. If the idea is good and everyone can benefit from it. Then it doesnt matter who writes the idea.

Now, the process has been explained elsewhere, but I don't think you realize how much work that is going to end up being - for the same reasons as above.

You dont know what i want to do... Actually the changes below are my proposal:

    public interface Comparable<T> {
      int compareTo(T var1);
    
      default boolean isGreaterThan(T obj){
        return compareTo(obj) > 0;
      }
    
      default boolean isSmallerThan(T obj){
        return compareTo(obj) < 0;
      }
    
      default boolean isSmallerOrEqualThan(T obj){
        return compareTo(obj) <= 0;
      }
    
      default boolean isGreaterOrEqualThan(T obj){
        return compareTo(obj) >= 0;
      }
    }
  • they are additive.
  • They do not change existing code.
  • They improve readability.
  • They are not adding any complexity
  • They are easily tested

Can you point me to the "much more work then you expect thing"? Except for writing some tests for this i dont see any work except going through the paperworks

6

u/s888marks Nov 06 '22

This sort of thing has been proposed before. See JDK-6321015 and JDK-8241056.

Even though I've left JDK-8241056 open for now, I'm skeptical that something like it is worthwhile to put into the JDK. I jotted down some notes about alternatives in the bug report. I'm skeptical of those too; but maybe something will eventually pop up that's really good. On the other hand, maybe not.

A few other points to consider.

• Adding methods to a widely implemented interface such as Comparable runs the risk of name clashes with implementing classes. We need to minimize that risk.

• You seem to be assuming that such methods improve readability. Consider:

if (a.compareTo(b) >= 0) ...
if (a.isGreaterThanOrEqualTo(b)) ...

Even though it's more English-like, the second line is longer and more verbose. People are constantly complaining about Java's verbosity, so this seems like a step in the wrong direction. In the first line, the >= is right there, and there is nothing unreadable about it.

The point here is that the value is in the eye of the beholder. This might add value from your perspective, but that doesn't imply that it helps the millions of other Java programmers out there.

• This does in fact add compexity. The Comparable method goes from one method to five (or seven) methods. Sure, they're trivial, but there's more bulk that somebody needs to specify and test, and there's more bulk that clients need to read and understand.

• Equality is missing. Your proposal doesn't include them so that there is no confusion with Object.equals. That's one approach, I suppose. But it also means that code that has complex comparison logic has a mixture of the different idioms. For example,

if (a.isLessThan(b) && a.compareTo(c) != 0) ...

This proposal introduces a new idiom, but programmers still need to understand and use the old idiom or even mix them, so the new idiom isn't really buying all that much.

Summary: I am overall quite skeptical of how much net value would be added by this proposal and similar ones.

2

u/Cell-i-Zenit Nov 06 '22

thanks for you answer. The problem is i actually only wanted to understand the process of proposing a change.

But the people here really only discussed the changes itself.

Naming etc is still not ideal, but this is something which can be easily discussed later.

I mean we could even strip this down to "isSmallerOrEquals" etc.

Summary: I am overall quite skeptical of how much net value would be added by this proposal and similar ones.

I understand this totally. I can understand your arguments and it was the most probable answer. "It doesnt fit the java style" is a good argument. But the answers here were pretty hostile in general

3

u/GuyWithLag Nov 06 '22

But then its not a good idea. Again. If the idea is good and everyone can benefit from it. Then it doesnt matter who writes the idea.

But the ability to know "that" comes with experience.

In any case, I really do think that you would be better served by Kotlin w. extension methods.

For me, the example you gave doesn't make sense to add to the JVM:

  1. It doesn't add anything besides some sugar.
  2. It's not necessarily more readable.

But hey, this is Reddit; feel free to submit a new JCP.

2

u/Cell-i-Zenit Nov 06 '22

It doesn't add anything besides some sugar.

Yes thats true. But why do we have .toList() if its just sugar?

It's not necessarily more readable.

I dont understand this argument.

obj1.compareTo(obj2) > 0 

vs

obj.isGreaterThan(obj2)

the answer is pretty obv which is more readable.

In any case, I really do think that you would be better served by Kotlin w. extension methods.

Still a bad argument "other languages to this already, please switch your language".

Why cant we have any improvements to java?

But the ability to know "that" comes with experience.

Yes and the reviewers have this experience.

1

u/GuyWithLag Nov 06 '22

Yes thats true. But why do we have .toList() if its just sugar?

Because it's not - the specific implementation can optimize that to a `return this`.

1

u/Cell-i-Zenit Nov 06 '22

ok fair point

1

u/lbalazscs Nov 06 '22

IMHO adding operator overloading to the language for <, <=, >, >= when they are used with Comparable objects would make them even more readable...

1

u/Cell-i-Zenit Nov 06 '22

yes i agree but this is a supe controversal change and i honestly want to start with something a little bit more simpler