Java best practices
Skill ComeOnOliver/skillshub/skills/HoangNguyen0403/agent-skills-standard/java-best-practices
Core Effective Java patterns for robust, maintainable Java code. Use when applying SOLID principles, choosing between inheritance and composition, refactoring Java code smells, or reviewing class design. (triggers: **/*.java, refactor, SOLID, builder, factory, composition, immutable, Optional, checked exception, clean code)From its SKILL.md
npx -y skills add ComeOnOliver/skillshub --skill java-best-practicesAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
SKILL.md
1.9 KB, 324 tokens by cl100k_base, as published. Nobody here has run it
Java Best Practices
Priority: P1 (HIGH)
Core engineering principles for robust, maintainable Java systems.
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 the 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.
Anti-Patterns
- No Null Returns: Return Optional<T> or empty collection instead.
- No Empty Catch: Log or rethrow; never swallow exceptions silently.
- No God Class: Split into focused classes following Single Responsibility Principle.
- No Magic Numbers: Extract named constants with clear meaning.
- No Mutable Statics: Avoid public static mutable fields (global state).
References
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.