8-7<X15>roman.xml 重构
频繁typo:他们
它们
这个和上一章一样
15.1 处理 bugs
例15.1, (1):在前面的章节中你注意到一个空字符串会匹配
上那个检查罗马数字有效性的正则表达式了吗?
例15.5, (1):空字符串测试用例现在通过了,说明 Bug 被改正
了。
修正
“改正” bug……怎么读怎么不通
Para -1:Coding this way does not make fixing bugs any easier. Simple bugs (like this one) require simple test cases; complex bugs will require complex test cases. In a testing-centric environment, it may seem like it takes longer to fix a bug, since you need to articulate in code exactly what the bug is (to write the test case), then fix the bug itself.
这样编程,并没有令 Bug 修正变得简单。简单的 Bug (就像这一个)需要简单的测试用例,复杂 Bug 则需要复杂的测试用例。在测试为核心的氛围下,这好像延长了修正 Bug 的时间,因为你需要先贴切地描述出 Bug (编写测试用例)然后才去修正它。
这样编程,并没有令 Bug 修正变得简单。简单的 Bug (就像这一个)需要简单的测试用例,复杂 Bug 则需要复杂的测试用例。以测试为核心的氛围好像延长了修正 Bug 的时间,因为你需要先贴切地描述出 Bug (编写测试用例)然后才去修正它。
同上:而且,由于任何新的更改后你都可以轻易地重新运行所有测试用例,新代码破坏老代码的机会也变得微乎其微。
而且不论如何更改,你都可以轻易地重新运行所有测试用例,新代码破坏老代码的机会也变得微乎其微。
15.2 应对需求变化
例15.8, (1):你不需要改变函数的其他部分,它们已经适用于新的情况。(它们会欣然地为新的 1000 添加 'M',以 4000 为例,他们
会返回 'MMMM' )
这里再说“它们”就不通了
函数
例15.8, (2):这就允许最多 4 个 M 字符而不再是 3 个,意味着你允许的
代表 4999 而不是 3999 的罗马数字。
15.3 重构
Para 2:重构是在可运行代码的基础上使之更良好工作的过程。
重构是在可运行代码的基础上使之工作得更好的过程。
例15.10, (1):This is the syntax you've seen before: re.search takes a regular expression as a string...
这是你曾在 re.search 中看到的语法。……
这是你看到过的 re.search 语法。
例15.10, (3):re.compile 返回
的已编译的 pattern 对象有几个值得关注的功能:……
例15.14, (2):This is not a license to endlessly tweak your code just for the sake of tweaking it;...
这并不是给只是为了调整代码而无休止地调整以许可,……
这并不是给无休止地为了调整代码而调整代码以许可;……
突出了for the sake of
例15.15, (1):The re.compile function can take an optional second argument, which is a set of one or more flags that control various options about the compiled regular expression.
re.compile 函数的第二个参数是可选的,这个参数通过一个或一组选项设置来控制预编译正则表达式的多样化选项。
re.compile 函数的第二个参数是可选的,这个参数通过一个或一组标志(flag)来控制预编译正则表达式的选项。
例15.16, (1):新“verbose”版本和老版本的运行速度一样。事实上,编译的 pattern 对象也一样,因为 re.compile 函数
会剔除掉所有你添加的内容。
15.4 后记
Para 1:The biggest headache (and performance drain) in the program as it is currently written is the regular expression, which is required because you have no other way of breaking down a Roman numeral.
现在写的这个程序中最令人头痛(性能负担)的是正则表达式,它是必需的,因为没有其它方法来处理罗马数字。
现在写的这个程序中最令人头痛的性能负担是正则表达式,但它是必需的,因为没有其它方法来识别罗马数字。Para -1:And unit tests can give you the confidence to do large-scale refactoring... even if you didn't write the original code.
并且单元测试给了你大规模重构的信心……既便没写出原有的代码也是这样。
并且单元测试给了你大规模重构的信心……即使原有的代码不是你写的。
15.5 小结
Para 8:为了
改进性能、可伸缩性、可读性、可维护性和任何缺少的特性而无情地重构。