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
Added on

NPX Install

npx skill4agent add icefrag/nbl-superpowers nbl.refactor-clean

Tags

Translated version includes tags in frontmatter

SKILL.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:
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
# 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:
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

Step 3: Secure Deletion Cycle

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

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 declared

Core 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