每当我要进行重构的时候,第一个步骤永远相同:我得为即将修改的代码建立一组可靠的测试环境
任何一个傻瓜都能写出计算机可以理解的代码。唯有写出人类容易理解的代码,才是优秀的程序员。
绝大多数情况下,函数应该放在它所使用的数据所属的object(class)内
Poorly designed code usually takes more code to do the same things, often because the code
quite literally does the same thing in several places. Thus an important aspect of improving
design is to eliminate duplicate code. The importance of this lies in future modifications to the
code. Reducing the amount of code won't make the system run any faster, because the effect on
the footprint of the programs rarely is significant. Reducing the amount of code does, however,make a big difference in modification of the code. The more code there is, the harder it is to
modify correctly. There's more code to understand. You change this bit of code here, but the
system doesn't do what you expect because you didn't change that bit over there that does much
the same thing in a slightly different context. By eliminating the duplicates, you ensure that the
code says everything once and only once, which is the essence of good design.
What is it that makes programs hard to work with? Four things I can think
of as I am typing this are as follows:
· Programs that are hard to read are hard to modify.
· Programs that have duplicated logic are hard to modify.
· Programs that require additional behavior that requires you to
change running code are hard to modify.
· Programs with complex conditional logic are hard to modify.
So, we want programs that are easy to read, that have all logic specified
in one and only one place, that do not allow changes to endanger
existing behavior, and that allow conditional logic to be expressed as
simply as possible
Replace magic number with symbolic constant