Tidy First — AI Code Refactoring Skill for Agents
Skill teaching AI agents Kent Beck's Tidy First methodology. Make small structural improvements before behavior changes to keep codebases clean and maintainable over time.
What it is
Tidy First is an AI agent skill that encodes Kent Beck's refactoring methodology into a structured set of instructions for AI coding assistants. The core idea: before making a behavioral change to code, consider making small structural improvements first. These tidyings make the subsequent change easier, safer, and more readable.
The skill targets developers using AI coding agents like Claude Code or Cursor who want their agents to follow disciplined refactoring practices rather than making ad-hoc changes to messy code.
How it saves time or tokens
Without the Tidy First discipline, AI agents tend to make behavioral changes directly into tangled code, producing patches that are hard to review and prone to regressions. By training the agent to tidy first, each change becomes two small, reviewable commits: one structural (renaming, extracting, reordering) and one behavioral (the actual feature or fix).
This separation reduces code review time, makes git history more meaningful, and prevents the accumulation of technical debt that eventually slows down entire teams.
How to use
- Save the Tidy First skill as
~/.claude/skills/tidy-first/SKILL.mdin your AI agent's skill directory. - The skill activates automatically when the agent encounters code that would benefit from structural improvement before a behavioral change.
- The agent will propose tidyings first (extract function, rename variable, reorder declarations), then make the behavioral change in a separate step.
Example
# Tidy First Skill
Before making behavioral changes, consider small structural tidyings.
Apply these patterns when code is messy:
Tidying patterns
- Guard clauses: replace nested if/else with early returns
- Extract helper: pull repeated logic into a named function
- Rename for clarity: make variable names reveal intent
- Reorder declarations: group related code together
- Remove dead code: delete unused functions and variables
Workflow
- Read the code that needs changing
- Identify structural issues that make the change harder
- Apply 1-3 tidyings in a separate commit
- Make the behavioral change in a clean commit
Related on TokRepo
- AI coding tools -- tools for AI-assisted development workflows
- Prompt library -- reusable prompts and skills for AI agents
Common pitfalls
- Over-tidying: the skill should apply 1-3 small tidyings before a change, not refactor the entire file. Scope discipline is critical.
- Tidying without tests: structural changes can introduce subtle bugs. Ensure tests pass after each tidying commit before proceeding to behavioral changes.
- Mixing tidying and behavior in one commit defeats the purpose. The value comes from separation -- one commit per concern.
Frequently Asked Questions
Tidy First is Kent Beck's approach to software design where you make small, safe structural improvements to code before making behavioral changes. The idea is that clean structure makes behavioral changes easier, cheaper, and less risky.
The skill is formatted as a SKILL.md file compatible with Claude Code, and can be adapted for other AI coding tools that support skill or rule files such as Cursor (.cursorrules) or Gemini CLI.
The recommended practice is 1-3 small tidyings. Each should take less than a minute to review. If you find yourself doing more than three, the code may need a dedicated refactoring effort rather than opportunistic tidying.
No. Tidy First is about opportunistic, small structural improvements done right before a behavioral change. Large-scale refactoring efforts still have their place but are planned separately from feature work.
Code reviews become easier because each commit has a single purpose -- either structural improvement or behavioral change. Reviewers can verify structural tidyings quickly and focus their attention on the behavioral change.
Citations (3)
- Tidy First by Kent Beck— Kent Beck's Tidy First methodology for incremental refactoring
- Anthropic Claude Code Documentation— Skill file format for Claude Code AI agent
- Martin Fowler on Refactoring— Refactoring as preparation for behavioral changes
Related on TokRepo
Source & Thanks
Based on Kent Beck's "Tidy First?" (2023).
tidyfirst.substack.com — Kent Beck's newsletter
Discussion
Related Assets
Claude-Flow — Multi-Agent Orchestration for Claude Code
Layers swarm and hive-mind multi-agent orchestration on top of Claude Code with 64 specialized agents, SQLite memory, and parallel execution.
ccusage — Real-Time Token Cost Tracker for Claude Code
CLI that reads ~/.claude logs and breaks down Claude Code token spend by day, session, and project — pluggable into your statusline.
SuperClaude — Workflow Framework for Claude Code
Adds 16+ slash commands, 9 cognitive personas, and a smart flag system to Claude Code in one pipx install.