Java/01intro10 min

Style and conventions (Google/Sun style)

Java has a long tradition of conventions — from the classic Sun Code Conventions to the modern Google Java Style Guide. ProfessNet adopts the Google Java Style and enforces it automatically so that code looks the same regardless of the author.

Naming

ElementConventionExample
Class, interfacePascalCaseProbeService, Scannable
Method, variablecamelCaserunScan, forestName
ConstantUPPER_SNAKE_CASEMAX_RETRIES
Packagelowercasecom.professnet.zeus.inventory
Type parametera single letterT, K, V
package com.professnet.zeus.inventory;

public final class ProbeService {

    private static final int MAX_RETRIES = 3;

    private final ProbeRepository repository;

    public ProbeService(ProbeRepository repository) {
        this.repository = repository;
    }

    public ScanResult runScan(String forestName) {
        // ...
    }
}

ProfessNet standard: interface names do not use the I prefix (that's a C# convention, not Java). Packages use a reversed domain: com.professnet.zeus.<module>.

Formatting per Google Style

The most important rules that Google Java Format enforces:

  • 2-space indentation (not 4, no tabs).
  • A line limit of 100 characters.
  • Braces always, even for a one-line if.
  • Each top-level class in its own file.
// good — braces always, even for a single statement
if (probe == null) {
  return Optional.empty();
}

// bad — no braces
if (probe == null) return Optional.empty();

Imports

We import specific classes; we don't use wildcards. Static imports only for assertions/utilities, and readably.

// bad — wildcard import
import java.util.*;

// good — explicit imports
import java.util.List;
import java.util.Optional;

Optional instead of null in the API

Methods that may not return a value return Optional<T>, not null.

public Optional<Probe> findByHost(String host) {
  return repository.findByHost(host);
}

ProfessNet standard: a public API does not return null for "no result" — we use Optional. null as a collection value is forbidden; use an empty list instead of null.

Automating style

Style is enforced by Spotless with the Google Java Format formatter, hooked into the build.

<!-- Maven: spotless-maven-plugin -->
<configuration>
  <java>
    <googleJavaFormat><version>1.22.0</version></googleJavaFormat>
  </java>
</configuration>
mvn spotless:check     # CI: verifies style
mvn spotless:apply     # locally: formats

Tip: add Checkstyle with the Google config to enforce rules the formatter itself won't catch (e.g. naming, modifier ordering).


Google Java Style + Spotless + Checkstyle in the build produce uniform, readable code without manual fixes at review. The machine replaces the formatting discussion, and we focus on logic.