Make Interfaces Easy to Use Correctly

I was perusing Scott Meyers's Articles and Interviews, and I noticed one with an interesting title: "Make Interfaces Easy to Use Correctly and Hard to Use Incorrectly".

This article comes from 97 Things Every Programmer Should Know, a collection of rules and advice proposed by the wider programming community. You can find a list of the rules selected for publication here.

Meyers points out that interface specification is one of the most common tasks we take on as a programmer:

One of the most common tasks in software development is interface specification. Interfaces occur at the highest level of abstraction (user interfaces), at the lowest (function interfaces), and at levels in between (class interfaces, library interfaces, etc.). Regardless of whether you work with end users to specify how they'll interact with a system, collaborate with developers to specify an API, or declare functions private to a class, interface design is an important part of your job. If you do it well, your interfaces will be a pleasure to use and will boost others' productivity. If you do it poorly, your interfaces will be a source of frustration and errors.

We should all take a moment to read this short article and reflect on whether we are designing good interfaces. Regardless of your answer - let's put more effort into our interfaces and do an even better job.

Read "Make Interfaces Easy to Use Correctly and Hard to Use Incorrectly" here.

My Highlights

One of the most common tasks in software development is interface specification. Interfaces occur at the highest level of abstraction (user interfaces), at the lowest (function interfaces), and at levels in between (class interfaces, library interfaces, etc.). Regardless of whether you work with end users to specify how they'll interact with a system, collaborate with developers to specify an API, or declare functions private to a class, interface design is an important part of your job. If you do it well, your interfaces will be a pleasure to use and will boost others' productivity. If you do it poorly, your interfaces will be a source of frustration and errors.

Think about how you want people to use your APIs before you begin implementing them and building on top of them:

Write calls to an API before the functions have been declared. Walk through common use cases and specify how you want the interface to behave. What do you want to be able to click on? What do you want to be able to pass? Easy to use interfaces seem natural, because they let you do what you want to do. You're more likely to come up with such interfaces if you develop them from a user's point of view. (This perspective is one of the strengths of test-first programming.)

Meyers's note on making interfaces harder to use incorrectly:

Making interfaces hard to use incorrectly requires two things. First, you must anticipate errors users might make and find ways to prevent them. Second, you must observe how an interface is misused during early release and modify the interface — yes, modify the interface! — to prevent such errors. The best way to prevent incorrect use is to make such use impossible. If users keep wanting to undo an irrevocable action, try to make the action revocable. If they keep passing the wrong value to an API, do your best to modify the API to take the values that users want to pass.