refactoring-analysis
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRefactoring Analysis
重构分析
Perform a systematic analysis of a codebase to identify refactoring opportunities based on
Martin Fowler's "Refactoring: Improving the Design of Existing Code" (2nd Edition).
Produce a prioritized report with actionable findings.
基于Martin Fowler的《重构:改善既有代码的设计(第二版)》对代码库进行系统性分析,识别可重构的机会,输出包含可落地发现项的优先级排序报告。
Procedures
操作流程
Step 1: Scope the Analysis
- Determine the analysis target — a specific directory, module, feature area, or the entire project. If the user did not specify, ask which area to focus on.
- Identify the project's language and paradigm (OOP, functional, mixed) to calibrate which smells and techniques are applicable.
- If the project follows domain-driven design (DDD) or hexagonal architecture, read
for additional SOLID-specific analysis criteria.
references/solid-ddd-context.md
Step 2: Explore the Codebase
- Map the directory structure and identify key modules, entry points, and shared utilities.
- Read the most critical files (entry points, core business logic, shared modules).
- Identify the project's conventions: naming, file organization, test patterns, dependency injection approach.
Step 3: Detect Code Smells
Read for the full catalog of detectable smells.
references/code-smells-catalog.mdSystematically scan for the following smell categories, in priority order:
- Bloaters — Long Functions (>15 lines of logic), Large Classes/Modules (>300 lines), Long Parameter Lists (>3 params), Data Clumps, Primitive Obsession.
- Change Preventers — Divergent Change (one module changed for multiple unrelated reasons), Shotgun Surgery (one change touches 5+ files).
- Dispensables — Duplicated Code, Dead Code, Speculative Generality, Lazy Elements, Comments as Deodorant.
- Couplers — Feature Envy, Insider Trading (excessive data sharing between modules), Message Chains (>2 levels deep), Middle Man (>50% delegation).
- Conditional Complexity — Nested Conditionals (>2 levels), Repeated Switches, Missing Guard Clauses, Complex Boolean Expressions.
- DRY Violations — Near-identical code blocks, copy-pasted logic with minor variations, repeated parameter groups, duplicated constants or magic numbers.
For each detected smell:
- Record the exact file path and line range
- Classify the smell type (from the catalog)
- Assess severity: |
critical|high|mediumlow - Note the impact on maintainability, readability, or change cost
Step 4: Map Refactoring Opportunities
Read for the full technique catalog.
references/refactoring-techniques.mdFor each detected smell, identify the recommended refactoring technique(s):
| Smell | Primary Technique |
|---|---|
| Long Function | Extract Function, Decompose Conditional |
| Duplicated Code | Extract Function, Pull Up Method |
| Long Parameter List | Introduce Parameter Object, Preserve Whole Object |
| Feature Envy | Move Function |
| Data Clumps | Extract Class, Introduce Parameter Object |
| Primitive Obsession | Replace Primitive with Object |
| Large Class/Module | Extract Class, Extract Module |
| Repeated Switches | Replace Conditional with Polymorphism |
| Message Chains | Hide Delegate, Extract Function |
| Nested Conditionals | Replace with Guard Clauses |
| Dead Code | Remove Dead Code |
| Magic Numbers/Strings | Extract Constant |
| Mutable Shared State | Encapsulate Variable, Split Variable |
| Imperative Loops | Replace Loop with Pipeline (map/filter/reduce) |
For each opportunity, draft a concrete before/after code sketch showing the transformation.
Step 5: Assess Coupling and Cohesion
- Identify modules with high afferent coupling (many dependents — risky to change).
- Identify modules with high efferent coupling (many dependencies — fragile).
- Flag circular dependencies between modules.
- Assess cohesion — modules that mix unrelated responsibilities are candidates for Extract Class or Split Phase.
Step 6: Evaluate DRY Opportunities
- Search for near-duplicate code blocks (>5 lines of similar structure).
- Identify repeated constant values (magic numbers, repeated string literals).
- Find repeated parameter patterns across function signatures.
- Look for copy-pasted logic with minor variations that could be parameterized.
- Propose extraction strategies: shared functions, constants files, parameter objects.
Step 7: SOLID Analysis (Domain Projects Only)
<GATE>
Only perform this step if the project uses domain-driven design (DDD), hexagonal/clean
architecture, or explicitly models a complex business domain. SOLID principles have the
most impact in domain-rich, object-oriented codebases. For simple CRUD apps, utility
libraries, or purely functional codebases, skip this step and note in the report that
SOLID analysis was not applicable.
</GATE>
Read for detailed guidance.
references/solid-ddd-context.mdEvaluate against SOLID principles with domain-project focus:
- SRP: Classes/modules with multiple reasons to change
- OCP: Areas requiring modification (not extension) for new variants
- LSP: Subclasses that override to throw or no-op (Refused Bequest smell)
- ISP: Interfaces forcing implementers to stub unused methods
- DIP: High-level modules importing low-level implementations directly
Step 8: Prioritize and Generate Report
- Read for the output template.
assets/refactoring-report-template.md - Rank all findings by a priority score combining:
- Impact: How much does this hurt readability, maintainability, or change cost?
- Frequency: How often is this pattern encountered in the codebase?
- Effort: How much work to refactor? (low effort + high impact = do first)
- Group findings into priority tiers:
- P0 — Critical: Blocking future development, causing bugs, or high coupling
- P1 — High: Significant maintenance burden, frequent pain point
- P2 — Medium: Noticeable but manageable, worth addressing opportunistically
- P3 — Low: Minor improvements, cosmetic, litter-pickup candidates
- Generate the report following the template and save to:
where
docs/_refacs/<YYYYMMDD>-<slug>.mdis a lowercase-hyphenated summary (e.g.,<slug>).auth-module-cleanup - Create the directory if it does not exist.
docs/_refacs/
Step 9: Present Summary
- Present a brief executive summary to the user with:
- Total findings count by severity
- Top 3-5 highest-impact opportunities
- Suggested refactoring order (quick wins first, then high-impact)
- Estimated complexity tier for each (trivial / moderate / significant)
- Ask the user if they want to proceed with any specific refactoring.
步骤1:确定分析范围
- 明确分析目标:指定目录、模块、功能域或整个项目。如果用户未指定,询问需要聚焦的分析区域。
- 识别项目使用的语言和编程范式(OOP、函数式、混合范式),校准适用的代码异味和重构技术。
- 如果项目遵循领域驱动设计(DDD)或六边形架构,阅读获取额外的SOLID专属分析标准。
references/solid-ddd-context.md
步骤2:探查代码库
- 梳理目录结构,识别核心模块、入口点和公共工具库。
- 阅读最高优先级文件:入口文件、核心业务逻辑、公共模块。
- 识别项目约定:命名规则、文件组织方式、测试模式、依赖注入实现方式。
步骤3:检测代码异味
阅读获取完整的可检测代码异味目录。
references/code-smells-catalog.md按优先级顺序系统性扫描以下代码异味类别:
- 膨胀类 — 过长函数(逻辑超过15行)、过大类/模块(超过300行)、过长参数列表(超过3个参数)、数据泥团、基本类型偏执。
- 变更阻碍类 — 发散式变化(一个模块会因多个不相关的原因被修改)、霰弹式修改(一个变更需要修改5个以上文件)。
- 冗余类 — 重复代码、死代码、投机性泛化、冗余元素、“除臭剂”式注释。
- 耦合类 — 依恋情结、内幕交易(模块间过度共享数据)、消息链(超过2层深度)、中间人(超过50%逻辑为委托调用)。
- 条件复杂度类 — 嵌套条件(超过2层)、重复Switch语句、缺失卫语句、复杂布尔表达式。
- DRY原则违反类 — 近乎完全相同的代码块、仅做了微小改动的复制粘贴逻辑、重复的参数组、重复的常量或魔法数字。
针对每个检测到的代码异味:
- 记录准确的文件路径和行范围
- 对异味类型进行分类(参考目录)
- 评估严重程度:|
critical|high|mediumlow - 标注对可维护性、可读性或变更成本的影响
步骤4:映射重构机会
阅读获取完整的重构技术目录。
references/refactoring-techniques.md针对每个检测到的代码异味,匹配推荐的重构技术:
| 代码异味 | 首选技术 |
|---|---|
| 过长函数 | 提取函数、分解条件语句 |
| 重复代码 | 提取函数、函数上移 |
| 过长参数列表 | 引入参数对象、保持对象完整 |
| 依恋情结 | 移动函数 |
| 数据泥团 | 提取类、引入参数对象 |
| 基本类型偏执 | 用对象替换基本类型 |
| 过大类/模块 | 提取类、提取模块 |
| 重复Switch语句 | 用多态替换条件语句 |
| 消息链 | 隐藏委托关系、提取函数 |
| 嵌套条件 | 替换为卫语句 |
| 死代码 | 移除死代码 |
| 魔法数字/字符串 | 提取常量 |
| 可变共享状态 | 封装变量、拆分变量 |
| 命令式循环 | 用管道(map/filter/reduce)替换循环 |
针对每个重构机会,草拟具体的代码前后对比示例,展示转换逻辑。
步骤5:评估耦合与内聚
- 识别高入向耦合的模块(有大量依赖方,变更风险高)。
- 识别高出向耦合的模块(依赖大量其他模块,稳定性差)。
- 标记模块间的循环依赖。
- 评估内聚性:混合了不相关职责的模块可作为提取类或阶段拆分的候选。
步骤6:评估DRY优化机会
- 搜索近乎重复的代码块(超过5行结构相似的代码)。
- 识别重复的常量值(魔法数字、重复的字符串字面量)。
- 查找跨函数签名的重复参数模式。
- 寻找仅做了微小改动、可通过参数化复用的复制粘贴逻辑。
- 提出提取策略:公共函数、常量文件、参数对象。
步骤7:SOLID分析(仅领域项目适用)
<GATE>
仅当项目使用领域驱动设计(DDD)、六边形/整洁架构,或明确对复杂业务域进行建模时才执行此步骤。SOLID原则在领域丰富的面向对象代码库中收益最高。对于简单的CRUD应用、工具库或纯函数式代码库,跳过此步骤,并在报告中注明SOLID分析不适用。
</GATE>
阅读获取详细指导。
references/solid-ddd-context.md针对领域项目的特点评估SOLID原则遵循情况:
- SRP(单一职责原则):存在多个变更理由的类/模块
- OCP(开闭原则):需要修改(而非扩展)来支持新变体的区域
- LSP(里氏替换原则):重写后抛出异常或无操作的子类(拒绝继承异味)
- ISP(接口隔离原则):强制实现类编写未使用方法存根的接口
- DIP(依赖倒置原则):直接导入低层实现的高层模块
步骤8:优先级排序与生成报告
- 阅读获取输出模板。
assets/refactoring-report-template.md - 结合以下维度计算优先级分数,对所有发现项排序:
- 影响:该问题对可读性、可维护性或变更成本的负面影响程度?
- 出现频率:该模式在代码库中出现的频次?
- 改造成本:重构需要的工作量?(低工作量+高影响=优先处理)
- 将发现项归入不同优先级层级:
- P0 — 严重:阻塞后续开发、导致bug或存在高耦合问题
- P1 — 高:带来显著的维护负担,是高频痛点
- P2 — 中:问题明显但可控,适合伺机处理
- P3 — 低:小幅优化、表面化问题,属于随手清理的候选
- 遵循模板生成报告并保存至:
其中
docs/_refacs/<YYYYMMDD>-<slug>.md为小写连字符分隔的概要(例如<slug>)。auth-module-cleanup - 如果目录不存在则创建。
docs/_refacs/
步骤9:展示总结
- 向用户展示简要的执行摘要,包含:
- 按严重程度划分的发现项总数
- 优先级最高的3-5个重构机会
- 建议的重构顺序(先快速赢,再高影响项)
- 每个项的预估复杂度等级(简单/中等/高)
- 询问用户是否想要推进任何特定的重构工作。
Error Handling
错误处理
- If the analysis target is too broad (>50 files), ask the user to narrow scope or confirm they want a high-level scan with sampling.
- If the project has no tests, warn that refactoring without test coverage is risky and recommend adding tests for critical paths before refactoring.
- If the project uses an unfamiliar framework or pattern, note this limitation in the report rather than guessing.
- If a smell is ambiguous (could be intentional design), flag it as "potential" and note the context that might justify the current structure.
- 如果分析目标范围过广(超过50个文件),请用户缩小范围,或确认是否需要采用抽样方式进行高层扫描。
- 如果项目没有测试,警告用户无测试覆盖的重构存在风险,建议在重构前为核心路径补充测试。
- 如果项目使用了不熟悉的框架或模式,在报告中注明该限制,不要做猜测性判断。
- 如果某个异味存在歧义(可能是有意设计),将其标记为“潜在”问题,并注明可能支持当前结构的上下文理由。