Effective API Design
Effective API Design
*/-->
div.org-src-container {
font-size: 85%;
font-family: monospace;
}
Table of Contents
A well-written API can be a great asset to the organization that wrote it and to all that
use it. Given the importance of good API design, surprisingly little has been written on the
subject. In this talk (recorded at Javapolis), Java library designer Joshua Bloch teaches
how to design good APIs, with many examples of what good and bad APIs look like.
From: Infoq.com
1 Foreword
- Why is API Design Important?
- APIs can be among a company's greates assets.
- Customers invest heavily: buying, writing, learning.
- Cost to stop using an API can be prohibitive
- Successful public APIs capture customers.
- Can also be among company's greatest liabilities
- bad APIs result in unending stream of support calls.
- Public APIs are forever
- One chance to get it right.
- Why is API Design Important to You?
- If you program, you are an API Designer
- Good code is modular - each module has an API
- Usefull modules tend to get reused
- Once module has users, can't change API at will
- You can add new ones,
- But you should not delete or modify existing ones at will.
- Once module has users, can't change API at will
- Thinking in tersm of API improves code quality.
- Characteristics of a Good API
- Easy to learn.
- Easy to use, Hard to misuse
- Easy to read and maintian code that uses it
- Sufficiently powerful to satisfy requirements.
- Easy to extend
- Appropriate to audience.
2 Process of API Design
- Gather requirements - with a Healthy Degree of Skepticism
Often you'll get proposed solutions instead, but better solutions may existing, and your job
is to extract true requirements from the use-cases.And, keep in mind that it can be easier and more rewarding to build somthing more general.
- Make spec short: single page is okey
- Write to Your API Early and Often
- Writing to SPI is Even More Important
- Service Provider Interface (SPI)
- Plugin-in interface enabling multiple implementations
- Example: Java Cryptography Extension (JCE).
- Write multiple plugins before release
- The Rule of threes.
- Maintain Realistic Expectations
3 General Principles
- API should do one thing and do it well
- Functionality should be easy to explain:
- If it's hard to name, that generaty a bad sign.
- Good names drive development.
- Be amenable to spliting and merging modules.
- API should be as mall as possible, but no smaller
- API should satisfy its requirements
- When in doubt, leave it out!
- You can always add, but you can never remove!
- Conceptual weight more important than bulk.
- Look for a good power-to-weight ratio
- Implementation should not impact API
- Minimize Accessibility of Everything
- Make classes and members as private as possible.
- Public classes should have no public fields.
- Maximizes information hiding
- Allow modules to be used, understood, built, tested, and debugged independently.
- Names Matter – API is a Little Language
- Names shoudl be Largely Self-Explanatory
- Be consistent – same word meas same thing
- Be regular – strive for symmetry
- Code should read like prose
- Documentation Matters
- Document Religiosly:
All public APIs should have Documentation:- Classes: what an instance represents
- Method: contract between method and its client
- Preconditions, postconditions, side-effects.
- Parameter: indicat units, form, ownership
- Document state space very carefully
- Consider Performace Consequences of API Design Decisions
- bad decisions can limit performance.
- API Must Coexist Peacefully with Platform
4 Classes Deisgn
- Minimize Mutability
Classes should be immutable unless there's a good reason to do otherwise. If mutable,
keep state-space small, well-defined. - Subcalss only where it makes sense
Subclassing implies subsitituability,- Public classses should not subclasses other public classes.
- Design and Document for Inheritance or Else Prohibit it!
5 Method Design
- Don't make the Client Do Anything the Module Could Do
- Don't Violate Principle of Least Astonishment
- Fail Fast – Report Errors as Sonn as Possible After They Occur
- Provide Programmatic Access to All Data Available in String Form
- Overload With Care
if you must provide ambiguous overloadings, ensure same behavior for same arguments.
- Use Appropriate Parameter and Return Types
- Favor interface types over classes for input.
- Use most specifi possible input parameter type
Moves error from runtime to compile time.
- Don't use string if a better type exists
- Do't use floating point for monetary values:
Binary floating point causes inexact result.
- Use double (64 bits) rather than float(32 bits):
Precision loss is real, performance loss negligible.
- Use Consistent parameter Ordering Across Methods
#include <string.h>
char *strcpy(char *dest, char *src);
void bcopy (void* src, void* dst, int n); // XXX: Bad example!
- Avoid Long Parameter Lists
- Three or fewer parameters is ideal
- Two techniques for shortening parameter lists:
- Break up method
- Create helper class to hold parameters
- Avoid Return Values that Demand Exceptional Processing
6 Exceptions Design
- Throw Exceptions to Indicate Exceptional Conditions
- Don't force client to use exceptions for control flow.
- Don't fail silently
- Favor Unchecked Exceptions
- Include Failure-Capture Information in Exceptions
7 Refactoring API Design
Just some examples….
(转载请注明出处,使用许可:署名-非商业性使用-相同方式共享 3.0 中国大陆许可协议 。)
Effective API Design的更多相关文章
- API Design
REST API Design Guidelines V 1.0.201208 Draft 5 Last Updated: 08/31/2012 1 简介 本文档旨在规范REST API的 ...
- Principles of good RESTful API Design 好的 RESTful API 设计
UPDATE: This post has been expanded upon and converted into an eBook. Good API design is hard! An AP ...
- RESTful API Design With NodeJS & Restify
http://code.tutsplus.com/tutorials/restful-api-design-with-nodejs-restify--cms-22637 The RESTful API ...
- API Design Principles -- QT Project
[the original link] One of Qt’s most reputed merits is its consistent, easy-to-learn, powerfulAPI. T ...
- Atitit.index manager api design 索引管理api设计
Atitit.index manager api design 索引管理api设计 1. kw 1 1.1. 索引类型 unique,normal,fulltxt 1 1.2. 聚集索引(cluste ...
- Atitit.index manager api design 索引管理api设计
Atitit.index manager api design 索引管理api设计 1. kw1 1.1. 索引类型 unique,normal,fulltxt1 1.2. 聚集索引(clustere ...
- RESTful API Design: 13 Best Practices to Make Your Users Happy
RESTful API Design: 13 Best Practices to Make Your Users Happy First step to the RESTful way: make s ...
- Web API design
Web API design 28 minutes to read Most modern web applications expose APIs that clients can use to i ...
- Google API Design Guide (谷歌API设计指南)中文版
面向资源的设计 这份设计指南的目标是帮助开发人员设计简单.一致.易用的网络API.同时,它也有助于收敛基于socket的API和(注:原文是with,这里翻译为“和”)基于HTTP的REST API. ...
随机推荐
- echarts分组柱状图的前后台处理 带平均线显示
原生的echarts使用: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /&g ...
- PostgreSQL 修改字段类型从int到bigint
由于现在pg的版本,修改int到bigint仍然需要rewrite表,会导致表阻塞,无法使用.但可以考虑其他方式来做.此问题是排查现网pg使用序列的情况时遇到的. 由于int的最大值只有21亿左右,而 ...
- 简单易懂的GBDT
转https://www.cnblogs.com/liuyu124/p/7333080.html 梯度提升决策树(Gradient Boosting Decision Tree,GBDT)算法是近年来 ...
- Linux运维六:用户管理及用户权限设置
Linux 系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系统.用户的账号一方面可以帮助系统管理员对使用系统的用户进行 ...
- Docker应用四:搭建docker镜像仓库(包括自生成https证书、登陆认证)
利用docker官网提供的registry镜像创建私有仓库 一.首先从docker官网拉取registry镜像: docker pull registry 二.然后运行该镜像: docker run ...
- 新建springboot项目启动出错 Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
错误信息入下: 2018-06-23 01:48:05.275 INFO 7104 --- [ main] o.apache.catalina.core.StandardService : Stopp ...
- ELASTIC SEARCH 安装
elastic search 2017年3月18日 安装&使用 环境 表 1 环境信息 Centos cat /etc/issue CentOS release 6.8 (Final) cat ...
- java学习第01天(搭建环境配置)
搭建环境配置分为3个步骤 1.下载并安装 jdk使用1.8.0_151版本,下载地址(包含32位和64位):链接:https://pan.baidu.com/s/1qZau8oO 密码:0t5o 2. ...
- 浅谈欧几里得算法求最大公约数(GCD)的原理及简单应用
一.欧几里得算法及其证明 1.定义: 欧几里得算法又称辗转相除法,用于求两数的最大公约数,计算公式为GCD(a,b)=GCD(b,a%b): 2.证明: 设x为两整数a,b(a>=b)的最大公约 ...
- 【leetcode 简单】 第九十五题 数字转换为十六进制数
给定一个整数,编写一个算法将这个数转换为十六进制数.对于负整数,我们通常使用 补码运算 方法. 注意: 十六进制中所有字母(a-f)都必须是小写. 十六进制字符串中不能包含多余的前导零.如果要转化的数 ...