Catalog
affaan-m/java-coding-standards

affaan-m

java-coding-standards

Java coding standards for Spring Boot services: naming, immutability, Optional usage, streams, exceptions, generics, and project layout.

global
0installs0uses~949
v1.2Saved Apr 20, 2026

Java Coding Standards

Standards for readable, maintainable Java (17+) code in Spring Boot services.

When to Activate

  • Writing or reviewing Java code in Spring Boot projects
  • Enforcing naming, immutability, or exception handling conventions
  • Working with records, sealed classes, or pattern matching (Java 17+)
  • Reviewing use of Optional, streams, or generics
  • Structuring packages and project layout

Core Principles

  • Prefer clarity over cleverness
  • Immutable by default; minimize shared mutable state
  • Fail fast with meaningful exceptions
  • Consistent naming and package structure

Naming

// PASS: Classes/Records: PascalCase
public class MarketService {}
public record Money(BigDecimal amount, Currency currency) {}

// PASS: Methods/fields: camelCase
private final MarketRepository marketRepository;
public Market findBySlug(String slug) {}

// PASS: Constants: UPPER_SNAKE_CASE
private static final int MAX_PAGE_SIZE = 100;

Immutability

// PASS: 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

// PASS: Return Optional from find* methods
Optional<Market> market = marketRepository.findBySlug(slug);

// PASS: Map/flatMap instead of get()
return market
    .map(MarketResponse::from)
    .orElseThrow(() -> new EntityNotFoundException("Market not found"));

Streams Best Practices

// PASS: Use streams for transformations, keep pipelines short
List<String> names = markets.stream()
    .map(Market::name)
    .filter(Objects::nonNull)
    .toList();

// FAIL: Avoid complex nested streams; prefer loops for clarity

Exceptions

  • Use unchecked exceptions for domain errors; wrap technical exceptions with context
  • Create domain-specific exceptions (e.g., MarketNotFoundException)
  • Avoid broad catch (Exception ex) unless rethrowing/logging centrally
throw new MarketNotFoundException(slug);

Generics and Type Safety

  • Avoid raw types; declare generic parameters
  • Prefer bounded generics for reusable utilities
public <T extends Identifiable> Map<Long, T> indexById(Collection<T> items) { ... }

Project Structure (Maven/Gradle)

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

Formatting and Style

  • Use 2 or 4 spaces consistently (project standard)
  • One public top-level type per file
  • Keep methods short and focused; extract helpers
  • Order members: constants, fields, constructors, public methods, protected, private

Code Smells to Avoid

  • Long parameter lists → use DTO/builders
  • Deep nesting → early returns
  • Magic numbers → named constants
  • Static mutable state → prefer dependency injection
  • Silent catch blocks → log and act or rethrow

Logging

private static final Logger log = LoggerFactory.getLogger(MarketService.class);
log.info("fetch_market slug={}", slug);
log.error("failed_fetch_market slug={}", slug, ex);

Null Handling

  • Accept @Nullable only when unavoidable; otherwise use @NonNull
  • Use Bean Validation (@NotNull, @NotBlank) on inputs

Testing Expectations

  • JUnit 5 + AssertJ for fluent assertions
  • Mockito for mocking; avoid partial mocks where possible
  • Favor deterministic tests; no hidden sleeps

Remember: Keep code intentional, typed, and observable. Optimize for maintainability over micro-optimizations unless proven necessary.

Files1
1 files · 1.0 KB

Select a file to preview

Overall Score

89/100

Grade

A

Excellent

Safety

95

Quality

88

Clarity

91

Completeness

82

Summary

A comprehensive style guide for Java 17+ code in Spring Boot services, covering naming conventions, immutability patterns, Optional/streams usage, exception handling, generics, project structure, and testing practices. Provides code examples for both correct and incorrect patterns to guide developers and code reviewers.

Detected Capabilities

Code style guidance and best practicesNaming convention standards (classes, methods, constants)Immutability and final field enforcementOptional and stream API usage patternsException handling and domain-specific error designGeneric type safety recommendationsMaven/Gradle project structure conventionsTesting framework recommendations (JUnit 5, Mockito, AssertJ)Code smell identification and refactoring guidance

Trigger Keywords

Phrases that MCP clients use to match this skill to user intent.

java code reviewspring boot standardsjava naming conventionsimmutable objectsoptional patternexception handlingproject structure

Use Cases

  • Writing new Java/Spring Boot services with consistent standards
  • Reviewing Java code for style and architecture compliance
  • Onboarding developers to project coding conventions
  • Establishing immutability and null-safety practices
  • Teaching modern Java 17+ features (records, sealed classes, pattern matching)

Quality Notes

  • Excellent use of contrastive code examples (PASS vs FAIL) to clarify expectations
  • Clear section hierarchy with practical, real-world patterns from Spring Boot domain
  • Strong focus on immutability and null-safety aligns with modern Java best practices
  • Project structure example is specific and actionable (src/main/java layout with package organization)
  • Logging section provides concrete SLF4J patterns with structured logging (key=value format)
  • Well-scoped to Java 17+ and Spring Boot ecosystem; avoids generic language-agnostic advice
  • Testing expectations are specific (JUnit 5, Mockito, AssertJ) rather than vague
  • Code smells section with solutions (parameter lists → DTOs, nesting → early returns) is practical
  • Null handling section references Bean Validation annotations, showing framework integration
  • Minor: 'Code Smells to Avoid' section could benefit from explicit examples of each anti-pattern
Model: claude-haiku-4-5-20251001Analyzed: Apr 20, 2026

Reviews

Add this skill to your library to leave a review.

No reviews yet

Be the first to share your experience.

Version History

v1.2

Content updated

2026-04-20

Latest
v1.1

Content updated

2026-04-12

v1.0

Seeded from github.com/affaan-m/everything-claude-code

2026-03-16

Add affaan-m/java-coding-standards to your library

Command Palette

Search for a command to run...