This is the 35th Step towards gaining the Programming Enlightenment series. If you didn’t learn the 34rd Step, read it.
What is an API?
It stands for Application Programming Interface.
It is a set of subroutine definitions, protocols, and tools for building application software.
In general terms, it is a set of clearly defined methods of communication between various software components.
What is the Golden Rule of API design?
APIs are dynamic, they might change in future, so we have to be careful how it might break the client’s code.
Equally it’s also true, how users of our API will affect us. Like they override one of our internal methods by subclassing it and user changes what it does.
The easiest way to solve this problem is lock down of the API.
- In Java we might be tempted to make most of our classes and methods final.
- Present the API through singleton or using static factory methods.
This may seem to solve the problem for us, but it doesn’t really.
The GOLDEN RULE:
It's not enough to write tests for an API you develop; You have to write unit tests for code that uses your API. When you do, you learn first-hand the hurdles that your users will have to overcome when they try to test their code independently.
This will not only give us some indication how developers would use the API and give us an insight how we should be designing the API.
TL;DR If you are going to design an API, think how the client are going to consume it.
Go to 34rd Step
Go to the 36th Step.
References:
- 97 things Every Programmer Should Know ~ Git Book
- 97 Things Every Programmer Should Know ~ Paperback
- What is an API? ~ Medium Blog
- What is an API? Youtube video