It's worth emphasizing that bottom-up design doesn't mean just writing the same program in a different order. When you work bottom-up, you usually end up with a different program. Instead of a single, monolithic program, you will get a larger language with more abstract operators, and a smaller program written in it. Instead of a lintel, you'll get an arch.
In typical code, once you abstract out the parts which are merely bookkeeping, what's left is much shorter; the higher you build up the language, the less distance you will have to travel from the top down to it. This brings several advantages:
1. By making the language do more of the work, bottom-up design yields programs which are smaller and more agile. A shorter program doesn't have to be divided into so many components, and fewer components means programs which are easier to read or modify. Fewer components also means fewer connections between components, and thus less chance for errors there. As industrial designers strive to reduce the number of moving parts in a machine, experienced Lisp programmers use bottom-up design to reduce the size and complexity of their programs.
2. Bottom-up design promotes code re-use. When you write two or more programs, many of the utilities you wrote for the first program will also be useful in the succeeding ones. Once you've acquired a large substrate of utilities, writing a new program can take only a fraction of the effort it would require if you had to start with raw Lisp.
3. Bottom-up design makes programs easier to read. An instance of this type of abstraction asks the reader to understand a general-purpose operator; an instance of functional abstraction asks the reader to understand a special-purpose subroutine. [1]
4. Because it causes you always to be on the lookout for patterns in your code, working bottom-up helps to clarify your ideas about the design of your program. If two distant components of a program are similar in form, you'll be led to notice the similarity and perhaps to redesign the program in a simpler way.
Bottom-up design is possible to a certain degree in languages other than Lisp. Whenever you see library functions, bottom-up design is happening. However, Lisp gives you much broader powers in this department, and augmenting the language plays a proportionately larger role in Lisp style-- so much so that Lisp is not just a different language, but a whole different way of programming.
上下上下...上上下下...
需要强调的是自下而上的设计并不仅仅只是调个方向来写出同样的代码。当你自下而上的工作时,往往最后写出来的代码也是不一样的。程序不再是很大一坨,而语言会变得更强大,拥有更多的抽象操作符,这样程序本身将会变得更小。换斗移星,去直梁,得弧拱。
In typical code, once you abstract out the parts which are merely bookkeeping, what's left is much shorter; the higher you build up the language, the less distance you will have to travel from the top down to it. This brings several advantages:
通常写代码时,当我们从一些流水帐般的逻辑(如增删改查)中抽象时,其实其它逻辑所剩无几。对于语言层面的构建层次越多,每次所需其它表达越少。这带来诸多好处:
1. By making the language do more of the work, bottom-up design yields programs which are smaller and more agile. A shorter program doesn't have to be divided into so many components, and fewer components means programs which are easier to read or modify. Fewer components also means fewer connections between components, and thus less chance for errors there. As industrial designers strive to reduce the number of moving parts in a machine, experienced Lisp programmers use bottom-up design to reduce the size and complexity of their programs.
1. 让语言来承担更多的工作,自下而上的设计逼着程序更简洁与灵活。一段本来就简短的代码不需要再被切分成更小的模块了。更少的模块数量也意味着程序更易读和修改。更少的模块也意味着更少的模块间的依赖和耦合,也减少了错误发生的可能性。就像工业设计者努力的减少机器中运动部件的数量一样,资深的Lisp程序员用自下而上的设计来减少代码的大小与复杂度。
2. Bottom-up design promotes code re-use. When you write two or more programs, many of the utilities you wrote for the first program will also be useful in the succeeding ones. Once you've acquired a large substrate of utilities, writing a new program can take only a fraction of the effort it would require if you had to start with raw Lisp.
2. 自下而上的设计能促进代码重用。当你写很很多的程序时,往往为第一个程序写的Utility类也可以应用到其它后来的程序上。当你积累了很多底层Utility类之后,比起用最原始的Lisp,写新的程序往往只需很少的工作了。
3. Bottom-up design makes programs easier to read. An instance of this type of abstraction asks the reader to understand a general-purpose operator; an instance of functional abstraction asks the reader to understand a special-purpose subroutine. [1]
3. 自下而上的设计能让程序更易读。读程序的人只需理解抽象出的通用操作符,与抽象出的底层逻辑即可。[1]
4. Because it causes you always to be on the lookout for patterns in your code, working bottom-up helps to clarify your ideas about the design of your program. If two distant components of a program are similar in form, you'll be led to notice the similarity and perhaps to redesign the program in a simpler way.
4. 自下而上的工作可以帮助你更专注于代码的设计模式,从而理清程序设计思路。比如你很容易就能注意到,同一个程序中的两段八杆子打不着的代码居然长得很像,并且能用简单的方式重新设计。
Bottom-up design is possible to a certain degree in languages other than Lisp. Whenever you see library functions, bottom-up design is happening. However, Lisp gives you much broader powers in this department, and augmenting the language plays a proportionately larger role in Lisp style-- so much so that Lisp is not just a different language, but a whole different way of programming.
自下而上的设计不仅仅只能在Lisp中才能达到。任何当你看到库函数的时候,自下而上的设计就自然出现了。但是Lisp能给你更大的力量,在Lisp编程风格中,代码量的增加能带来对应功能的增加。因此Lisp不仅仅是种语言,也是一种全新的编程方式。
——自下而上的编程 | Programming Bottom-Up Paul Graham
一周热门 更多>