artima网站对在Bjarne Stroustrup在2003进行了专访,摘录了一些有感触的内容。
1. 有责任维护类的不变性的函数应该成为类的接口The functions that are taking any responsibility for maintaining the invariant should be in the class
2. 如果一个类的成员可以是任何的值,那么他成为类的意义就不大(也许可以成为一个结构struct)If every data can have any value, then it doesn't make much sense to have a class.
3. 如果一个类有太多的set、get函数,那么意味着它不应该是一个类(也许可以成为一个结构struct). I particularly dislike classes with a lot of get and set functions.
4. 辅助函数并不是利用类继承的合法理由 I've seen the Date
problem solved by having a base class Date
with some operations on it and the data protected, with utility functions provided by deriving a new class and adding the utility functions
多接口和单实现
5. 有的时候多继承,纯粹是为了方便;你可以使用聚合,但是不方便。
6. 给问题建模尽可能的直接和简单,但是不能更简单了。you try to model the problem as directly and simply as possible, but no simpler
7. 资源获取就是初始化的含义就是把资源的获取和释放放在构造函数和析构函数中,So "resource acquisition is initialization" is a consequence of the view of hiding allocation and deallocation by dealing with it in constructors and destructors of classes
8. 设计工具不会给你更多的帮助,你应该开发一个小的系统,然后逐渐的升级为一个大的系统。Design tools don't give you much feedback, so therefore I tend towards the view that you should build a smaller system and grow it into a bigger one.
9. 建造函数库,然后使用它们I think people should mentally separate programming into building libraries and using them.
10. 丑陋的操作应该使用丑陋的语法An ugly operation should have an ugly syntactic form