java-best-practices
Scannednpx machina-cli add skill Taoidle/plan-cascade/java --openclawFiles (1)
SKILL.md
2.0 KB
Java Best Practices (17+)
Code Style
| Rule | Guideline |
|---|---|
| Formatter | google-java-format |
| Analysis | SpotBugs, Error Prone |
| Naming | Clear, no abbreviations |
Modern Features
| Feature | Usage |
|---|---|
| Records | Immutable data carriers |
| Sealed classes | Restricted hierarchies |
| Pattern matching | instanceof with binding |
var | Local 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
| Rule | Guideline |
|---|---|
| Specific exceptions | Not bare Exception |
| Try-with-resources | For 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
| Pattern | Usage |
|---|---|
| Optional | Null-safe returns |
| Stream API | Functional collections |
| Builder | Complex construction |
public Optional<User> findById(String id) {
return Optional.ofNullable(users.get(id));
}
Anti-Patterns
| Avoid | Use Instead |
|---|---|
| Returning null | Optional<T> |
| Mutable data | Records |
instanceof chains | Pattern 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
- Step 1: Enable google-java-format and static analysis in your build
- Step 2: Refactor a sample class to a record and add Optional results
- 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