Chapter 19. Convenience Is Not an -ility
MUCH HAS BEEN SAID about the importance and challenges of designing good APIs. Itâs difficult to get right the first time and itâs even more difficult to change laterâsort of like raising children. Most experienced programmers have learned that a good API follows a consistent level of abstraction, exhibits consistency and symmetry, and forms the vocabulary for an expressive language. Alas, being aware of the guiding principles does not automatically translate into appropriate behavior. Eating sweets is bad for you.
Instead of preaching from on high, I want to pick on a particular API design âstrategy,â one that I encounter time and again: the argument of convenience. It typically begins with one of the following âinsightsâ:
I donât want other classes to have to make two separate calls to do this one thing.
Why should I make another method if itâs almost the same as this method? Iâll just add a simple
switch
.See, itâs very easy: if the second string parameter ends with â.txtâ, the method automatically assumes that the first parameter is a filename, so I really donât need two methods.
While well intended, such arguments are prone to decrease the readability of code using the API. A method invocation like:
parser.processNodes(text, false);
is virtually meaningless without knowing the implementation or at least ...
Get 97 Things Every 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.