Chapter 67. Read OpenJDK Daily

Heinz M. Kabutz

OpenJDK consists of millions of lines of Java code. Almost every class violates some “clean code” guidelines. The real world is messy. There is no such thing as “clean code,” and we will struggle to even define what that is.

Experienced Java programmers can read code that follows different styles. OpenJDK has over a thousand authors. Even though there is some consistency in the formatting, they code in disparate ways.

For example, consider the Vector.writeObject method:

private void writeObject(java.io.ObjectOutputStream s)
        throws java.io.IOException {
    final java.io.ObjectOutputStream.PutField fields = s.putFields();
    final Object[] data;
    synchronized (this) {
        fields.put("capacityIncrement", capacityIncrement);
        fields.put("elementCount", elementCount);
        data = elementData.clone();
    }
    fields.put("elementData", data);
    s.writeFields();
}

Why did the programmer mark the local variables fields and data as final? There is no reason why this was necessary. It is a coding style decision. Good programmers can read code equally well, whether the local variables are final or not. It does not bother them either way.

Why is fields.put("elementData", data) outside of the synchronized block? This may have been due to a premature optimization, wanting to reduce the serial section of code. Or perhaps the programmer was careless? It is easy to want to optimize everything we see, but we need to resist this urge.

Here is another method from the Spliterator inside ArrayList:

public Spliterator<E> trySplit() {
    int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
    return (lo >= mid) ? null : // divide range in half unless too small
            new RandomAccessSpliterator<>(this, lo, index = mid);
}

This method would definitely raise all sorts of “clean code” warning bells. Those in love with final would complain that hi, lo, and mid could be final. Yeah, they could. But they are not. In OpenJDK they generally do not mark local variables as final.

Why do we have this obscure (lo + hi) >>> 1? Could we not rather say (lo + hi) / 2? (Answer: it’s not exactly the same.)

And why are all three local variables declared on a single line? Is that not violating all that is good and proper? 

Turns out, according to research, the number of bugs is in proportion to the lines of code. Spread out your method the way that your university professor asked you to, and you have more lines of code (LOC). And with more LOC, you also end up with more bugs for the same functionality. It can also be that rookie programmers tend to spread their code over many pages. Experts write tight, compact code.

We need to learn to read many different styles of coding, and for that, I recommend OpenJDK. Read the java.util classes, java.io, and so many others out there.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.200.143