What is Tidy First?
Tidy First is Kent Beck's methodology for software maintenance: make small structural improvements (tidyings) before changing behavior. Instead of big refactors, you make tiny, safe cleanups that make the next change easier. This skill teaches AI agents to apply Tidy First principles — recognizing when to tidy, which tidyings to apply, and how to keep them separate from behavior changes.
Answer-Ready: Tidy First is Kent Beck's code maintenance methodology. Small structural improvements before behavior changes. This skill teaches AI agents 15+ tidying patterns — guard clauses, extract helpers, normalize symmetries, dead code removal. Keeps codebases clean without risky refactors.
Best for: AI agents maintaining and evolving codebases. Works with: Claude Code, Cursor, any AI coding tool. Setup time: Under 1 minute.
The 15 Tidyings
1. Guard Clauses
# Before (nested)
def process(data):
if data is not None:
if data.is_valid():
return transform(data)
return None
# After (guarded)
def process(data):
if data is None:
return None
if not data.is_valid():
return None
return transform(data)2. Dead Code Removal
# Remove code that is never called
# Remove commented-out code
# Remove unused imports3. Normalize Symmetries
# Before (inconsistent)
if user.is_admin:
grant_access()
if not user.is_blocked:
show_content()
# After (symmetric)
if user.is_admin:
grant_access()
if user.is_active:
show_content()4. Extract Helper
# Before
total = sum(item.price * item.quantity for item in cart if not item.is_deleted)
# After
def calculate_cart_total(cart):
return sum(item.price * item.quantity for item in cart if not item.is_deleted)
total = calculate_cart_total(cart)5. Inline Helper (Reverse of Extract)
When a helper is used only once and the name adds no clarity, inline it.
6. Reorder Statements
Move related code together. Group variable declarations near their usage.
7. Explicit Parameters
# Before (implicit)
def send_email():
to = config.admin_email # Hidden dependency
# After (explicit)
def send_email(to: str):
...8-15. More Tidyings
| # | Tidying | When |
|---|---|---|
| 8 | Chunk statements | Group related lines with blank lines |
| 9 | Extract constants | Magic numbers → named constants |
| 10 | Rename variables | Unclear names → descriptive names |
| 11 | Remove duplication | Two identical blocks → one function |
| 12 | Simplify conditionals | Complex boolean → named function |
| 13 | Flatten nesting | Deep nesting → early returns |
| 14 | Split phase | One function doing two things → two functions |
| 15 | Introduce type | Raw dict/tuple → named class/dataclass |
When to Tidy
Ask yourself:
1. Is the code I need to change messy enough to slow me down? → Tidy first
2. Is the tidying small (< 5 minutes)? → Do it now
3. Is the tidying large? → Separate PR, or skip for now
4. Am I unsure if the code is used? → Don't tidy, leave itRules for AI Agents
- Tidy in separate commits — Never mix structural and behavioral changes
- Small tidyings only — Each tidying should be < 10 lines changed
- One tidying per commit — Reviewable, revertable
- Tidy what you touch — Only tidy code you are about to change
- Don't tidy speculatively — No tidying "just in case"
FAQ
Q: How is this different from refactoring? A: Tidying is a subset of refactoring. Tidyings are small (minutes), safe, and done immediately before a behavior change. Refactoring can be large and risky.
Q: Should AI always tidy first? A: No. Only tidy if the mess slows you down. If the code is clear enough, go straight to the behavior change.
Q: Where does this come from? A: Kent Beck's book "Tidy First?" (2023). He is also the creator of TDD and extreme programming.