The Two Hats: Adding Function vs Restructuring
What you'll learn: You'll understand why you must strictly separate adding new features from refactoring, never mixing the two activities.
The Mental Switch
Imagine you're working at your desk wearing two different hats. One hat says "BUILD" and the other says "CLEAN." Martin Fowler popularized this metaphor: when developing software, you're always wearing one of two hats, but never both at once.
The "Adding Functionality" Hat: When wearing this hat, you're focused on making the software do something new. You write tests for new behavior, then write code to make those tests pass. You might create messy code—that's okay for now. Your commits should show failing tests becoming passing tests.
The "Refactoring" Hat: When wearing this hat, you're focused purely on improving the structure of existing code without changing what it does. You're not adding features or fixing bugs. All existing tests should remain passing throughout. Your commits should show structure changes with no behavioral changes.
Why Keep Them Separate?
Mixing these activities creates confusion and risk. If you refactor while adding a feature and something breaks, you won't know whether the problem is in your new logic or in your structural changes. Debugging becomes a nightmare.
By keeping them separate, you maintain clarity: if tests fail during feature work, it's your new code. If tests fail during refactoring, you've accidentally changed behavior and must revert immediately.
The Rhythm
Alternate between hats deliberately. Add a feature (tests green), then switch hats and clean up the mess you just made. Commit. Then add the next feature.
Key Takeaway: Never mix adding functionality with refactoring—wear one hat at a time to maintain code clarity, simplify debugging, and ensure your changes are intentional and traceable.