Java best practices
Skill ComeOnOliver/skillshub/skills/HoangNguyen0403/agent-skills-standard/java-best-practices
🧠The right skill, one API call. AI agent skills registry with token-efficient skill resolution. 5,000+ skills from 500+ top repos.
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.
What its author says it does
Copied from the file, not written here
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)
SKILL.md
1.9 KB, 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).