Refactoring: Improving the Design of Existing Code (Addison-Wesley Object Technology Series)
Rate it:
3%
Flag icon
Refactoring is the process of changing a software system in such a way that it does not alter the external behavior of the code yet improves its internal structure.
3%
Flag icon
“Improving the design after it has been written.
3%
Flag icon
A good design comes first, and the coding comes second.
4%
Flag icon
Here’s how to get the most from this book without reading all of it.
5%
Flag icon
If you are writing a program that you don’t expect to change, then cut and paste is fine. If the program is long lived and likely to change, then cut and paste is a menace.
6%
Flag icon
When you find you have to add a feature to a program, and the program’s code is not structured in a convenient way to add the feature, first refactor the program to make it easy to add the feature, then add the feature.
6%
Flag icon
Whenever I do refactoring, the first step is always the same. I need to build a solid set of tests for that section of code.
6%
Flag icon
Before you start refactoring, check that you have a solid suite of tests. These tests must be self-checking.
7%
Flag icon
Refactoring changes the programs in small steps. If you make a mistake, it is easy to find the bug.
7%
Flag icon
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
11%
Flag icon
It is a bad idea to do a switch based on an attribute of another object. If you must use a switch statement, it should be on your own data, not on someone else’s.
11%
Flag icon
Figure 1.12. Class diagram before moving methods to movie
11%
Flag icon
A movie can change its classification during its lifetime. An object cannot change its class during its lifetime. There is a solution, however, the State pattern [Gang of Four].
11%
Flag icon
you are familiar with the Gang of Four patterns, you may wonder, “Is this a state, or is it a strategy?”
12%
Flag icon
To introduce the state pattern I will use three refactorings. First I’ll move the type code behavior into the state pattern with Replace Type Code with State/Strategy (227). Then I can use Move Method (142) to move the switch statement into the price class. Finally I’ll use Replace Conditional with Polymorphism (255) to eliminate the switch statement.
13%
Flag icon
The most important lesson from this example is the rhythm of refactoring: test, small change, test, small change, test, small change. It is that rhythm that allows refactoring to move quickly and safely.
13%
Flag icon
Refactoring (noun): a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior.
13%
Flag icon
Refactor (verb): to restructure software by applying a series of refactorings without changing its observable behavior.
14%
Flag icon
When you refactor, you make a point of not adding function; you only restructure the code. You don’t add any tests (unless you find a case you missed earlier); you only change tests when you absolutely need to in order to cope with a change in an interface.
14%
Flag icon
don’t want to proclaim refactoring as the cure for all software ills. It is no “silver bullet.”
14%
Flag icon
“I’m not a great programmer; I’m just a good programmer with great habits.”
14%
Flag icon
The first time you do something, you just do it. The second time you do something similar, you wince at the duplication, but you do the duplicate thing anyway. The third time you do something similar, you refactor.
14%
Flag icon
Whenever I have to think to understand what the code is doing, I ask myself if I can refactor the code to make that understanding more immediately apparent.
14%
Flag icon
Code reviews help spread knowledge through a development team. Reviews help more experienced developers pass knowledge to less experienced people.
15%
Flag icon
Programs have two kinds of value: what they can do for you today and what they can do for you tomorrow.
15%
Flag icon
Every time you break one thing into two pieces, you have more things to manage.
16%
Flag icon
Don’t publish interfaces prematurely. Modify your code ownership policies to smooth refactoring.
18%
Flag icon
If it stinks, change it.
18%
Flag icon
A heuristic we follow is that whenever we feel the need to comment something, we write a method instead.
20%
Flag icon
If you only have a few cases that affect a single method, and you don’t expect them to change, then polymorphism is overkill. In this case Replace Parameter with Explicit Methods (285) is a good option.
20%
Flag icon
You get it when people say, “Oh, I think we need the ability to do this kind of thing someday” and thus want all sorts of hooks and special cases to handle things that aren’t required.
21%
Flag icon
When you feel the need to write a comment, first try to refactor the code so that any comment becomes superfluous.
21%
Flag icon
Fixing the bug is usually pretty quick, but finding it is a nightmare.
21%
Flag icon
Make sure all tests are fully automatic and that they check their own results.
21%
Flag icon
A suite of tests is a powerful bug detector that decapitates the time it takes to find bugs.
23%
Flag icon
Functional tests typically treat the whole system as a black box as much as possible.
23%
Flag icon
When you get a bug report, start by writing a unit test that exposes the bug.
23%
Flag icon
Think of the boundary conditions under which things might go wrong and concentrate your tests there.