java-best-practices
Installation
SKILL.md
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 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
Related skills
More from hoangnguyen0403/agent-skills-standard
skill creator
Standards for creating new High-Density Agent Skills with optimal token economy.
181java-language
Modern Java 21+ standards including Records, Pattern Matching, and Virtual Threads. Use when working with Java records, sealed classes, switch expressions, text blocks, Optional, or upgrading from older Java versions. (triggers: **/*.java, pom.xml, build.gradle, record, sealed, switch, var, Optional, stream, VirtualThread, instanceof, text block)
1swift swiftui
Standards for State Management, View Lifecycle, and Property Wrappers
1swift tooling
Standards for SPM, Build Configs, and Code Quality
1swift testing
Standards for XCTest, Async Tests, and Test Organization
1swift language
Standards for Optionals, Protocols, Extensions, and Type Safety
1