Refactor and reorganize codebases using SOLID principles, clean architecture, strict naming conventions, and defensive programming. Trigger with "refactor this codebase", "reorganize the code", "clean up the project structure", "fix the naming", "audit the codebase", "code organization", or when someone needs help restructuring, renaming, deduplicating, or improving code quality across a project.
インストール
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
Refactor and reorganize codebases using SOLID principles, clean architecture, strict naming conventions, and defensive programming. Trigger with "refactor this codebase", "reorganize the code", "clean up the project structure", "fix the naming", "audit the codebase", "code organization", or when someone needs help restructuring, renaming, deduplicating, or improving code quality across a project.
when_to_use
Refactoring or reorganizing a codebase with SOLID and clean architecture; auditing naming conventions and cleaning up project structure; consolidating duplicated logic and establishing shared modules; improving async error handling and modularity
allowed-tools
Read, Glob, Grep, Bash
Codebase Refactoring & Organization
A complete methodology for auditing, refactoring, and reorganizing codebases. Every decision must be filtered through the principles below - they are constraints, not suggestions. When two principles conflict, the order listed here represents priority.
1. Guiding Principles
SOLID Principles
Single Responsibility (SRP): Every module, class, function, and component must have exactly one reason to change. If you can describe what something does using the word "and," it does too much - split it.
Open/Closed (OCP): Code should be open for extension but closed for modification. New features should be addable by writing new code (new modules, handlers, components), not by editing existing stable code. Use plugin patterns, strategy patterns, and configuration-driven behavior.
Liskov Substitution (LSP): Any subclass or implementation must be usable wherever its parent or interface is expected without breaking behavior.
Interface Segregation (ISP): No module should depend on methods or properties it doesn't use. Prefer many small, focused interfaces over one large general-purpose one.
Dependency Inversion (DIP): High-level business logic must never depend on low-level implementation details. Both should depend on abstractions.
Core Philosophies
KISS: The simplest solution that correctly solves the problem is the best solution. If a junior developer can't understand a module within 5 minutes, it's too complex.
YAGNI: Do not build abstractions or extension points for hypothetical future requirements. Solve today's problem today.
DRY: Every piece of knowledge must have a single, unambiguous, authoritative representation. But apply with judgment - two pieces of code that look similar but serve different domains and change for different reasons are NOT duplication.
Separation of Concerns: Each layer, module, and function should address one concern. If removing one concern would require rewriting a module, the concerns are too entangled.
Composition Over Inheritance: Build complex behavior by combining simple, independent pieces rather than extending deep class hierarchies.
Principle of Least Surprise: Code should behave the way a reasonable developer would expect from reading its name and signature.
Fail Fast, Fail Loud: Detect errors at the earliest possible point and surface them immediately. Never let invalid state propagate silently.
Convention Over Configuration: Establish clear patterns and follow them everywhere.
Boy Scout Rule: Leave every file you touch cleaner than you found it.
Defensive Programming
Validate at boundaries: All external input must be validated and sanitized before entering business logic.
Use type systems aggressively: Leverage strict mode, type hints with runtime validators (Pydantic, Zod, etc.). Avoid any, object, untyped function signatures.
Make illegal states unrepresentable: Design data structures so invalid combinations cannot exist. Use discriminated unions, enums, and required fields.
Prefer immutability: Use const, readonly, frozen data structures by default. Mutate only with clear justification, contained to the smallest scope.
2. Naming & Nomenclature Standards
The Cardinal Rule
Names must describe what something IS, not how it compares to something that came before. Names are read thousands of times by developers with no knowledge of history. The name must stand entirely on its own.
Banned Naming Patterns
These are strictly prohibited in file names, folder names, function names, class names, variable names, component names, and any other identifier. If any exist, they must be renamed.
Banned Pattern
Why It's Harmful
Correct Alternative
EnhancedWidget / ImprovedWidget
Implies a non-enhanced version exists. Enhanced compared to what?
Name for what it does: WidgetWithValidation
NewUserService / OldUserService
"New" is only new today. Which one is active?
UserService. For migrations: UserServiceV1 + UserServiceV2 with deprecation timeline
BetterParser / FastParser / SmartParser
Subjective, relative, unverifiable
Name the mechanism: StreamingParser, BatchParser, RecursiveDescentParser
utils2.js / helpers_new.py / styles_final.css
Versioned filenames signal fear of replacing the original
One file, one name. utils.js. Delete or merge the old one
handleClickNew() / processDataV2()
Creates ghost references developers hunt for
handleClick(). Or name the behavioral difference: processDataInBatches()
temp_, test_ (non-test), my_, foo, bar, xxx
Non-descriptive placeholders in production
Name for what it does: extractInvoiceLineItems()
data, info, item, thing, stuff, obj, val, result (standalone)
Semantically empty - every variable holds data
Name the domain concept: invoice, userProfile, authToken
The same concept must use the same noun everywhere: backend, frontend, database, API, types, docs. Maintain a domain glossary at docs/glossary.md mapping business terms to canonical code names.
Every folder must have an index/barrel file explicitly exporting its public API
No files at project root except configs and documentation
If a file doesn't clearly belong to frontend/ or backend/, it belongs in shared/ or scripts/
Group by feature/domain, not by technical role: prefer features/auth/ containing its components, hooks, services, and tests over separate top-level components/, hooks/, services/ folders
Mirrored Naming Conventions
Concept
Frontend Path
Backend Path
User auth
frontend/src/features/auth/
backend/src/features/auth/
API client / route
frontend/src/api/users.ts
backend/src/routes/users.py
Type definitions
frontend/src/types/user.ts
backend/src/schemas/user.py
Validation logic
frontend/src/validation/user.ts
backend/src/validation/user.py
Shared constants
shared/constants/roles.ts
shared/constants/roles.py
File naming: lowercase-kebab-case for non-Python, lowercase_snake_case for Python, PascalCase for components.
4. Eliminating Duplication
For every instance of duplicated or near-duplicated logic:
Identify - flag every function, component, class, type, or constant appearing in multiple places
Consolidate - extract to a single, well-named module in the appropriate location:
Same-side duplication -> utils/, hooks/, or services/ within that side
Cross-side duplication -> shared/
Re-export - update all consumers to import from the single source
Validate - confirm no orphaned copies remain
Shared types between frontend and backend must live in shared/types/. Pure utility functions belong in shared/utils/.
5. Modularity & Scalability
Frontend
One component does one thing (SRP)
Separate presentational components (rendering) from containers (data fetching, state)
Extract business logic into custom hooks or service modules - components are thin wrappers
Configuration and feature flags are externalized, never hardcoded