Standards for readable, maintainable Java (17+) code in Spring Boot services.
Core Principles
Prefer clarity over cleverness
Immutable by default; minimize shared mutable state
Fail fast with meaningful exceptions
Consistent naming and package structure
Naming
// β Classes/Records: PascalCase
public class MarketService {}
public record Money(BigDecimal amount, Currency currency) {}
// β Methods/fields: camelCase
private final MarketRepository marketRepository;
public Market findBySlug(String slug) {}
// β Constants: UPPER_SNAKE_CASE
private static final int MAX_PAGE_SIZE = 100;
Immutability
// β Favor records and final fields
public record MarketDto(Long id, String name, MarketStatus status) {}
public class Market {
private final Long id;
private final String name;
// getters only, no setters
}
Optional Usage
// β Return Optional from find* methods
Optional<Market> market = marketRepository.findBySlug(slug);
// β Map/flatMap instead of get()
return market
.map(MarketResponse::from)
.orElseThrow(() -> new EntityNotFoundException("Market not found"));
Streams Best Practices
// β Use streams for transformations, keep pipelines short
List<String> names = markets.stream()
.map(Market::name)
.filter(Objects::nonNull)
.toList();
// β Avoid complex nested streams; prefer loops for clarity
Exceptions
Use unchecked exceptions for domain errors; wrap technical exceptions with context