liveBook · Manning

Contextual attribute

As explained in Data-Oriented Programming in Java

A contextual attribute is an attribute whose meaning applies specifically to a particular domain state or context rather than to the general concept.

A contextual attribute is meaningful only for a particular domain state. The attribute completedBy fits a Completed status, but distorts a generic Status when stored there. section 4.2.1

Use the attribute’s friction as feedback about the domain model:

  • Combine attributes only when their meaning remains valid for every value of the containing type. section 4.2.1
  • Move attributes that describe one state into a dedicated data type, such as Completed. section 4.2.1
  • Separate other states, such as NotStarted and Skipped, instead of using generic fields that permit misleading combinations. section 4.2.1

Completed status

A generic Status containing instance, step, isCompleted, and completedBy mixes state meanings. A Completed record makes completedBy and completedOn belong explicitly to the completed state. section 4.2.1

Listing 4.27 Combining this attribute breaks the meaning of our data
record Status(              
    Instance instance,     
    /*AND*/ Step step,               
    /*AND*/ boolean isCompleted,   
    /*AND*/ User completedBy      #A
){}

Inspect the misleading generic Status record where completedBy is combined with a completion flag. section 4.2.1

How Data-Oriented Programming in Java covers it

How deep it goes
Defines the idea through incremental attribute combinations and stops at extracting state-specific data types.
Examples
Uses Status, Completed, NotStarted, and Skipped records to contrast generic and state-specific attributes.
What you can do after
Build it from scratch by identifying attributes that describe a narrower state and placing them in a dedicated data type.
How it connects
Connects contextual attributes to domain modeling, algebraic data types, records, and compiler-readable state distinctions.

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming in Java. The section links point to the parts of the book it draws on.