Implementing Code Quality Practices
1. Implementing Code Reviews
| Practice | Detail |
| Small PRs | < 400 lines preferred |
| Checklist | Tests, security, perf, docs |
| Two reviewers | For sensitive changes |
| Author tests own code | Reviewer verifies tests |
| Tone | Suggest, don't command |
2. Implementing Static Analysis
| Tool | Detects |
| SpotBugs | Bug patterns |
| PMD | Code smells, style |
| Checkstyle | Format conventions |
| SonarQube | Aggregated quality + security |
| Error Prone | Compile-time bug catching |
3. Implementing Code Coverage
| Target | Reasonable Range |
| Domain logic | ≥ 80% |
| Controllers / glue | 50-70% |
| Total | 70-80% pragmatic |
| Tool | JaCoCo |
Warning: 100% coverage doesn't mean correct — focus on assertions, not lines hit.
4. Implementing Linting Rules
| Stack | Tool |
| Java | Checkstyle, Spotless |
| Format | google-java-format, Spotless |
| Pre-commit | Reject unformatted code |
| CI gate | Fail PR on violation |
5. Implementing Documentation Standards
| What | Where |
| Public APIs | Javadoc; @param, @return, @throws |
| Architecture | ADRs (Architecture Decision Records) |
| Onboarding | README + run book |
| Don't document | Obvious code; rename instead |
Example: Spotless config (Gradle)
spotless {
java {
googleJavaFormat("1.22.0")
removeUnusedImports()
importOrder("java", "javax", "org", "com", "")
trimTrailingWhitespace()
endWithNewline()
}
}
7. Implementing Code Complexity Limits
| Metric | Threshold |
| Cyclomatic complexity | ≤ 10 per method |
| Method length | ≤ 30-50 lines |
| Class length | ≤ 300-500 lines |
| Param count | ≤ 5 (use object) |
| Cognitive complexity | SonarQube ≤ 15 |
8. Implementing Refactoring Patterns
| Pattern | Use |
| Extract Method | Break long methods |
| Extract Class | Split large class by responsibility |
| Replace Conditional with Polymorphism | Eliminate type switches |
| Introduce Parameter Object | Group related params |
| Strangler Fig | Replace legacy gradually |
9. Implementing Technical Debt Tracking
| Practice | Detail |
| // TODO with ticket | Link to issue |
| Debt board | Visible backlog |
| Budget | ~ 20% of capacity |
| SonarQube | Tracks debt ratio over time |
10. Implementing Best Practices
| Principle | Detail |
| DRY | Don't repeat yourself |
| KISS | Keep it simple |
| YAGNI | You ain't gonna need it |
| Boy Scout rule | Leave cleaner than found |
| Fail fast | Surface errors early |
11. Implementing Code Smells Detection
| Smell | Fix |
| Long method | Extract method |
| God class | Extract class |
| Feature envy | Move method to data's class |
| Primitive obsession | Value object |
| Switch on type | Polymorphism / sealed |
| Shotgun surgery | Consolidate change point |
12. Implementing Code Metrics
| Metric | Signal |
| LOC | Size proxy (weak) |
| Cyclomatic / cognitive | Complexity |
| Coupling (CBO) | Inter-class dependencies |
| Cohesion (LCOM) | How focused a class is |
| Duplications | SonarQube duplication % |
| Code churn | Hot spots = bug-prone |