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. ...
随机推荐
- 【Asp.net入门4-01】基本开发工具
- Jenkins和Gitblit集成实现提交后自动构建
Gitblit是一个纯Java基于git的解决方案.它属于Apache Allura等伪造软件之一,它将票务系统与web ui结合在一起.我经历了一个设置过程,我想我可以帮助新用户避免痛点. 使用场景 ...
- Linux运维五:定时任务crond服务
一.crond简介 crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动cro ...
- python基础之面向对象01
什么是面向对象? 所谓面向对象就是将那些具有共同特质的事物组成一个类(Class),在类中为这些特质定义一个个方法(函数). 具体一点,比如说英雄联盟.在英雄联盟中我们可以把所有的英雄划分为一个英雄类 ...
- Selenium geckodriver异常
sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...
- Java基础-包(package)的声明与访问
Java基础-包(package)的声明与访问 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.包的概念 Java中的包,其实就是我们电脑系统中的文件夹,包里存放的是程序员生成的 ...
- 【操作记录】Asp.Net Core 的一些基本操作或属性
用于记录在项目中使用到的方法.属性.操作,持续更新中 .net core 开源地址 图片上传: public async Task<IActionResult> Upload([FromS ...
- [转载]mysql下载安装
转自https://www.cnblogs.com/tyhj-zxp/p/6693046.html 下载 打开:https://www.mysql.com/downloads/ 1.点击该项:
- Linux下安装PHP的lua扩展库
一.安装Lua 5.3.4 下载 http://www.lua.org/ftp/lua-.tar.gz tar xvf lua-.tar.gz cd lua- 重要:进入解压缩后的路径 cd .../ ...
- c++虚函数&重写
虚函数是C++中实现多态的一种方法,父类A的一个函数声明为虚函数,在子类B中覆盖定义之后,当在调用的时候使用A*a=new B(),此时调用对应的那个虚函数的名字,则会执行B中的函数.当父类中没有定义 ...