java-best-practices
Installation
SKILL.md
Java Best Practices
Priority: P1 (HIGH)
Implementation Guidelines
- Immutability: Prefer immutable objects (
finalfields, unmodifiable collections). - Access Modifiers: Minimize visibility. Default to package-private (no modifier). Use
privatefor all fields. Onlypublicfor API contracts. - Composition > Inheritance: Favor
Has-AoverIs-A. Avoid deep hierarchies. - Constructors: Use Static Factory Methods (
User.of()) over complex constructors. - Builder Pattern: Use for objects with 4+ parameters.
- Exceptions: Recoverable → Checked; Programming error → Unchecked.
- Fail Fast: Validate parameters (
Objects.requireNonNull) at method start. - Interfaces: Code to interfaces (
List,Map), not implementations (ArrayList). - Dependency Injection: Inject dependencies via constructor; don't create them internally.
- Method References: Use
String::toUpperCaseovers -> s.toUpperCase()where readable.