Bjarne Stroustrup大師談

artima網站對在Bjarne Stroustrup在2003進行了專訪,摘錄了一些有感觸的內容。

1.        有責任維護類的不變性的函數應該成爲類的接口The functions that are taking any responsibility for maintaining the invariant should be in the class

 

2.        如果一個類的成員可以是任何的值,那麼他成爲類的意義就不大(也許可以成爲一個結構structIf every data can have any value, then it doesn't make much sense to have a class.

 

3.        如果一個類有太多的setget函數,那麼意味着它不應該是一個類(也許可以成爲一個結構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

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章