Know the common sources of complexity
One of the goals of writing good programs is simply to avoid complexity.
The three most common sources of complexity are:
- large number N of things:
a large number of lines in a class, classes in a package, and so on.
If such things start to feel too large, start thinking about how they might be broken up sensibly into cohesive parts.
- large number N of interconnections between things: this increases coupling.
With fewer interconnections, the less there is that can go wrong, and the less "wiring" there is between parts.
- change of state with time: this is a particularly fruitful source of complexity.
Furthermore, if data is changing, then the more long-lived it is, then the harder it is to understand.
Immutable objects negate this source of complexity altogether; that's why they're so useful.
When you see code becoming complex in the above ways, it's always useful to start thinking about alternatives.
See Also :