Java Coding Standards

中级 Intermediate 参考型 Reference ⚡ Claude Code 专属 ⚡ Claude Code Optimized
1 min read · 71 lines

Java 17+ coding standards — records, sealed classes, pattern matching, and streams

Java Coding Standards

File: skills/java-coding-standards/SKILL.md

Key Standards

Naming

  • Classes/Records: PascalCaseMarketService, Money
  • Methods/Fields: camelCasefindBySlug, marketRepository
  • Constants: UPPER_SNAKE_CASEMAX_PAGE_SIZE

Immutability

Favor records and final fields. Use MarketDto(Long id, String name) records. Only getters, no setters.

Optional Usage

Return Optional from find methods. Use map/flatMap/orElseThrow instead of get().

Streams

Use streams for transformations, keep pipelines short. For complex logic, prefer loops for clarity.

Exceptions

Unchecked exceptions for domain errors. Create domain-specific exceptions. Avoid broad catch (Exception ex).

Generics

Avoid raw types. Prefer bounded generics: <T extends Identifiable>.


Project Structure

src/main/java/com/example/app/
  config/ controller/ service/ repository/ domain/ dto/ util/
src/main/resources/application.yml
src/test/java/... (mirrors main)

Code Smells

  • Long parameter lists -> DTO/builders
  • Deep nesting -> early returns
  • Magic numbers -> named constants
  • Static mutable state -> dependency injection
  • Silent catch blocks -> log and rethrow

Testing

JUnit 5 + AssertJ for fluent assertions. Mockito for mocking. Deterministic tests only.


Remember: Keep code intentional, typed, and observable. Optimize for maintainability.

相关技能 Related Skills