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 ...
Get 97 Things Every Java Programmer Should Know now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.