How APIs made me write better code
Application Programming Interfaces - hence API, were introduced to us this week. It took a while to grasp what it meant and how they may be of use. The reason is (In my opinion) that it has two distinct meanings. One is the abstraction of creating an interface for a given object, which can be easily applied to reality. eg we interact with a car through the interface of the dashboard. It is a point where one type of thing can interact with another in a predefined and prescribed way.
Then there is the other meaning of API, the practical way or writing it in any given language/framework and what it means to be interacted with from either server side or client side. This distinction seems obvious now, but at first it was hard to reconcile learning how the code I wrote was related to the abstraction.
Now I cant get enough of APIs they just make sense, there is a pattern for writing code called the façade pattern, the idea of creating façades to your code much like you would for a building. Effectively each façade becomes a place of interaction with the outside world while it conceals the inner workings of the building. To continue the analogy the building may be a mammoth archiving warehouse and the façade a single door going in and another out. This might represent a data structure with a read/write function on the API. From the outside world all we can see are these two doors - entry and exit. Boxes of files go in and are later retrieved when needed.
You may also use a façade internally to divide up the workings of the warehouse. This could be seen as the various departments of a factory. In a car factory the paint department effectively has a two door entry/write and exit/read façade. To the other departments they don't care what's going on in the department only that if they push a car towards into it at a given time later it can be retrieved or sent back painted. This is basically an API for a sub program. The scope of the analogy can be shrunk down until you have a single method that does a single function, in effect the methods name is the API to the rest of the program and defines how it will be interacted with.
So why the fuss? How does this warrant a whole week of focus inside the boot camp and this corresponding blog post. Well its simple - to write an API is more work and adds an extra level of complexity to the code. However it is definitely worth it, WebApps use the HTTP application protocol and allow developers to define API façades that enable other programs to interact with theirs. The Google maps api is what allows many other webseverices to use Google maps with custom elements for their own service. Amazon heavily uses APIs internally allowing for interactivity between departments as well as external software. APIs make code more usable/easier to maintain and allow for unpredicted uses from the outside of the façade.
For myself the unpredictability of how a web API will get consumed is the exciting part. It gives the code an almost organic quality as its uses adapt and grow. In a way APIs and the façade pattern are simply bio-mimicry of how the natural/physical world works. Surfaces/skins are the interfaces between the internal and external in everything.












