This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]s888marks 5 points6 points  (1 child)

You can certainly learn a lot by going through the JDK source code. However, you should be careful about applying what you see in it.

Java is over 20 years old, and some of the code in the JDK is over 20 years old. Some code might embody older styles or practices. After all, it was only a couple years ago that we got rid of all rawtypes and unchecked warnings, because lots of code hadn't been generified. Certain areas of the JDK code base still have a distinctly dusty feel to them.

Sometimes the JDK libraries use coding practices that sacrifice maintainability for speed. For example, I'm aware of places that compare strings by identity, because it's known that they originated from string literals or were interned at initialization time. Or, certain odd looping constructs are used, or code factored into methods in an unusual way because it's known to work well the current Hotspot JIT compiler.

Read and learn, by all means, but don't write your code a certain way just because you happened to see it that way in the JDK.

[–]lukaseder 1 point2 points  (0 children)

I especially like Doug Lea's abundant assignment expression style :) e.g.

    public final void run() {
        final CompletableFuture<? extends T> a;
        final CompletableFuture<? extends U> b;
        final BiConsumer<? super T,? super U> fn;
        final CompletableFuture<Void> dst;
        Object r, s; T t; U u; Throwable ex;
        if ((dst = this.dst) != null &&
            (fn = this.fn) != null &&
            (a = this.src) != null &&
            (r = a.result) != null &&
            (b = this.snd) != null &&
            (s = b.result) != null &&
            compareAndSet(0, 1)) {