assets/skills/feature/refactor/SKILL.md
DDD refactoring workflow with phase-based checks and review loop. Refactor existing code to DDD architecture or improve existing DDD structure. Use when user says "refactor", "clean up", "improve code", "restructure", "migrate to ddd", "refactor ddd".
npx skillsauth add phuthuycoding/moicle feature-refactorInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Restructure existing code into DDD layers, or fix drift in an existing DDD module. Preserves behavior — refactor structure, never change logic.
ARGUMENTS: <module> <domain> — e.g., marketing notification, users identity, products catalog
/feature-new/fix-hotfix or /fix-root-causeDetect stack via ~/.claude/architecture/_shared/stack-detection.md. Load ddd-architecture.md + stack doc.
0 FOUNDATION → 1 ANALYZE → 2 DOMAIN → 3 INFRA → 4 APP → 5 TESTS → 6 CLEANUP → REVIEW LOOP
Each phase has a Rule Check. Do not skip any phase.
New project (no domain/ yet): create
domain/shared/ — base event types, event collector, dispatcher interfaceExisting project: verify
ls {domain_root}/shared/ 2>/dev/null && echo PASS || echo NEED SETUP
ls {eventbus_path}/ 2>/dev/null && echo PASS || echo NEED SETUP
If FAIL → set up foundation before continuing.
Goal: read ALL source files in the old module before touching anything.
## Refactor Plan: {module} → {domain}
### Current state
- Entities/models: {list with fields}
- Usecases (functions): {list with 1-line logic summary}
- DTOs: {list}
- Cross-module calls: {list}
- Side-effects: {notifications / SSE / analytics / async jobs}
- External deps: {DB, cache, messaging}
- Endpoints/screens: {list with method + path}
- Test files: {list with case counts}
### Proposed DDD structure
- Value objects to extract: {list}
- Entities: {list}
- Events: {list}
- Ports: {list}
- Usecases: {list}
- Listeners: {list}
Create domain/{domain}/ (or add to existing).
valueobjects/) — extract typed values (status strings, rates, amounts). Immutable + behavior methods. Stdlib imports only.entities/) — convert old models. Constructor + behavior methods + event collection. Add mappers to/from persistence. No framework imports.events/) — one file per event. Extract from existing direct side-effect calls.ports/) — one file per interface. Store ports (persistence), adapter ports (external services). Platform-agnostic naming (URLParser not ShopeeURLParser). No infra imports.usecases/) — extract business logic from old controllers/handlers/services. Import from ports/. Split by concern, ≤200 lines/file. No infra imports.{build_domain} && echo PASS || echo FAIL
{grep_forbidden in domain/} && echo FAIL || echo PASS
{cross_domain_check} && echo FAIL || echo PASS
domain/{domain}/ports/CRITICAL: Side-effects (notifications, SSE, analytics, jobs) MUST NOT be called directly in usecases or infra. Flow must be: entity collects event → usecase dispatches → listener handles.
CRITICAL: read old tests first, copy every scenario. Do not lose coverage.
{test_command} passes{full_build} && echo PASS || echo FAIL
test -d {old_module_path} && echo "FAIL: still there" || echo PASS
grep -r "{old_import_path}" --include="*.{ext}" . && echo "FAIL: stale imports" || echo PASS
After Phase 6, call /review-architect {stack} {domain}. Loop until score ≥ B.
LOOP:
1. /review-architect {stack} {domain}
2. IF violations severity ≥ MEDIUM:
fix all → full build → all tests → GOTO 1
3. IF score ≥ B → BREAK
## Refactor Complete: {module} → {domain}
### Changes
- Files created: {N}
- Files modified: {N}
- Files deleted: {N}
### Endpoints preserved
| Old path | New handler | Status |
|----------|-------------|--------|
### Domain events introduced
| Event | Listener(s) |
|-------|-------------|
### Tests
- Files: {N}, cases: {M}
- All old scenarios migrated: YES
### Review score: {A/B}
- Build / Lint / Domain purity / Old module removed / No stale imports / Tests: all PASS
| When | Use |
|------|-----|
| Building from scratch (no existing code) | /feature-new |
| Just adding tests to untested code | /review-tdd |
| Reviewing the refactor before merging | /review-branch |
| Final architecture check (called automatically in review loop) | /review-architect |
| Phase | Agent | Purpose |
|-------|-------|---------|
| ANALYZE | @refactor | Identify refactor scope |
| ANALYZE | @code-reviewer | Smell detection |
| PLAN | @clean-architect | Architecture alignment |
| BUILD | Stack-specific dev agent | Implementation |
| TESTS | @test-writer | Domain tests |
| REVIEW | @code-reviewer | Final quality check |
development
Test-Driven Development workflow. Use when doing TDD, writing tests first, or when user says "tdd", "test first", "test driven", "red green refactor".
development
Thorough pull request review workflow with architecture compliance checks. Use when reviewing pull requests, checking code changes, or when user says "review pr", "check pr", "review code", "pr review", "review pull request".
development
Review local branch changes for architecture compliance, conventions, and code quality before pushing/PR. Stack-aware — detects the project stack and applies the matching rules. Use when user says "review changes", "review branch", "check branch", "check changes", "review my code", "review before pr".
testing
DDD architecture compliance review with automated checks and review loop. Use when user says "architect-review", "architecture review", "review architecture", "check architecture", "review ddd", "ddd review".