歡迎您光臨本站 註冊首頁

你懷疑過嗎,面向對象編程是個騙局?

←手機掃碼閱讀     火星人 @ 2014-03-12 , reply:0
  

今天在網上看到網頁叫“Object Orientation Isa Hoax”——面向對象是一個騙局,標題很有煽動性(註:該網站上還有一個網頁叫Object Orientation Is Dead),好吧,打開看看上面有些 什麼,發現這個網頁是在收集一些關於“面向對象的反動言論”,沒想到的是,很多言論出自很多大師之口。比如:Alexander Stepanov和Bjarne Stroustrup。這些言論挺有意思的,所以,我摘兩段在下面:

第一段是Alexander Stepanov的(不要告訴我你不知道這個人,STL之父,關於他的故事,可以到這裡看看)。他N年前作過一段採訪,原文在這裡(我非常建議大家去讀一下這篇採訪,相當過癮),譯文在這裡(不過有地方把原意都譯反了,我重譯了一下),其中有一個問答被上述的那個面向對象反動言論的網頁收錄了:

Alexander Stepanov

Question:
I think STL and Generic Programming mark a definite departure from the common C++ programming style, which I find is almost completely derived from SmallTalk. Do you agree?

提問:
我認為STL和泛型編程標誌著非同一般的C++編程風格,而這種風格幾乎完全是從SmallTalk派生過來的。你同意嗎?

Answer:
Yes. STL is not object oriented. I think that object orientedness is almost as much of a hoax as Artificial Intelligence. I have yet to see an interesting piece of code that comes from these OO people. In a sense, I am unfair to AI: I learned a lot of stuff from the MIT AI Lab crowd, they have done some really fundamental work: Bill Gosper’s Hakmem is one of the best things for a programmer to read. AI might not have had a serious foundation, but it produced Gosper and Stallman (Emacs), Moses (Macsyma) and Sussman (Scheme, together with Guy Steele). I find OOP technically unsound. It attempts to decompose the world in terms of interfaces that vary on a single type. To deal with the real problems you need multisorted algebras – families of interfaces that span multiple types. I find OOP philosophically unsound. It claims that everything is an object. Even if it is true it is not very interesting – saying that everything is an object is saying nothing at all. I find OOP methodologically wrong. It starts with classes. It is as if mathematicians would start with axioms. You do not start with axioms – you start with proofs. Only when you have found a bunch of related proofs, can you come up with axioms. You end with axioms. The same thing is true in programming: you have to start with interesting algorithms. Only when you understand them well, can you come up with an interface that will let them work.

回答:
是的。STL不是面向對象的。我認為面向對象和人工智慧差不多,都是個騙局。我至今仍然沒有從那些OO編程的人那裡看到一丁點有意思的代碼。從某種意義上來說,我這麼說對人工智慧(AI)並不公平:因為我聽說過很多MIT(麻省理工大) AI實驗室里一幫人搞出來的東西,而且他們的確直正幹了一些基礎性的工作:Bill Gosper的Hakmem是程序員最好的讀物之一。AI或許沒有一個實實在在的基礎,但它造就了Gosper和Stallman(Emacs), Moses(Macsyma)和Sussman(Scheme, 和Guy Steele一起)。

  • 我發現OOP在技術上是荒謬的,它企圖把世界解構成不同的單一類型的介面,為了處理實際問題,你需要多種代數方法——橫跨多種類型的介面族;
  • 我發現OOP在哲學上是荒謬的,它聲稱一切都是對象。即使這是真的也不是很有趣——因為說一切都是對象跟什麼都沒說一樣;
  • 我發現OOP的方法論是錯誤的,它從類開始,就好像數學要從公理開始一樣。你不從公理開始,你就要從證明開始。直到你找到了一大堆相關證據后你才能歸納出公理,然後以公理結束。在程序設計方面存在著同樣的事實:你要從有趣的演算法開始。只有很好地理解了演算法,你才有可能提煉出介面以讓其工作。

<———>

下面,我們再來看C++的發明者Bjarne Stroustrup,在1998年IEEE採訪時的一段話(全篇見這裡),下面是其中的幾段話:(我的翻譯如下)

Bjarne Stroustrup

So what is OO? Certainly not every good program is object-oriented, and not every object-oriented program is good. If this were so, “object-oriented” would simply be a synonym for “good,” and the concept would be a vacuous buzzword of little help when you need to make practical decisions. I tend to equate OOP with heavy use of class hierarchies and virtual functions (called methods in some languages). This definition is historically accurate because class hierarchies and virtual functions together with their accompanying design philosophy were what distinguished Simula from the other languages of its time. In fact, it is this aspect of Simula’s legacy that Smalltalk has most heavily emphasized.

那麼,什麼是OO面向對象?當然,不會是所有的程序都是面向對象的,而且,也不是所有的面向對象程序就是好的。如果面向對象是好的,那麼“Object-Oriented”應該成為“Good”的同義詞,並且,OO概念只會成為一個假大空的口號,在你需要做出實際決定時只可能幫你那麼一丁點。我傾向於把OOP等價於大量使用繼承類和虛函數(某些語言的調用方法)。從歷史上來說,這個定義是精確的,因為,在那個時候,只有類的繼承和虛函數一起存在的設計哲學,才能把Simula和其它語言分別開來。事實上,Smalltalk相當地強調著這種Simula的遺留問題。

Defining OO as based on the use of class hierarchies and virtual functions is also practical in that it provides some guidance as to where OO is likely to be successful. You look for concepts that have a hierarchical ordering, for variants of a concept that can share an implementation, and for objects that can be manipulated through a common interface without being of exactly the same type. Given a few examples and a bit of experience, this can be the basis for a very powerful approach to design.

用繼承類和虛函數來定義OO在實際上可以讓很多OO指導性的東西更能成功一些。在解決問題時,尋找的那些有層級次序的對象,以應對不同對象也可以重用同一個實現,並且對象可以被某個共同的介面來操作而不需要完全相同的類型。在你了解了一些示例和擁有了一些經驗后,OO可以成為Design的一個強有力的基礎。

However, not every concept naturally and usefully fits into a hierarchy, not every relationship among concepts is hierarchical, and not every problem is best approached with a primary focus on objects. For example, some problems really are primarily algorithmic. Consequently, a general-purpose programming language should support a variety of ways of thinking and a variety of programming styles. This variety results from the diversity of problems to be solved and the many ways of solving them. C++ supports a variety of programming styles and is therefore more appropriately called a multiparadigm, rather than an object-oriented, language (assuming you need a fancy label).

然後,並不是每一個對象都自然地有效地適合繼承,並不是每一個對象間的關係都是繼承,也並不是每一個問題的最佳解決途徑需要主要地通過對象。例如,很多問題主要是演算法問題(譯註:如業務邏輯,數據流等)。我們知道,一個一般性的編程語言都應該有能力支持不同的思路和不同的編程風格。這樣,對於問題的多樣性,我們可以使用許許多多不同的的方法去解決他們,這就產生了很多的不同解法。C++支持編程風格的多樣性,因此,C++叫做“多範式  multi-paradigm”會更合適一些,而不是一個面向對象語言。

<———>

我個人在看過這些言論后,我先不管“面向對象是不是一個騙局”,不過從某種角度上來看的確是有些問題的,C++、OO、XML、SOA、網格計算等等諸如此類的東西的確被掛上了神聖的光壞。這些東西出來的時候總是只有一種讚美的聲音。無論好壞,只有一種聲音總是令人恐怖的,無論好壞,有不同的聲音總是好的,每當這個社會或是我們的IT界大張旗鼓地鼓吹或是信仰某些東西,卻沒有任何一點不同意見的時候,我就會感到一種莫名的恐慌。我知道,這是我們從小受到的那種“非黑即白”的價值觀教育所致,事物要麼全是好的,要麼全是不好的。其實任何事物都是有好有不好的,C++,敏捷開發,CMMi,OO,設計模式,重構,等等等等,他們都有好的也有不好的,關鍵看你怎麼來使用。這個世界只有適合不適合的東西,不會出現放之四海皆準的東西,也不可能出現一種可以解決所有問題的東西,如果有,那麼這種東西必然是一種宗教性質的用來洗腦的東西。

所以,每當在我身邊看到或聽到那些只有一種聲音有如“電視購物”或是“新聞聯播”之類的宣傳或是鼓動的時候,我就感到很一種莫名的反感…… 不多說了,還是交給大家來評價吧。我僅以此篇文章獻給那些OO-Oriented,Design Pattern-Oriented,Agile-Oriented,Process-Oriented,等等有著宗教信仰一般的人和事。

原文鏈接:http://coolshell.cn/articles/3036.html



[火星人 ] 你懷疑過嗎,面向對象編程是個騙局?已經有505次圍觀

http://coctec.com/news/soft/show-post-71558.html