Api Design 2 Bloch
Api Design 2 Bloch
17-214 1
Administrivia
17-214 2
Review: libraries, frameworks both define APIs
API
public MyWidget extends JContainer {
Library
/ render component on first view and
resizing
protected void
paintComponent(Graphics g) {
// draw a red box on his
componentDimension d = getSize();
g.setColor(Color.red);
g.drawRect(0, 0, d.getWidth(),
d.getHeight()); }
}
your code
API
public MyWidget extends JContainer {
your code
17-214 3
Outline
17-214 4
Review: what’s an API?
17-214 5
Exponential growth in the power of APIs
This list is approximate and incomplete, but it tells a story
17-214 6
What the dramatic growth in APIs has done for us
17-214 7
Why is API design important?
17-214 8
Why is API design important to you?
17-214 9
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 evolve
• Appropriate to audience
17-214 10
Outline
17-214 11
The process of API design – 1-slide version
Not sequential; if you discover shortcomings, iterate!
17-214 12
Gather requirements – with a healthy degree of skepticism
17-214 13
Requirements gathering (2)
17-214 14
Choosing an abstraction (model)
17-214 15
Start with short spec – one page is ideal!
17-214 16
Sample Early API Draft
17-214 17
Write to the API, early and often
17-214 18
When you think you’re on the right track, then write a
prototype implementation
• Some of your client code will run; some won’t
• You will find “embarrassing” errors in your API
– Remember, they are obvious only in retrospect
– Fix them and move on
17-214 19
Then flesh out documentation so it’s usable by people
who didn’t help you write the API
• You’ll likely find more problems as you flesh out the docs
– Fix them
• Then you’ll have an artifact you can share more widely
• Do so, but be sure people know it’s subject to change
• If you’re lucky, you’ll get bug reports & feature requests
• Use the API feedback while you can!
– Read it all…
– But be selective: act only on the good feedback
17-214 20
Maintain realistic expectations
* Well, not equally – I said that back in 2004 because I thought it sounded
funny, and it stuck; actually you should decide which uses are most
important and favor them.
17-214 21
Issue tracking
17-214 22
Disclaimer – one size does not fit all
17-214 23
It’s Puzzler Time!
17-214 24
Puzzler: “The Name Game”
17-214 25
What Does It Print?
17-214 26
What Does It Print?
17-214 27
What Does It Print?
(a) 0
(b) 1
(c) 2
(d) None of the above
No programmer-defined constructor!
17-214 28
Another Look
17-214 29
How Do You Fix It?
17-214 30
The Moral
17-214 31
Java’s typographical naming conventions
17-214 32
Outline
17-214 33
Names Matter – API is a little language
Naming is perhaps the single most important factor in API usability
• Primary goals
– Client code should read like prose (“easy to read”)
– Client code should mean what it says (“hard to misread”)
– Client code should flow naturally (“easy to write”)
• To that end, names should:
– be largely self-explanatory
– leverage existing knowledge
– interact harmoniously with language and each other
17-214 34
How to choose names that are easy to read & write
17-214 35
Names drive development, for better or worse
17-214 36
Vocabulary consistency
17-214 38
Avoid abbreviations except where customary
17-214 39
Grammar is a part of naming too
17-214 40
Names should be regular – strive for symmetry
addRow removeRow
addColumn removeColumn
17-214 41
Don’t mislead your user
17-214 42
Don’t lie to your user outright
• Name method for what it does, not what you wish it did
• If you can’t bring yourself to do this, fix the method!
• Again, ignore this at your own peril
Skips over and discards n bytes of data from this input stream. The
skip method may, for a variety of reasons, end up skipping over some
smaller number of bytes, possibly 0. This may result from any of a
number of conditions; reaching end of file before n bytes have been
skipped is only one possibility. The actual number of bytes skipped is
returned…
17-214 43
Good naming takes time, but it’s worth it
17-214 44
Lecture summary
• APIs took off in the past thirty years, and gave us super-powers
• Good APIs are a blessing; bad ones, a curse
• Following an API design process greatly improves API quality
• Naming is critical to API usability
17-214 45
To be continued…
17-214 46
Puzzler: “Big Trouble”
System.out.println(total);
}
17-214 47
What Does It Print?
System.out.println(total);
}
17-214 49
What Does It Print?
(a) 0
(b) 500000
(c) 555000
(d) It varies
BigInteger is immutable!
17-214 50
Another Look
System.out.println(total);
}
17-214 51
How do you fix it?
System.out.println(total);
}
Prints 555000
17-214 52
The moral
17-214 53