masterminds of programming 翻译 (二)

Why did you choose to support multiple paradigms?

为什么您选择支持不同的编程范式?

 

Bjarne: Because a combination of programming styles often leads to the best code, where “best” means code that most directly expresses the design, runs faster, is most maintainable, etc. When people challenge that statement, they usually do so by either defining their

favorite programming style to include every useful construct (e.g., “generic programming is simply a form of OO”) or excluding application areas (e.g., “everybody has a 1GHz, 1GB machine”).

Bjarne:因为一个组合式的编程方式往往可以产生最好的代码,这里最好的意思是最直接表达设计、运行速度快、最易维护的代码。当人们质疑这种说法时,他们通常通过定义他们自己喜欢的编程方式来包含各种有用构造(例如,泛型编程就只是一个面向对象编程的一个形式)或排除一些应用领域(例如,每个人都有一个1GHz主频,1GB内存的电脑)去达到这个最好

 

Java focuses solely on object-oriented programming. Does this make Java code more complex in some cases where C++ can instead take advantage of generic programming?

Java只专注于面向对象编程。这会使java代码在有些C++可以用泛型编程取巧的情景中显得更复杂吗?

 

Bjarne: Well, the Java designers—and probably the Java marketers even more so—emphasized OO to the point where it became absurd. When Java first appeared, claiming purity and simplicity, I predicted that if it succeeded Java would grow significantly in size and complexity. It did.

恩,java的设计者,很可能还有java的受用人群过于强调面向对象编程到了荒谬的地步。当java刚问世的时候,主张纯粹和简单性,我当时就预言如果这门语言能成功,java会在大小和复杂度方面增长迅速。它确实如此。

 

Bjarne:For example, using casts to convert from Object when getting a value out of a container (e.g., (Apple)c.get(i)) is an absurd consequence of not being able to state what type the objects in the container is supposed have. It’s verbose and inefficient. Now Java has generics, so it’s just a bit slow. Other examples of increased language complexity (helping the programmer) are enumerations, reflection, and inner classes.

例如,对一个从容器中取出的对象进行类型转换(例如,(Apple)c.get(i))就是一个荒谬的结果,因为我们无从知晓容器中原本包含的对象的类型是什么。这既啰嗦效率又低。现在java引入了泛型思想,所以它更慢了。还有一些增加语言复杂度(帮助程序员)的例子是枚举类、反射机制和内联类。

 

The simple fact is that complexity will emerge somewhere, if not in the language definition, then in thousands of applications and libraries. Similarly, Java’s obsession with putting every algorithm (operation) into a class leads to absurdities like classes with no data consisting exclusively of static functions. There are reasons why math uses f(x) and f(x,y) rather than x.f( ), x.f(y), and (x,y).f( )—the latter is an attempt to express the idea of a “truly object-oriented method” of two arguments and to avoid the inherent asymmetry of x.f(y).

一个简单的事实是复杂度总归要出现的,如果不是在语言的定义中,就是在成千上万的应用程序和库中。类似的,java对于需要把每一个算法都放入类的过度导致了一些荒谬的情况发生,如一个没有任何数据成员而只是为了包含静态函数的类。数学上使用f(x)f(x,y)而不是使用x.f(),x.f(y)(x,y).f()是有道理的:后者试图在表达一个真正含有2个参数的面向对象方法的同时尽量避免x.f(y)天生的非对称性。

 

C++ addresses many of the logical as well as the notational problems with object orientation through a combination of data abstraction and generic programming techniques. A classical example is vector<T> where T can be any type that can be copied—including builtin types, pointers to OO hierarchies, and user-defined types, such as strings and complex numbers. This is all done without adding runtime overheads, placing restrictions on data layouts, or having special rules for standard library components. Another example that does not fit the classical single-dispatch hierarchy model of OO is an operation that requires access to two classes, such as operator*(Matrix,Vector), which is not naturally a “method” of either class.

C++通过数据抽象和泛型编程的技术组合处理面向对象所带来的逻辑和标注问题。一个经典的例子是vector<T>,这里T可以是任何可以复制的类型-包括内置类型,指向面向对象各层的指针、以及用户自定义的类型,如字符串类和复数类。所有这些都在没有增加运行时负载、对数据存储方式进行限制或者对标准库组建提出特别的规则下完成的。另一个不适合传统面向对象模型中单分派例子是一个需要用到两个类的操作,比如operator*(Matrix,Vector), 这个操作一般来说不属于任何其中一个类。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值