nbl.refactor-clean
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRefactor 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依赖 | |
| SpotBugs | 静态代码分析 | |
| PMD | 代码质量检查+重复代码 | |
| JaCoCo | 测试覆盖率分析 | |
| Checkstyle | 代码规范检查 | |
如果没有专用工具,使用Grep查找零引用的公开方法:
bash
undefinedRun analysis tools according to project type:
| Tool | Detection Content | Command |
|---|---|---|
| Maven Dependency | Unused Maven dependencies | |
| SpotBugs | Static code analysis | |
| PMD | Code quality check + duplicate code check | |
| JaCoCo | Test coverage analysis | |
| Checkstyle | Code specification 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"
undefinedgrep -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:
| Level | Example | Action |
|---|---|---|
| Safe | Unused utility methods, private methods, constants | Can be safely deleted |
| Caution Required | Service methods, Controller methods, DTO classes | Verify there are no dynamic calls or external consumers |
| High Risk | Feign interfaces, Entity classes, Enum classes | Make decision after thorough investigation |
| Prohibited | Public interfaces in api modules, configuration classes | Deletion is not allowed |
步骤3:安全删除循环
Step 3: Secure Deletion Cycle
对于每个安全级别的项:
- 运行完整测试套件 — 建立基线(全部通过)
bash
mvn clean test - 删除死代码 — 使用Edit工具精确移除
- 重新运行测试 — 验证没有破坏
bash
mvn test - 如果测试失败 — 立即回滚 并跳过此项
git checkout -- <file> - 如果测试通过 — 移至下一项
For each Safe level item:
- Run the full test suite — Establish a baseline (all tests pass)
bash
mvn clean test - Delete dead code — Use editing tools for precise removal
- Re-run tests — Verify no functionality is broken
bash
mvn test - If tests fail — Roll back immediately with and skip this item
git checkout -- <file> - 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
undefinedVerify no layered architecture violation before deletion:
bash
undefinedController不能直接调用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"
undefinedgrep -r "QueryWrapper|LambdaQueryWrapper" --include="*ServiceImpl.java"
undefinedMaven依赖清理
Maven Dependency Cleanup
bash
undefinedbash
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
undefinedundefined核心规则
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