How To Design A Good API and Why It Matters
How To Design A Good API and Why It Matters
Joshua Bloch
Principal Software Engineer
2 _
How to Design a Good API and Why it Matters
Why is API Design Important to You?
3 _
How to Design a Good API and Why it Matters
Characteristics of a Good API
• Easy to learn
• Easy to use, even without documentation
• Hard to misuse
• Easy to read and maintain code that uses it
• Sufficiently powerful to satisfy requirements
• Easy to extend
• Appropriate to audience
4 _
How to Design a Good API and Why it Matters
Outline
5 _
How to Design a Good API and Why it Matters
I. The Process of API Design
6 _
How to Design a Good API and Why it Matters
Gather Requirements–with a Healthy
Degree of Skepticism
Good
7 _
How to Design a Good API and Why it Matters
Start with Short Spec–1 Page is Ideal
Bad
10 _How to Design a Good API and Why it Matters
Maintain Realistic Expectations
12 _
How to Design a Good API and Why it Matters
API Should Do One Thing and Do it Well
14 _
How to Design a Good API and Why it Matters
Implementation Should Not Impact API
• Implementation details
_ Confuse users
_ Inhibit freedom to change implementation
• Be aware of what is an implementation detail
_ Do not overspecify the behavior of methods
_ For example: do not specify hash functions
_ All tuning parameters are suspect
• Don't let implementation details “leak” into API
_ On-disk and on-the-wire formats, exceptions
15 _
How to Design a Good API and Why it Matters
Minimize Accessibility of Everything
18 _
How to Design a Good API and Why it Matters
Document Religiously
19 _
How to Design a Good API and Why it Matters
Consider Performance Consequences of
API Design Decisions
20 _
How to Design a Good API and Why it Matters
Effects of API Design Decisions on
Performance are Real and Permanent
21 _
How to Design a Good API and Why it Matters
API Must Coexist Peacefully with Platform
• Do what is customary
_ Obey standard naming conventions
_ Avoid obsolete parameter and return types
_ Mimic patterns in core APIs and language
• Take advantage of API-friendly features
_ Generics, varargs, enums, default arguments
• Know and avoid API traps and pitfalls
_ Finalizers, public static final arrays
23 _
How to Design a Good API and Why it Matters
Minimize Mutability
25 _
How to Design a Good API and Why it Matters
Design and Document for Inheritance
or Else Prohibit it
26 _
How to Design a Good API and Why it Matters
IV. Method Design
27 _
How to Design a Good API and Why it Matters
Don't Make the Client Do Anything the
Module Could Do
28 _
How to Design a Good API and Why it Matters
Don't Violate the Principle of Least
Astonishment
30 _
How to Design a Good API and Why it Matters
Provide Programmatic Access to All
Data Available in String Form
31 _
How to Design a Good API and Why it Matters
Overload With Care
32 _
How to Design a Good API and Why it Matters
Use Appropriate Parameter and Return Types
34 _
How to Design a Good API and Why it Matters
Avoid Long Parameter Lists
35 _
How to Design a Good API and Why it Matters
Avoid Return Values that Demand
Exceptional Processing
package java.awt.image;
public interface BufferedImageOp {
// Returns the rendering hints for this operation,
// or null if no hints have been set.
public RenderingHints getRenderingHints();
}
36 _
How to Design a Good API and Why it Matters
V. Exception Design
37 _
How to Design a Good API and Why it Matters
Throw Exceptions to Indicate
Exceptional Conditions
38 _
How to Design a Good API and Why it Matters
Favor Unchecked Exceptions
39 _
How to Design a Good API and Why it Matters
Include Failure-Capture Information in
Exceptions
41 _
How to Design a Good API and Why it Matters
1. Sublist Operations in Vector
43 _
How to Design a Good API and Why it Matters
2. Thread-Local Variables
44 _
How to Design a Good API and Why it Matters
Thread-Local Variables Refactored (1)
47 _
How to Design a Good API and Why it Matters
Shameless Self-Promotion
48 _
How to Design a Good API and Why it Matters
How to Design a Good
API and Why it Matters
Joshua Bloch
Principal Software Engineer