The Ten Commandments of Egoless Programming

无我编程的十诫

  1. Understand and accept that you will make mistakes. The point is to find them early before they make it into production. Fortunately, except for the few of us developing rocket guidance software at JPL, mistakes are rarely fatal in our industry. We can, and should, learn, laugh, and move on.

    理解并接受你会犯错误。关键是要在错误进入生产之前尽早发现它们。幸运的是,除了我们在 JPL 开发火箭制导软件的少数人之外,错误在我们的行业中很少是致命的。我们可以,也应该,学习、欢笑并继续前进。

  2. You are not your code. Remember that the entire point of a review is to find problems, and problems will be found. Don’t take it personally when one is uncovered. (Allspaw note – related: see below, number #10, and the points Theo made above.)

    你不是你的代码。请记住,审查的整个目的就是发现问题,而问题会被发现。当发现问题时,不要把它当作个人问题。(Allspaw 注 – 相关内容:见下文,第 #10 条,以及 Theo 上面提到的要点。)

  3. No matter how much “karate” you know, someone else will always know more. Such an individual can teach you some new moves if you ask. Seek and accept input from others, especially when you think it’s not needed.

    无论你知道多少“空手道”,总会有人知道更多。如果你请求,这样的人可以教你一些新招式。寻求并接受他人的意见,尤其是在你认为不需要的时候。

  4. Don’t rewrite code without consultation. There’s a fine line between “fixing code” and “rewriting code.” Know the difference, and pursue stylistic changes within the framework of a code review, not as a lone enforcer.

    在没有咨询的情况下不要重写代码。“修复代码”和“重写代码”之间有一条细微的界限。了解区别,并在代码审查的框架内追求风格上的变化,而不是作为孤独的执行者。

  5. Treat people who know less than you with respect, deference, and patience. Non-technical people who deal with developers on a regular basis almost universally hold the opinion that we are prima donnas at best and crybabies at worst. Don’t reinforce this stereotype with anger and impatience.

    尊重、谦恭和耐心地对待那些知识不如你的人。与开发人员定期打交道的非技术人员几乎普遍认为我们充其量是自命不凡,最糟糕的是像小孩一样哭闹。不要用愤怒和不耐烦来强化这种刻板印象。

  6. The only constant in the world is change. Be open to it and accept it with a smile. Look at each change to your requirements, platform, or tool as a new challenge, rather than some serious inconvenience to be fought.

    世界上唯一不变的就是变化。要对变化保持开放态度,并以微笑接受它。将每一次对您的需求、平台或工具的变化视为一个新的挑战,而不是需要抗拒的严重不便。

  7. The only true authority stems from knowledge, not from position. Knowledge engenders authority, and authority engenders respect — so if you want respect in an egoless environment, cultivate knowledge.

    唯一真正的权威源于知识,而非地位。知识产生权威,权威产生尊重——因此,如果你想在一个无自我的环境中获得尊重,就要培养知识。

  8. Fight for what you believe, but gracefully accept defeat. Understand that sometimes your ideas will be overruled. Even if you are right, don’t take revenge or say “I told you so.” Never make your dearly departed idea a martyr or rallying cry.

    为你所信仰的而奋斗,但优雅地接受失败。要明白,有时你的想法会被否决。即使你是对的,也不要报复或说“我早就说过了。”永远不要让你心爱的想法成为殉道者或号召口号。

  9. Don’t be “the coder in the corner.” Don’t be the person in the dark office emerging only for soda. The coder in the corner is out of sight, out of touch, and out of control. This person has no voice in an open, collaborative environment. Get involved in conversations, and be a participant in your office community.

    不要成为“角落里的程序员”。不要成为那个在黑暗办公室里只为喝汽水而出现的人。角落里的程序员不在视线范围内,失去联系,失去控制。这个人在开放、协作的环境中没有发言权。参与对话,成为你办公室社区的参与者。

  10. Critique code instead of people — be kind to the coder, not to the code. As much as possible, make all of your comments positive and oriented to improving the code. Relate comments to local standards, program specs, increased performance, etc.

    批评代码而不是人 — 对编码者要友善,而不是对代码。尽可能使您的所有评论都是积极的,并旨在改善代码。将评论与本地标准、程序规范、性能提升等相关联。

Data Abstraction and Object Orientation

Object-Oriented Programming

he abstraction provided by modules and module types has at least three important benefits:

  1. It reduces conceptual load by minimizing the amount of detail that the programmer must think about at one time.
  2. It provides fault containment by preventing the programmer from using a program component in inappropriate ways, and by limiting the portion of a program’s text in which a given component can be used, thereby limiting the portion that must be considered when searching for the cause of a bug.
  3. It provides a significant degree of independence among program components, making it easier to assign their construction to separate individuals, to modify their internal implementations without changing external code that uses them, or to install them in a library where they can be used by other programs.

Object-oriented programming can be seen as an attempt to enhance opportunities for code reuse by making it easy to define new abstractions as extensions or refinements of existing abstractions.

  • Public and Private Members
  • Tiny Subroutines(get set)
  • Derived Classes
  • General-Purpose Base Classes
  • Overloaded Constructors
  • Modifying Base Class Methods
  • Containers/Collections

Encapsulation and Inheritance(封装与继承)

Encapsulation mechanisms enable the programmer to group data and the subroutines that operate on them together in one place, and to hide irrelevant details from the users of an abstraction.

Modules

When the header and body of a module appear in separate files, a change to a module body never requires us to recompile any of the module’s users.

Classes

With the introduction of inheritance,object-oriented languages must supplement the scope rules of module-based languages to cover additional issues.

类之间的可见性讨论

Nesting (Inner Classes)

内部类的可见性?

Initialization and Finalization

Several important issues arise:

  • Choosing a constructor
  • References and values
  • Execution order
  • Garbage collection

Dynamic Method Binding

动态方法绑定会造成开销,动态和静态每个语言的选择不一样,Java选择可以用final来固定一些方法。

Unfortunately, as we shall see in Section 9.4.3, dynamic method binding imposes run-time overhead.While this overhead is generally modest,it is nonetheless a concern for small subroutines in performance-critical applications. Smalltalk, Objective-C, Modula-3, Python, and Ruby use dynamic method binding for all methods. Java and Eiffel use dynamic method binding by default, but allow individual methods and (in Java) classes to be labeled final (Java) or frozen (Eiffel), in which case they cannot be overridden by derived classes, and can therefore employ an optimized implementation.

  • Virtual and Nonvirtual Methods
  • Abstract Classes
  • Member Lookup
  • Polymorphism
  • Object Closures

Multiple Inheritance(多重继承)

Object-Oriented Programming Revisited

we characterized object-oriented programming in terms of three fundamental concepts: encapsulation, inheritance, and dynamic method binding.

  • Encapsulation allows the implementation details of an abstraction to be hidden behind a simple interface.
  • Inheritance allows a new abstraction to be defined as an extension or refinement of some existing abstraction,obtaining some or all of its characteristics automatically.
  • Dynamic method binding allows the new abstraction to display its new behavior even when used in a context that expects the old abstraction.

Summary and Concluding Remarks

这是我们关于语言设计的五个核心章节中的最后一个:名称(第3章)、控制流(第6章)、类型(第7章)、子程序(第8章)和对象(第9章)。

我们在第9.1节开始,通过确定面向对象编程的三个基本概念:封装、继承和动态方法绑定。我们还介绍了类、对象和方法的术语。我们已经在第3章的模块中看到了封装。封装允许复杂数据抽象的细节被隐藏在比较简单的接口后面。继承通过使程序员能够轻松定义新的抽象作为现有抽象的细化或扩展,扩展了封装的实用性。继承为多态子程序提供了一个自然的基础:如果一个子程序期望一个给定类的实例作为参数,那么可以使用从预期类派生的任何类的对象来代替(假设它保留了整个现有接口)。动态方法绑定通过安排对参数方法之一的调用在运行时使用与实际对象的类相关联的实现,而不是与参数的声明类相关联的实现,扩展了这种形式的多态性。我们注意到,包括Modula-3、Oberon、Ada 95和Fortran 2003在内的一些语言,通过一种类型扩展机制支持面向对象,其中封装与模块相关联,但继承和动态方法绑定与一种特殊形式的记录相关联。

在后续章节中,我们详细讨论了对象的初始化和终结、动态方法绑定以及(在PLP CD上的)多重继承。在许多情况下,我们发现功能性与一方面的简洁性和执行速度之间存在权衡。将变量视为引用而非值,通常会导致更简单的语义,但需要额外的间接性。垃圾收集,如第7.7.3节前所述,极大地简化了软件的创建和维护,但带来了运行时的成本。动态方法绑定通常要求(在一般情况下)通过vtables或其他查找机制来分派方法。多重继承的简单实现即使未使用也会带来开销。

在多个案例中,我们也看到了时间/空间的权衡。如第8.2.4节先前提到的,内联子程序可以显著提高代码性能,这不仅是通过消除子程序调用本身的开销,还允许寄存器分配、公共子表达式分析以及其他“全局”代码改进在调用间应用。同时,内联扩展通常会增加对象代码的大小。练习9.28和9.30探讨了在实现多重继承中的类似权衡。

尽管Smalltalk缺乏多重继承,但它仍被广泛认为是最纯粹和最灵活的面向对象语言之一。然而,它缺乏编译时类型检查,加上其“基于消息”的计算模型以及对动态方法查找的需求,使得其实现相对较慢。C++,具有对象值变量、默认的静态绑定、最小的动态检查和高质量的编译器,很大程度上负责面向对象编程日益增长的普及性。可靠性、可维护性和代码重用的改进,可能会也可能不会证明Smalltalk的高性能开销是合理的。它们几乎肯定证明了C++的相对适度开销,可能也证明了Eiffel略高的开销。随着软件系统规模的不断增加,互联网上分布式计算的爆炸性增长,以及高度可移植的面向对象语言(Java)、面向对象的脚本语言(Python、Ruby、PHP、JavaScript)和二进制对象标准(.NET [WHA03]、CORBA [Sie96]、JavaBeans [Sun97])的开发,面向对象编程将在21世纪的计算中显然扮演核心角色。

0%