Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

API Design Best practices from Google Senior engineer

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Sharing from Joshua Bloch, a senior engineer at Google: best practices for API design

Why is API design so important? API is the most important asset of a company.

Why is the design of API so important to programmers?

Once API is released, for compatibility reasons, it cannot be easily modified at will, such as deleting parameters.

From the perspective of API's God-designer, it helps to improve the quality of the code.

A good API should meet the following criteria:

Easy to learn and use, even self-describing, allows beginners to get started quickly without documentation. It is not easy to cause misunderstanding. Follow-up maintainers are easy to understand and meet the open-close principle-it can be easily extended.

How to design a good API

First of all, we should start from collecting requirements. Note that it takes into account the complexity of the API implementation.

As a first step, first give the requirements specification document, which can be done on one page:

Don't expect your API to satisfy everyone. And don't expect it to make no mistakes after it's released-that's impossible.

API should also follow a single responsibility: if you find it difficult to name it based on what your API implements, it's a bad sign that you're doing too much in your API-try to split it into multiple API.

Information encapsulation-public classes try to avoid exposing public fields and maximize information hiding

The Art of API naming-the name of API is also a language.

The relationship between API and document

Rational use of inheritance and subclasses, do not abuse the principle of Richter substitution

Using the fail fast strategy, throw the error message as early as possible:

API data should be allowed to be accessed by users through strings.

Use heavy load with caution

Select the appropriate API parameter and return type

The order of parameters in API is also very particular.

Avoid lengthy parameter lists. If there are more than 3 parameters, users need to read the documentation before they can consume.

Try to return parameters that do not require exception handling by the caller, such as an empty array or collection, rather than null

Best practices related to exception handling in API design

Best practices for API refactoring

Best practices related to API design and Thread-local

For more original Jerry articles, please follow the official account "Wang Zixi":

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 278

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report