nbl.refactor-clean

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Refactor Clean Skill

Refactor Clean Skill

安全识别并移除死代码,每一步都进行测试验证。
Securely identify and remove dead code, with testing and verification conducted at each step.

激活时机

Activation Timing

  • 用户请求清理死代码
  • 代码维护优化
  • 移除未使用代码
  • User requests dead code cleanup
  • Code maintenance and optimization
  • Remove unused code

步骤1:检测死代码

Step 1: Detect Dead Code

根据项目类型运行分析工具:
工具检测内容命令
Maven Dependency未使用的Maven依赖
mvn dependency:analyze
SpotBugs静态代码分析
mvn spotbugs:check
PMD代码质量检查+重复代码
mvn pmd:check
JaCoCo测试覆盖率分析
mvn test jacoco:report
Checkstyle代码规范检查
mvn checkstyle:check
如果没有专用工具,使用Grep查找零引用的公开方法:
bash
undefined
Run analysis tools according to project type:
ToolDetection ContentCommand
Maven DependencyUnused Maven dependencies
mvn dependency:analyze
SpotBugsStatic code analysis
mvn spotbugs:check
PMDCode quality check + duplicate code check
mvn pmd:check
JaCoCoTest coverage analysis
mvn test jacoco:report
CheckstyleCode specification check
mvn checkstyle:check
If no dedicated tools are available, use Grep to find public methods with zero references:
bash
undefined

查找类定义,然后检查是否被引用

Find class definitions, then check if they are referenced

grep -r "ClassName" --include="*.java" | grep -v "ClassName.java"
undefined
grep -r "ClassName" --include="*.java" | grep -v "ClassName.java"
undefined

步骤2:分类检测结果

Step 2: Classify Detection Results

将检测结果按安全等级分类:
等级示例操作
安全未使用的工具方法、私有方法、常量可放心删除
谨慎Service方法、Controller方法、DTO类验证无动态调用或外部消费者
危险Feign接口、Entity类、枚举类调查后再决定
禁止api模块的公开接口、配置类不允许删除
Classify the detection results by security level:
LevelExampleAction
SafeUnused utility methods, private methods, constantsCan be safely deleted
Caution RequiredService methods, Controller methods, DTO classesVerify there are no dynamic calls or external consumers
High RiskFeign interfaces, Entity classes, Enum classesMake decision after thorough investigation
ProhibitedPublic interfaces in api modules, configuration classesDeletion is not allowed

步骤3:安全删除循环

Step 3: Secure Deletion Cycle

对于每个安全级别的项:
  1. 运行完整测试套件 — 建立基线(全部通过)
    bash
    mvn clean test
  2. 删除死代码 — 使用Edit工具精确移除
  3. 重新运行测试 — 验证没有破坏
    bash
    mvn test
  4. 如果测试失败 — 立即回滚
    git checkout -- <file>
    并跳过此项
  5. 如果测试通过 — 移至下一项
For each Safe level item:
  1. Run the full test suite — Establish a baseline (all tests pass)
    bash
    mvn clean test
  2. Delete dead code — Use editing tools for precise removal
  3. Re-run tests — Verify no functionality is broken
    bash
    mvn test
  4. If tests fail — Roll back immediately with
    git checkout -- <file>
    and skip this item
  5. If tests pass — Proceed to the next item

步骤4:处理谨慎级别项

Step 4: Process Caution Required Level Items

删除谨慎级别项之前:
  • 搜索动态调用:反射
    Class.forName()
    Method.invoke()
  • 搜索字符串引用:配置文件中的类名、Spring Bean名称
  • 检查是否被Feign接口导出
  • 检查api模块中是否有对应的Req/Resp/Query对象引用
  • 验证无其他微服务调用(检查api模块接口)
Before deleting Caution Required level items:
  • Search for dynamic calls: Reflection
    Class.forName()
    ,
    Method.invoke()
  • Search for string references: Class names in configuration files, Spring Bean names
  • Check if they are exported by Feign interfaces
  • Check if there are corresponding Req/Resp/Query object references in the api module
  • Verify no other microservice calls exist (check api module interfaces)

步骤5:合并重复代码

Step 5: Merge Duplicate Code

移除死代码后,检查:
  • 相似方法(>80%相似)— 合并为一个
  • 重复的DTO转换逻辑 — 使用MapStruct或提取工具方法
  • 无价值的包装方法 — 直接内联调用
  • 多余的中间层 — 移除不必要的委托
After removing dead code, check for:
  • Similar methods (>80% similarity) — Merge into one
  • Duplicate DTO conversion logic — Use MapStruct or extract utility methods
  • Valueless wrapper methods — Use direct inline calls
  • Redundant middle layers — Remove unnecessary delegates

步骤6:总结

Step 6: Summary

输出结果报告:
死代码清理报告
──────────────────────────────
已删除:   12个未使用方法
          3个未使用类
          5个未使用依赖
已跳过:   2项(测试失败)
节省:     约450行代码
──────────────────────────────
编译通过 ✅
测试通过 ✅
覆盖率: 85% ✅
Output the result report:
Dead Code Cleanup Report
──────────────────────────────
Deleted:   12 unused methods
          3 unused classes
          5 unused dependencies
Skipped:   2 items (test failed)
Saved:     Approximately 450 lines of code
──────────────────────────────
Compilation passed ✅
Test passed ✅
Coverage: 85% ✅

Java Web项目特定规则

Java Web Project Specific Rules

禁止删除

Prohibited Deletions

  • api模块的Feign接口 — 可能被其他微服务调用
  • Entity实体类 — MyBatis-Plus动态使用
  • 枚举类(Enum后缀) — 可能被序列化/反序列化
  • Mapper接口 — XML中的SQL可能动态调用
  • Spring配置类 — 影响应用启动
  • Feign interfaces in api modules — May be called by other microservices
  • Entity classes — Dynamically used by MyBatis-Plus
  • Enum classes (suffixed with Enum) — May be used for serialization/deserialization
  • Mapper interfaces — SQL in XML may make dynamic calls
  • Spring configuration classes — Affect application startup

分层架构检查

Layered Architecture Check

删除前验证是否违反分层:
bash
undefined
Verify no layered architecture violation before deletion:
bash
undefined

Controller不能直接调用Mapper

Controller cannot directly call Mapper

grep -r "Mapper" --include="*Controller.java" | grep -v "//"
grep -r "Mapper" --include="*Controller.java" | grep -v "//"

Service层禁止直接使用QueryWrapper

Service layer is prohibited from directly using QueryWrapper

grep -r "QueryWrapper|LambdaQueryWrapper" --include="*ServiceImpl.java"
undefined
grep -r "QueryWrapper|LambdaQueryWrapper" --include="*ServiceImpl.java"
undefined

Maven依赖清理

Maven Dependency Cleanup

bash
undefined
bash
undefined

分析未使用的依赖

Analyze unused dependencies

mvn dependency:analyze
mvn dependency:analyze

关注输出:

Pay attention to the output:

- Unused declared dependencies: 可安全移除

- Unused declared dependencies: Can be safely removed

- Used undeclared dependencies: 需要显式声明

- Used undeclared dependencies: Need to be explicitly declared

undefined
undefined

核心规则

Core Rules

  • 禁止不运行测试就删除
  • 一次只删一个 — 原子变更使回滚更容易
  • 不确定就跳过 — 保留死代码比破坏生产环境好
  • 清理与重构分开 — 先清理,后重构
  • 每批次后编译验证
    mvn compile
  • Prohibit deletion without running tests first
  • Delete only one item at a time — Atomic changes make rollback easier
  • Skip if uncertain — Keeping dead code is better than breaking the production environment
  • Separate cleanup and refactoring — Clean up first, then refactor
  • Compile and verify after each batch
    mvn compile