Skip to content

Lesson 9 of 9 · 24m

Writing Java That Does Not Read Like 2008

Adoption rules for records, text blocks, switch expressions, Optional, immutability, exceptions, naming, and pragmatic object-oriented design.

Modern Java style is not about using every new keyword. It is about removing ceremony that no longer carries meaning and refusing abstractions that were fashionable when XML configuration was considered a personality. The best modern Java reads plainly: explicit boundaries, immutable values where possible, small objects with real responsibilities, and errors that preserve operational truth.

Prefer complete objects over setter archaeology

A constructor or factory should produce a valid object. If a workflow requires five setter calls before use, the type is lying about its invariants.

Factories are useful when the creation policy deserves a name. Builders still have their place for many optional values, but not every class needs one.

Use text blocks for embedded languages

SQL, JSON fixtures, HTML templates, and policy documents are more readable as text blocks:

String sql = """
    select id, customer_id, total
    from invoices
    where status = ?
    """;

The compiler handles incidental indentation. Do not concatenate half the query elsewhere unless you enjoy injection accidents and unreadable logging. Parameters still belong in bind variables.

Optional is a return type, not a lifestyle

Optional communicates a possibly absent result:

Optional<Invoice> findOpenInvoice(UUID customerId);

Do not put Optional in fields, constructor parameters, or collections by default. Do not call .get() unless you enjoy runtime failure. Use composition:

return repository.findOpenInvoice(customerId)
    .map(Invoice::id)
    .orElseThrow(() -> new NotFoundException("no open invoice for " + customerId));

If absence is exceptional, throw the domain exception directly. If null is forbidden, enforce non-null early and say so in the API.

Exceptions should carry operational meaning

Catch what you can handle. Translate at boundaries. Preserve causes.

Do not catch Exception, log e.getMessage(), and continue with a half-populated object. Do not throw checked exceptions for routine validation outcomes. Choose exception design around who can act on the failure.

Interfaces earn their existence

The 2008 pattern was InvoiceService plus InvoiceServiceImpl, one implementation forever, with every method passing through. If there is only one implementation and no test double boundary, collapse it. Interfaces are valuable for ports, plugins, external system boundaries, and substitution — not for satisfying an old diagram.

Modern style is team scale

The most maintainable Java codebases are consistent. They use records for values, sealed types for closed state machines, virtual threads where blocking IO dominates, modules where strong boundaries pay rent, immutable collections at edges, explicit nullness expectations, and straightforward exceptions. None of this requires a rewrite. It requires senior engineers to stop writing new code in the dialect that caused the scars.

Field notes

  • Use var when the name carries meaning; avoid it when the type is the important information.
  • Do not make every method chain fluent; side effects belong in readable statements.
  • List.copyOf and immutable factories reject nulls — a feature, but one that can expose bad input assumptions.
  • Avoid Optional as a field unless a library contract truly demands it.
  • A modern idiom that makes a teammate grep four files to understand a simple path is not modernization.

Was this lesson helpful?

Sign in to rate