Make Interfaces Easy to Use Correctly and Hard to Use Incorrectly

Make Interfaces Easy to Use Correctly and Hard to Use Incorrectly

Scott Meyers

ONE OF THE MOST COMMON TASKS in software development is interface specification. Interfaces occur at the highest level of abstraction (user inter- faces), at the lowest (function interfaces), and at levels in between (class inter- faces, library interfaces, etc.). Regardless of whether you work with end users to specify how they’ll interact with a system, collaborate with developers to specify an API, or declare functions private to a class, interface design is an important part of your job. If you do it well, your interfaces will be a pleasure to use and will boost others’ productivity. If you do it poorly, your interfaces will be a source of frustration and errors.

Good interfaces are:

Easy to use correctly

People using a well-designed interface almost always use the interface cor- rectly, because that’s the path of least resistance. In a GUI, they almost always click on the right icon, button, or menu entry, because it’s the obvi- ous and easy thing to do. In an API, they almost always pass the correct parameters with the correct values, because that’s what’s most natural. With interfaces that are easy to use correctly, things just work.

Hard to use incorrectly

Good interfaces anticipate mistakes people might make, and make them difficult—ideally, impossible—to commit. A GUI might disable or remove commands that make no sense in the current context, for example, or an API might eliminate argument-ordering problems by allowing parameters to be passed in any order.

??110

97 Things Every Programmer Should Know

?

???????????????A good way to design interfaces that are easy to use correctly is to exercise them before they exist. Mock up a GUI—possibly on a whiteboard or using index cards on a table—and play with it before any underlying code has been created. Write calls to an API before the functions have been declared. Walk through common use cases and specify how you want the interface to behave. What do you want to be able to click on? What do you want to be able to pass? Easy-to-use interfaces seem natural, because they let you do what you want to do. You’re more likely to come up with such interfaces if you develop them from a user’s point of view. (This perspective is one of the strengths of test-first programming.)

Making interfaces hard to use incorrectly requires two things. First, you must anticipate errors users might make and find ways to prevent them. Second, you must observe how an interface is misused during early release and modify the interface—yes, modify the interface!—to prevent such errors. The best way to prevent incorrect use is to make such use impossible. If users keep wanting to undo an irrevocable action, try to make the action revocable. If they keep passing the wrong value to an API, do your best to modify the API to take the values that users want to pass.

Above all, remember that interfaces exist for the convenience of their users, not their implementers.

时间: 2024-10-07 23:23:29

Make Interfaces Easy to Use Correctly and Hard to Use Incorrectly的相关文章

effective c++ 条款18 make interface easy to use correctly and hard to use incorrectly

举一个容易犯错的例子 class Date { private: int month; int day; int year; public: Date(int month,int day,int year) { this->month = month; ... } } //wrong example Date date(30,3,1995);//should be 3,30 Date date(2,30,1995);//should be 3,30 使用类型可避免这个问题 class Month

条款18:让接口容易被正确使用,不易被误用(Make interface easy to use correctly and hard to use incorrectly)

NOTE : 1.好的接口容易被正确使用,不容易被误用.应该让所有接口努力达成这些性质. 2.“促进正确使用”的办法包括接口的一致性,以及内置类型的行为兼容. 3.“阻止误用”的办法包括建立新类型/限制类型上的操作,束缚对象值,以及消除客户的资源管理责任. 4.tr1::shared_ptr 支持定制型删除器(custom deleter).这可以防范DLL问题,可以被用来自动解除互斥锁(mutexs)等等.

Item 18:让接口容易被正确使用,不易被误用 Effective C++笔记

Item 18: Make interfaces easy to use correctly and hard to use incorrectly. "让接口容易被正确使用,不易被误用",这也是面向对象设计中的重要概念,好的接口在工程实践中尤其重要. 在使用优秀的第三方组件时,常常能够切身感受到好的接口原来可以这么方便,甚至不需记住它的名字和参数就能正确地调用. 反观自己写的API,常常会有人三番五次地问这个参数怎么设置,真是失败.人非圣贤孰能无过,只能在这种痛苦的驱动下努力的重构和

C++学习书籍推荐《Effective C++ 第三版(英文)》下载

百度云及其他网盘下载地址:点我 作者简介 Scott Meyers is one of the world's foremost authorities on C++, providing training and consulting services to clients worldwide. He is the author of the best-selling Effective C++ series of books (Effective C++, More Effective C+

ARTS Week 17

Feb 17, 2020 ~ Feb 23, 2020 Algorithm Problem 205.Isomorphic Strings(同构字符串) 题目链接 题目描述:给定两个字符串 s 和 t,判断是否可以通过字符替换从 s 得到 t.顺序不可以改变,同时不允许两个字符映射到同一个字符,但允许一个字符映射到它自身.举例如下: Input: s = "egg", t = "add" Output: true Input: s = "foo",

[转载] Conv Nets: A Modular Perspective

原文地址:http://colah.github.io/posts/2014-07-Conv-Nets-Modular/ Conv Nets: A Modular Perspective Posted on July 8, 2014 neural networks, deep learning, convolutional neural networks, modular neural networks Introduction In the last few years, deep neura

21/CLASS - C Language Style for Scalability——ZeroMQ官网建议的C语言命令规范

http://rfc.zeromq.org/spec:21 Table of Contents License Change Process Language Goals Quality Aspirations Common Problems General Style Definitions Language Naming Project Style Project Focus Project Name General Layout Dependencies Project Header Fi

The 10 Most Important Security Controls Missing in JavaEE--reference

JavaEE has some excellent built-in security mechanisms, but they don’t come close to covering all the threats that your applications will face.  Many common attacks like Cross-Site Scripting (XSS), SQL Injection, Cross-Site Request Forgery (CSRF), an

基于weka的文本分类实现

weka介绍 参见 1)百度百科:http://baike.baidu.com/link?url=V9GKiFxiAoFkaUvPULJ7gK_xoEDnSfUNR1woed0YTmo20Wjo0wYo7uff4mq_wg3WzKhTZx4Ok0JFgtiYY19U4q 2)weka官网: http://www.cs.waikato.ac.nz/ml/weka/ 简单文本分类实现: 此处文本为已处理好的文本向量空间模型,关于文本特征提取主要是基于TF-IDF算法对已分词文档进行特征抽取,然后基于