Agent skill
refactoring
Refactor code to improve maintainability by consolidating redundant logic and applying DRY principles
Install this agent skill to your Project
npx add-skill https://github.com/uuta/dotfiles/tree/main/skills/refactoring
SKILL.md
Refactoring
冗長なロジックや重複している箇所を集約したりすることで保守性を上げる。
Stragegy
- Check whether the logic can be erased by consolidating into Schema
- Test first. Before starting implementation, check the current tests for the target method cover the all pattern.
Points of view
The following rules are listed by the order of priorities.
- Is the code in the right place from a DDD perspective?
- Is it possible to remove the logic by changing the schema itself?
- Isn't it against DRY?
- Is it possible to write code with better visibility by cutting it out into a separate method?
- Can loop be improved more? (O(n) > O(n^2) > O(2^n) > O(n!))
Validate Assumptions Against Downstream Capabilities
Before adding preprocessing or normalization steps, verify that the downstream process doesn't already handle the problem.
Ask: "Does the downstream layer already have the capability to handle this without preprocessing?"
If the answer is yes, the upstream processing is redundant and should be removed.
Examples:
- Normalizing input to English before passing to an AI that already handles multilingual input
- Fetching a "cleaner" version of data when the downstream consumer can work with the original
- Transforming data into a format that the next step will transform again anyway
Related smell: A try/catch fallback that almost always fires is a signal that the try block's assumption is wrong — the "exception" has become the norm. Treat it as near-dead code.
Fat Usecase Avoidance
Usecases should orchestrate operations, not contain business logic. Follow these principles to keep usecases thin and maintainable.
1. Move Logic to Appropriate Layers
Place logic where it belongs according to DDD principles. Business rules, complex calculations, and data transformations should live in domain services and value objects, not in usecases.
Guidelines:
- Business rules → Domain services
- Data transformation → Value objects
- Validation → Domain validators
- Complex calculations → Domain services
Example: Move filtering logic to domain services (e.g., FileFilter.shouldReview()), calculations to value objects (e.g., ComplexityCalculator.calculate()). Usecases only orchestrate these operations.
2. Eliminate else Clauses
Use early returns and guard clauses instead of else blocks to reduce cognitive load.
Before:
if condition:
if another_condition:
return success_result
else:
return failure
else:
raise error
After:
if not condition:
raise error
if not another_condition:
return failure
return success_result
3. Reduce Nesting Depth
Flatten nested structures with early returns or by extracting methods. Replace multiple nested conditions with guard clauses that return early, or extract the nested logic into well-named helper methods.
4. When to Extract Helper Methods
Extract methods to improve single responsibility, not just to hide complexity.
When to extract:
- Logic has a clear single responsibility
- Method can be meaningfully named
- Logic might be reused
- Makes the main flow more readable
When NOT to extract:
- Just to reduce line count
- Creates methods that are only called once with unclear purpose
- Makes code harder to follow by breaking natural flow
- Hides complexity instead of addressing it
Avoid over-extraction that creates trivial helper methods. Simple operations like getFiles() that just returns request.files or buildResponse() that just wraps a constructor call should stay inline.
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
pbi-sub-issue
タスク分割プラン(pbi-task-splitで作成)を元に、GitHub の Sub-issue を作成する。親 Issue との関係も自動で設定。
dev
Orchestrate TDD-based development workflow. Checks workflow state and delegates work to subagents (dev-plan, dev-step, create-pr).
neo-frontend-design
Create dark command-center / cyberpunk dashboard interfaces inspired by the OJPP Portal design. Use this skill when the user asks for a dark, neon-accented, terminal-aesthetic UI. Generates production-grade code with glitch effects, monospace typography, and per-module neon color coding.
tdd-plan
Create implementation plans following TDD methodology with test-first approach. Each test file is immediately followed by its implementation (fine-grained RED→GREEN cycles).
prototype
Generate 3 HTML prototype variants using a team of parallel agents. Each agent creates a distinct design pattern (e.g., hover effects, animations, layouts) based on user prompts or docs/goal.md. Use when the user wants to explore multiple design directions for a UI component.
review-format
Output in the specified format when a review of specific markdown is requested.
Didn't find tool you were looking for?