Cheat Sheet: Designing a Java API
This article is a quick-reference cheat sheet based on a great Devoxx presentation by Mario Fusco – “Let’s Make a Contract: The Art of Designing a Java API.”
The goal of this article is not to explain the concepts in depth but to provide a compact reminder of the key principles to revisit when designing a Java API.
Key Principles
Method signature — Prefer using objects over primitives, especially when argument ordering is important and unclear with primitives.
Static factory methods — Use static factory methods to create objects. These can return interfaces with hidden implementations.
Fluent API — Consider fluent APIs as alternatives to complex constructors for better readability.
Weakest possible types — Accept the weakest possible type to allow flexibility in usage.
Avoid checked exceptions — They leak implementation details and increase coupling. Favor unchecked exceptions or functional handling.
Prefer small interfaces — Split large interfaces into smaller, more focused ones.
No booleans in method signatures — Avoid methods like
save(true), which are unclear. Use enums or split into separate methods.Return empty collections / booleans — Never return
null— return empty collections orOptional/boolean instead.Use meaningful return types — Avoid returning raw data types when domain-specific types convey more meaning.
Defend API contracts — Return unmodifiable collections to prevent external mutation.