LLMSkillHub
mirroredCopied from Taoidle/plan-cascade under MIT. Taoidle has not claimed this page, and is not affiliated with LLM SkillHub.

taoidle / java-best-practices

Java coding best practices. Use when writing or reviewing Java code (17+). Covers modern features, error handling, and patterns.

0
SKILL.md
2.0 KB

Java Best Practices (17+)

Code Style

RuleGuideline
Formattergoogle-java-format
AnalysisSpotBugs, Error Prone
NamingClear, no abbreviations

Modern Features

FeatureUsage
RecordsImmutable data carriers
Sealed classesRestricted hierarchies
Pattern matchinginstanceof with binding
varLocal type inference
public record User(String id, String name) {}

if (obj instanceof String s && !s.isEmpty()) { process(s); }

public sealed interface Result<T> permits Success, Failure {}
record Success<T>(T value) implements Result<T> {}
record Failure<T>(Exception error) implements Result<T> {}

Error Handling

RuleGuideline
Specific exceptionsNot bare Exception
Try-with-resourcesFor AutoCloseable
try (var reader = new BufferedReader(new FileReader(path))) {
    return reader.lines().toList();
} catch (IOException e) {
    throw new ConfigException("Failed: " + path, e);
}

Project Structure

src/main/java/com/example/{domain,service}/
src/test/java/
pom.xml or build.gradle

Common Patterns

PatternUsage
OptionalNull-safe returns
Stream APIFunctional collections
BuilderComplex construction
public Optional<User> findById(String id) {
    return Optional.ofNullable(users.get(id));
}

Anti-Patterns

AvoidUse Instead
Returning nullOptional<T>
Mutable dataRecords
instanceof chainsPattern matching

Testing (JUnit 5 + AssertJ)

@Test void shouldProcess() {
    var service = new Service(mockRepo);
    var result = service.process(input);
    assertThat(result.status()).isEqualTo(SUCCESS);
}