Unit 7 — Clean Code, Code Smells & Refactoring¶
Learning objectives¶
- Identify maintainability problems at method, class, and dependency levels.
- Distinguish refactoring from feature work and bug fixing.
- Use tests and small steps to reduce refactoring risk.
Refactoring¶
Refactoring changes the internal structure of software while preserving externally observable behavior. It is safer when behavior is protected by automated tests and changes are kept small.
Common smells¶
- Long method / deeply nested control flow.
- Duplicated logic.
- Large class / too many responsibilities.
- Feature envy and inappropriate intimacy.
- Primitive obsession.
- Shotgun surgery and divergent change.
- Dead code and speculative generality.
Safe loop¶
flowchart LR
T1[Tests green] --> S[Small structural change]
S --> T2[Run tests]
T2 -->|green| C[Commit]
T2 -->|red| F[Fix / revert]
C --> T1
F --> T1
Code quality should be discussed in context: a smell is a signal to investigate, not an automatic proof of bad design.