《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.
相关文章推荐
- 学习笔记之《Code Reading:The Open Source Perspective》Chapter two(part1)
- 学习笔记之《Code Reading:The Open Source Perspective》Chapter two(part2)
- 学习笔记之《Code Reading:The Open Source Perspective》Chapter one
- Code Reading: The Open Source Perspective 各章精要
- Code reading: the open source perspective
- Code Reading: The Open Source Perspective 各章精要2
- 作为程序员容易忽略的一些问题 读《Code Quality :The Open Source Perspective》有感
- Code Quality: The Open Source Perspective
- Open Source for the Operations Research Community
- .Net Quick start for the OpenFlashChart control
- SOS: Autodesk MapGuide Studio - Preview can not display "Feature Label" with Chinese text on the platform of MapGuide Open Source
- 开源网络准入系统(open source Network Access Control system)
- vs2008 There was a failure while initializing the Microsoft Visual SourceSafe source control provider. You cannot use this provi
- <<the architecture of open source applications>> 中关于虚拟机复制与迁移(中)
- The Ultimate List of Open Source Static Code Analysis Security Tools
- Facebook, Google, and the Rise of Open Source Security Software
- Open Source Software List: The Ultimate List
- the Open Source Community
- Open Source Development (This Article Is the Winner of PRSA 2003 Award in Excellence in Technology …
- Atlas Control Toolkit and Source Code for the Build-in Asp.Net 2.0 Providers