Get the FREE Ultimate OpenClaw Setup Guide →

java-best-practices

Scanned
npx machina-cli add skill Taoidle/plan-cascade/java --openclaw
Files (1)
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);
}

Source

git clone https://github.com/Taoidle/plan-cascade/blob/master/builtin-skills/java/SKILL.mdView on GitHub

Overview

Java Best Practices (17+) guides you through modern language features, error handling patterns, and common design patterns. It emphasizes readability, safety, and maintainable architectures. Use it when writing or reviewing Java code (17+).

How This Skill Works

It combines prescriptive rules with concrete examples and code snippets. Technical guidance covers modern features like records, sealed classes, pattern matching, and local type inference (var), plus practical patterns for errors and resources.

When to Use It

  • Starting a new Java 17+ project or module
  • Reviewing legacy code for readability and safety
  • Refactoring data carriers to immutable records
  • Improving error handling, resources, and exception use
  • Auditing project structure and patterns for consistency

Quick Start

  1. Step 1: Enable google-java-format and static analysis in your build
  2. Step 2: Refactor a sample class to a record and add Optional results
  3. Step 3: Run tests with JUnit 5 + AssertJ and fix issues

Best Practices

  • Use google-java-format for consistent code style
  • Run SpotBugs and Error Prone for static analysis
  • Prefer clear, non-abbreviated naming
  • Adopt modern features: records, sealed classes, pattern matching, var
  • Favor Optional, Stream API, and Builders for safer design

Example Use Cases

  • Refactor a data carrier to a record to model immutable data
  • Replace a null return with Optional<T> for null safety
  • Use pattern matching to simplify instanceof checks
  • Define a sealed interface like Result<T> with permitted types
  • Wrap AutoCloseable resources in try-with-resources

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers