Java Coding Guidelines¶
Language Version¶
Java 25 (LTS). Adopt new language features actively.
Modern Java Features¶
- Records for immutable data carriers — prefer over plain POJOs.
- Sealed classes for closed type hierarchies.
- Pattern matching (
instanceof,switchexpressions) — prefer over casting. - Text blocks for multiline strings.
Optional<T>as return type instead of returningnull; never passOptionalas a parameter.- Stream API for collection operations; prefer over manual loops.
- Immutable collections:
List.of(),Map.of(),Set.of().
Naming¶
camelCase— methods, variables, parameters.PascalCase— classes, interfaces, enums, records.SCREAMING_SNAKE_CASE— constants.- Interface names: noun or adjective (
Repository,Serializable), notIRepository.
Type Aliases and Domain Modelling¶
- Use single-field records as type aliases for domain concepts:
record Amount(BigDecimal value) {}. - Prefer
BigDecimaloverdoublefor monetary values.
Dependency Injection¶
- Constructor injection only — never field injection (
@Autowiredon fields is forbidden). - Prefer
finalfields for all injected dependencies.
Error Handling¶
- Prefer unchecked exceptions for programming errors; checked exceptions for recoverable conditions callers must handle.
- Never declare
throws Exception— declare specific exception types. - Never swallow exceptions with empty
catchblocks.
Build¶
- Gradle (Kotlin DSL) preferred for new projects; Maven acceptable.
- Enforce code style with Checkstyle in CI; config at
checkstyle.xml(copy fromtemplates/).
Frameworks¶
- Services: Spring Boot — preferred.
- Configuration:
application.yml; override with environment variables viaspring-dotenvor OS env.
Testing¶
- JUnit 6 for unit tests (JUnit 5 still supported for migration).
- Mockito for mocking at architectural boundaries only.
- AssertJ for fluent assertions.
- Cucumber JVM + JUnit Platform for BDD integration tests.
- Structure test bodies with
// GIVEN,// WHEN,// THEN.
General Advice¶
- Avoid to much hidden magic, e.g. Java Annotation Processing. Better make things more explicit and visible. Never use Mapstruct.
- When using Spring (Boot)
- Use constructor injection
@autowireprohibited