liveBook · Manning

Parameter object

As explained in The Art of Code

The parameter object pattern groups related method parameters into a single object that represents their combined input.

The Parameter Object pattern groups related method parameters into one small, immutable object. It replaces crowded signatures with a named input whose fields communicate intent and reduce mistakes when several parameters share a type. section 4.2.2

From loose parameters to a meaningful input

Instead of passing separate weight and height values to BMI logic, pass a PhysicalProfile or another focused carrier. Java records such as WeightInPounds, HeightInInches, and PhysicalProfile make units, relationships, and immutability visible at the call site. The same modeling approach produces AgeBmiKey for combined age-and-category lookup keys. section 4.2.2

Listing 4.3 Defining a clear and type-safe physical profile
public record Age(int value) {
  public Age {
    if (value < 0 || value > 150) {
      throw new IllegalArgumentException("Age must " +
         "be between 0 and 150.");
    }
  }
}
public record HeightInInches(double value) {
  public HeightInInches {
    if (value <= 0) {
      throw new IllegalArgumentException("Height must be greater " +
         "than 0 inches.");
    }
  }
}
public record WeightInPounds(double value) {
  public WeightInPounds {
    if (value <= 0) {
      throw new IllegalArgumentException("Weight must be greater " +
         "than 0 pounds.");
    }
  }
}
public record PhysicalProfile(Gender gender,
                              Age age,
                              WeightInPounds weight,
                              HeightInInches height) {

}

Inspect a type-safe record-based carrier that validates its values and groups the physical profile's related inputs. section 4.2.2

How The Art of Code covers it

How deep it goes
Defines the pattern and demonstrates it through typed records, composite keys, and a refactored BMI transformation.
Examples
Uses PhysicalProfile, AgeBmiKey, BMI statistics, and typed records for age, height, weight, percentage, and results.
What you can do after
Apply it via Java records to group related inputs, express units, enforce immutability, and clarify call sites.
How it connects
Connects small data carriers with value types, immutability, records, composite keys, and functional programming.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Art of Code. The section links point to the parts of the book it draws on.