📄 1software.html
字号:
<html>
<head>
<title>Software Project</title>
<link rel="stylesheet" href="rs.css" tppabs="http://www.relisoft.com/book/rs.css">
</head>
<body background="margin.gif" tppabs="http://www.relisoft.com/book/images/margin.gif" bgcolor="#ffffe0">
<!-- Main Table -->
<table cellpadding="6">
<tr>
<td width="78"> </td>
<td>
<h2>About Software</h2>
<h3>Complexity</h3>
<p class=topics>Dealing with complexity, the finite capacity of human mind, divide and conquer, abstraction.
<P>Dealing with complexity is the essence of software engineering. It is also the most demanding part of it, requiring both discipline and creativity. Why do we need special methodologies to deal with complexity? The answer is in our brains. In our immediate memory we can deal only with a finite and rather small number of objects--whatever type they are, ideas, images, words. The ballpark figure is seven plus/minus two, depending on the complexity of the objects themselves. Apparently in many ancient cultures the number <I>seven</I> was considered synonymous with <I>many</I>. There are many folk stories that start with "Long, long ago behind seven mountains, behind seven forests, behind seven rivers there lived..."
<P>There are essentially two ways in which we human beings can deal with complexity. The divide-and-conquer method, and the abstraction method. The divide-and-conquer methods is based on imposing a tree-like structure on top of a complex problem. The idea is that at every node of the tree we have to deal with only a small number of branches, within the limits of our immediate memory. The traversal of the tree leaf-to-root or root-to-leaf requires only a logarithmic number of steps-- again, presumably within the limits of our immediate memory. For instance, the body of academic knowledge is divided into humanities and sciences (branching factor of 2). Sciences are subdivided into various areas, one of them being Computer Science, and so on.
<P>To understand Kernighan and Ritchie's book on C, the CS student needs only very limited education in humanities. On the other hand, to write a poem one is not required to program in C. The tree-like subdivision of human knowledge not only facilitates in-depth traversal and search, it also enables division of work between various teams. We can think of the whole humanity as one large team taking part in the enormous project of trying to understand the World.
<P>Another very powerful tool developed by all living organisms and perfected by humans is abstraction. The word "abstraction" has the same root as subtraction. Abstracting means subtracting non-essential features. Think of how many features you can safely subtract from the description of your car before it stops being recognizable as a car. Definitely the color of the paint, the license plates, the windshield wipers, the capacity of the trunk, etc. Unconsciously the same process is applied by a bird when it creates its definition of a "predator." Abstraction is not 100% accurate: a crow may get scared by a scarecrow, which somehow falls within its abstract notion of a "predator."
<P>Division and abstraction go hand in hand in, what one can call, divide-and-abstract paradigm. A complex system can be visualized as a very large network of interconnected nodes. We divide this network into a few "objects"--subsets of nodes. A good division has the property that there are as few inter-object connections as possible. To describe the objects resulting from such a division we use abstraction. In particular, we can describe the objects by the way they connect to other objects (the interface). We can simplify their inner structure by subtracting as many inessential features as possible. At every stage of division, it should be possible to understand the whole system in terms of interactions between a few well abstracted objects. If there is no such way, we give up. The real miracle of our World is that large portions of it (maybe even everything) can be approached using this paradigm.
<p><IMG SRC="Image1.gif" tppabs="http://www.relisoft.com/book/proj/images/Image1.gif" WIDTH=288 HEIGHT=192>
<p class=caption>A complex system.
<p><IMG SRC="Image2.gif" tppabs="http://www.relisoft.com/book/proj/images/Image2.gif" WIDTH=288 HEIGHT=192>
<p class=caption>Abstracting objects out of a complex system.
<p><IMG SRC="Image3.gif" tppabs="http://www.relisoft.com/book/proj/images/Image3.gif" WIDTH=288 HEIGHT=192>
<p class=caption>The high level view of the complex system after abstracting objects.
<p>This process is then repeated recursively by dividing objects into sub-objects, and so on. For every object, we first undo the abstraction by adding back all the features we have subtracted, divide it into sub-objects and use new abstractions to define them. An object should become understandable in terms of a few well abstracted sub-objects. In some way this recursive process creates a self-similar, fractal-like structure.
<P><IMG SRC="Image4.gif" tppabs="http://www.relisoft.com/book/proj/images/Image4.gif" WIDTH=474 HEIGHT=246>
<p class=caption>The fractal structure of a complex systems.
<P>In software engineering we divide a large project into manageable pieces. In order to define, name and describe these pieces we use abstraction. We can talk about symbol tables, parsers, indexes, storage layers, etc. They are all abstractions. And they let us divide a bigger problem into smaller pieces.
<h3>The Fractal Nature of Software</h3>
<P>Let me illustrate these ideas with the familiar example of the software project that we've been developing in the second part of the book--the calculator. The top level of the project is structured into a set of interrelated objects, Figure.
<P><IMG SRC="Image5.gif" tppabs="http://www.relisoft.com/book/proj/images/Image5.gif" WIDTH=414 HEIGHT=354>
<P class=caption>Top level view of the calculator project.
<P>This system is closed in the sense that one can explain how the program works (what the function <var>main</var> does) using only these objects--their public interfaces and their functionality. It is not necessary to know <I>how</I> these object perform their functions; it is enough to know <I>what</I> they do.
<P>So how does the program work? First, the <var>Calculator</var> is created inside <var>main</var>. The <var>Calculator</var> is <var>Serializable</var>, that means that its state can be saved and restored. Notice that, at this level, we don't need to know anything about the streams--they are black boxes with no visible interface (that's why I didn't include them in this picture).
<P>Once the <var>Calculator</var> is created, we enter the loop in which we get a stream of text from the user and create a <var>Scanner</var> from it. The <var>Scanner</var> can tell us whether the user input is a command or not. If it is a command, we create a <var>CommandParser</var>, otherwise we create a <var>Parser</var>. Either of them requires access to both the <var>Calculator</var> and the <var>Scanner</var>. <var>CommandParser</var> can <var>Execute</var> a command, whereas <var>Parser</var> can <var>Parse</var> the input and <var>Calculate</var> the result. We then display the result and go back to the beginning of the loop. The loop terminates when <var>CommandParser</var> returns status <var>stQuit</var> from the <var>Execute</var> method.
<P>That's it! It could hardly be simpler than that. It's not easy, though, to come up with such a nice set of abstraction on the first try. In fact we didn't! We had to go through a series of rewrites in order to arrive at this simple structure. All the techniques and little rules of thumb described in the second part of the book had this goal in mind.
<P>But let's continue the journey. Let's zoom-in into one of the top level components--the Calculator. Again, it can be described in terms of a set of interrelated objects, Figure.
<P><IMG SRC="Image6.gif" tppabs="http://www.relisoft.com/book/proj/images/Image6.gif" WIDTH=510 HEIGHT=264>
<P class=caption>The result of zooming-in on the Calculator.
<P>And again, I could explain the implementation of all Calculator methods using only these objects (and a few from the level above).
<P>Next, I could zoom-in on the <var>Store</var> object and see a very similar picture.
<P><IMG SRC="Image7.gif" tppabs="http://www.relisoft.com/book/proj/images/Image7.gif" WIDTH=564 HEIGHT=390>
<P class=caption>The result of zooming-in on Store.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -