3 Skills Needed to Write Changeable Code
- Understand OO design
- Skilled at refactoring code
- Ability to write high-value tests
Changeability is the only design metric that matters; code that's easy to change is well-designed.
Good design preserves maximum flexibility at minimum by putting off decisions at every opportunity, deferring commitments until more specific requirements arrive. When that day comes, refactoring is how you morph the current code structure into the one what will accommodate the new requirements.
Tests free you to refactor with impunity.
The true purpose of testing, just like the true purpose of design, is to reduce costs.
Intentional Testing
Benefits of testing
- Finding bugs
- Supplying documentation ("Tests provide the only reliable documentation of design.")
- Deferring design decisions ("When your tests depend on interfaces you can refactor the underlying code with reckless abandon.")
- Supporting abstractions
- Exposing design flaws
Knowing What to Test
Most programmers write too many tests...One simple way to get better value from tests is to write fewer of them.
Dealing with objects as if they are only and exactly the messages to which they respond lets you design a changeable application, and it is your understanding of the importance of this perspective that allows you to create tests that provide maximum benefit at minimum cost.
Here, then, are guidelines for what to test: Incoming messages should be tested for the state they return. Outgoing command messages should be tested to ensure they get sent. Outgoing query messages should not be tested.
Knowing When to Test
First, obviously.
Tests are reuse.
Your application is improved by ruthlessly eliminating code that is not actively being used.
Freeing your imagination from an attachment to the class of the incoming object opens design and testing possibilities that are otherwise unavailable.