Beyond average: 10 best practices for coding Java APIs

Published around this time last year, jOOQ creator Lukas Eder's Java programming best practices are written as a series of caveats to the standard wisdom of API design. Eder writes: "Being an internal DSL, jOOQ challenges Java compilers and generics to the max, combining generics, varargs and overloading in a way that Josh Bloch probably wouldn’t recommend for the 'average API.'"

Digging into the "beast" that is Java, Eder emerges with some perennial best practices -- "It often makes sense to free memory in the inverse order of allocation"; "Avoid returning anonymous, local or inner class instances from methods to the outside scope"; "Arrays or Collections should never be null" -- and a handful related to new features in Java 8 (see his comments on using defender methods, Optional, and single abstract methods). More controversially, Eder takes on the question of whether it's ever safe to make methods final by default:

[I]f you’re in full control of all source code, there’s absolutely nothing wrong with making methods final by default, because:

  • If you do need to override a method (do you really?), you can still remove the final keyword
  • You will never accidentally override any method anymore

Read more in Lukas Eder's blog, Java, SQL, and jOOQ ...

This story, "Beyond average: 10 best practices for coding Java APIs" was originally published by Java Everywhere.

Join the discussion
Be the first to comment on this article. Our Commenting Policies
See more