nbl.refactor-clean
Original:🇨🇳 Chinese
Translated
Java Web dead code cleanup and refactoring expert. It safely identifies and removes dead code, with testing and verification performed at every step. Trigger conditions: User requests for dead code cleanup, refactoring optimization, and unused code removal.
12installs
Sourceicefrag/nbl-superpowers
Added on
NPX Install
npx skill4agent add icefrag/nbl-superpowers nbl.refactor-cleanTags
Translated version includes tags in frontmatterSKILL.md Content (Chinese)
View Translation Comparison →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
Step 1: Detect Dead Code
Run 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
# Find class definitions, then check if they are referenced
grep -r "ClassName" --include="*.java" | grep -v "ClassName.java"Step 2: Classify Detection Results
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 |
Step 3: Secure Deletion Cycle
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
Step 4: Process Caution Required Level Items
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)
Step 5: Merge Duplicate Code
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
Step 6: Summary
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 Project Specific Rules
Prohibited Deletions
- 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
Verify no layered architecture violation before deletion:
bash
# Controller cannot directly call Mapper
grep -r "Mapper" --include="*Controller.java" | grep -v "//"
# Service layer is prohibited from directly using QueryWrapper
grep -r "QueryWrapper\|LambdaQueryWrapper" --include="*ServiceImpl.java"Maven Dependency Cleanup
bash
# Analyze unused dependencies
mvn dependency:analyze
# Pay attention to the output:
# - Unused declared dependencies: Can be safely removed
# - Used undeclared dependencies: Need to be explicitly declaredCore Rules
- 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