今天在网上看到网页叫“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++编程风格,而一般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,等等有着宗教信仰一般的人和事。