您的位置:首页 > 运维架构

《Code Reading:The Open Source Perspective》Chapter two之Control Structures Revisited

2010-07-17 19:54 429 查看
 

Having examined the syntactic details of the control flow statements we can now focus our attention on the way we can reason about them at an abstract level.
The first thing you should remember is to examine one control structure at a time, treating its contents as a black box. The beauty of structured programming is that the control structures employed allow you to abstract and selectively reason
about parts of a program, without getting overwhelmed by the program's overall complexity.
Consider the following code sequence.
while (enum.hasMoreElements()) {
[...]
if (object instanceof Resource) {
[...]
if (!copy(is, os))
[...]
} else if (object instanceof InputStream) {
[...]
if (!copy((InputStream) object, os))
[...]
} else if (object instanceof DirContext) {
[...]
}
}

Although we have removed a large part of the 20 lines of code, the loop still appears quite complex. However, the way you should read the above loop is
while (enum.hasMoreElements()) {
// Do something
}

At that level of abstraction you can then focus on the loop body and examine its functioning without worrying about the control structure in which it is enclosed. This idea suggests a second rule we should follow when examining a program's
flow of control: treat the controlling expression of each control structure as an assertion for the code it encloses. Although the above statement may appear obtuse or trivial, its significance to the understanding of code can be profound.
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  structure object idea each os
相关文章推荐