Skip to content
AstralSurge Learnings
Follow LinkedIn X RSS

Core Java Fundamentals IV: Naming Conventions, Records, and Clean Architecture

Standardized naming conventions and structural idioms are essential for engineering clean, maintainable enterprise software. Adhering to the official Java Language Specification preserves readability across distributed teams.

1. Standard Java Naming Conventions

  • Classes & Records (UpperCamelCase): Should be nouns or noun phrases representing concepts (UserProfile, OrderDispatcher).
  • Interfaces (UpperCamelCase): Frequently adjectives indicating capabilities (Runnable, Serializable, Comparable) or nouns describing contracts (UserRepository).
  • Methods (lowerCamelCase): Should be verbs or verb phrases (calculateTotal(), fetchRecordById()).
  • Variables (lowerCamelCase): Should clearly indicate intent (retryCount, activeSessionToken).
  • Constants (SCREAMING_SNAKE_CASE): Declared as static final primitives or immutable references (DEFAULT_TIMEOUT_MS).

2. Modern Data Carriers: Records vs. JavaBeans

Traditional JavaBeans required verbose getters, setters, equals(), hashCode(), and toString() implementations. Java 16+ introduced immutable record types to eliminate boilerplate:

// Modern immutable record carrier
public record CustomerDto(
    Long customerId,
    String fullName,
    String emailAddress
) {
    // Compact constructor for validation
    public CustomerDto {
        Objects.requireNonNull(customerId, "Customer ID cannot be null");
        Objects.requireNonNull(emailAddress, "Email cannot be null");
    }
}

3. Immutability and Defensive Copying

Always design domain entities to be immutable by default. When storing or returning mutable references like collections or arrays, create defensive copies using List.copyOf() or Collections.unmodifiableList() to preserve internal state integrity.

0 Comments