> java-best-practices
Core engineering principles inspired by Effective Java and Clean Code. Use when applying Effective Java patterns, SOLID principles, or clean code practices in Java. (triggers: **/*.java, refactor, clean code, smells, patterns, design)
curl "https://skillshub.wtf/HoangNguyen0403/agent-skills-standard/java-best-practices?format=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: Use Checked vs Unchecked wisely. Recoverable -> Checked; Programming Error -> Unchecked.
- Fail Fast: Validate parameters (
Objects.requireNonNull) at the method start. - Interfaces: Code to interfaces (
List,Map), not implementations (ArrayList,HashMap). - Dependency Injection: Invert control. Inject dependencies via constructor.
- Method References: Use
String::toUpperCaseovers -> s.toUpperCase()where readable.
Anti-Patterns
- Return Null: Returns empty
Optionalor Collection instead. - Empty Catch: Never swallow exceptions. Log or rethrow.
- God Class: Single Responsibility Principle. Break it down.
- Magic Numbers: Extract constants with meaningful names.
- Mutable Statics: Avoid
public staticfields (Global state).
Code
// Static Factory + Builder (Implicit via Library or Manual)
public class Pizza {
private final int size;
private final boolean cheese;
private Pizza(Builder b) { ... }
public static Pizza of(int size) {
return new Pizza(size, false);
}
}
// Composition
public class Service {
private final Repository repo; // Injected
public Service(Repository repo) {
this.repo = Objects.requireNonNull(repo);
}
}
Related Topics
language | concurrency | tooling
> related_skills --same-repo
> typescript-tooling
Development tools, linting, and build config for TypeScript. Use when configuring ESLint, Prettier, Jest, Vitest, tsconfig, or any TS build tooling. (triggers: tsconfig.json, .eslintrc.*, jest.config.*, package.json, eslint, prettier, jest, vitest, build, compile, lint)
> typescript-security
Secure coding practices for TypeScript. Use when validating input, handling auth tokens, sanitizing data, or managing secrets and sensitive configuration. (triggers: **/*.ts, **/*.tsx, validate, sanitize, xss, injection, auth, password, secret, token)
> typescript-language
Modern TypeScript standards for type safety and maintainability. Use when working with types, interfaces, generics, enums, unions, or tsconfig settings. (triggers: **/*.ts, **/*.tsx, tsconfig.json, type, interface, generic, enum, union, intersection, readonly, const, namespace)
> typescript-best-practices
Idiomatic TypeScript patterns for clean, maintainable code. Use when writing or refactoring TypeScript classes, functions, modules, or async logic. (triggers: **/*.ts, **/*.tsx, class, function, module, import, export, async, promise)