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.
先审查再安装
这个资产需要先审查。复制的指令会要求 Agent dry-run、列出写入项,确认后再继续。
npx -y tokrepo@latest install 905bfdbf-dbb4-45e5-b3e3-601ab52d9ef1 --target codex先 dry-run,确认写入项后再运行此命令。
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.
常见问题
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.
引用来源 (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
来源与感谢
Kent Beck, "Tidy First?" (2023)
讨论
相关资产
Spec-Driven Develop — Spec-First Agent Skill (2026)
Spec-Driven Develop is a spec-first skill/plugin for big agent changes; verified 715★ with installs for Claude Code, Codex marketplace, and Cursor.
TaskWeaver — Code-First Data Analytics Agent
TaskWeaver is a Microsoft code-first agent framework for data analytics tasks. 6.1K+ stars. Planning, stateful execution, DataFrames, plugins. MIT.
Claude Code Swarm Orchestration Skill Guide
Complete reference skill for multi-agent swarm orchestration in Claude Code. Covers TeammateTool API, 6 orchestration patterns, spawn backends, and error handling.
MoAI-ADK — Spec-First Claude Code Agent Kit
MoAI-ADK ships a Go-based Claude Code environment with 24 agents, 52 skills, 18 languages, and strong quality gates for agent-first teams.