After years of working in software development, I started to realize that API and contract are very essential to understand. So here, I’ll explain what I understand and how it impacts my day-to-day work.

Years ago

When I was still in university, I learned how to create RESTFul API. Since then, my understanding of API is just about REST API. Every time people talk about API, I automatically think about REST API.

Until one day, I attend a live coding interview. Like other live coding interviews, I need to write the solution for the given problem statement and explain it to the interviewer. In the middle of the interview, the interviewer asked me “Can you explain what the API does?”. I got confused about which API he was referring to because there is no REST API involved in the solution. After I clarified what API he asked for, then I understood that he was referring to a function that I used from a built-in library.

After the interview, my understanding of API changed.

What is API?

API stands for Application Programming Interface. The main word is interface. Interface is the contract between client and implementation. The contract define how to interact with the implementation. For example, what parameter can be supplied to the function, what is the return type, etc.

Let’s take a look this example

func calculate(int a, int b) int

From that snippet, the client, the one that gonna use the function, know that the function name is calculate and it accept two parameters with type integer and return one output with type integer. The client doesn’t really need to know the implementation detail.

Simple, right?

Why Design By Contract is Important?

Design by contract is an approach to design a system based on contract that agreed by the parties. Each party should follow the contract to make the system work as expected. If there any violation of the contract, the parties know that the system will behave differently. This is make design by contract is important.

For example, in software development, establishing a contract between backend and frontend will help backend system know what to implement and frontend system know how to consume the API. If the contract in backend without notifiying the frontend, it might lead to failure in the frontend.

In beautiful setup, any modification to the contract should be visible to all party that using it. For example, if you use OpenAPI or ProtoBuf to define the API, all the modification should be tracked there. To extend it, backend and frontend should generate the code base on OpenAPI or ProtoBuf.

Cappy Hoding! 🖖🏾