liveBook · Manning

Semantic drift

As explained in Data-Oriented Programming in Java

Semantic drift is the gradual divergence of a program’s interpretation of data or state from its intended meaning.

Semantic drift starts when code stores a business meaning only as an arrangement of fields, flags, nulls, and conventions. Each later reader or method must reconstruct that meaning. One mistaken interpretation can turn an apparently valid state into a bug. section 1.3.1

A scheduled task whose fields tell different stories

A task sets scheduledAt and attempts together: a short delay plus incremented attempts suggests an immediate retry; a longer delay plus zero attempts suggests waiting; null plus zero attempts means cancellation. Those meanings are not represented by the fields themselves, so cancellation has to be rediscovered elsewhere by testing scheduledAt for null. section 1.3.1

Reducing duplicated fields does not necessarily repair the model. A single Status record with an enum and nullable action details still permits combinations that make sense only in a particular context. Constructors and every consumer then need defensive checks, while new statuses multiply the validation burden. section 4.1.6

Records combine every attribute with an implicit AND. That is useful when the attributes genuinely belong together, but harmful when one lifecycle is compressed into nullable details: completedBy and completedOn imply completion even when a completion flag says otherwise. Treat the mismatch as a modeling problem: compare what the data means with what the code can actually express, then separate distinct ideas rather than relying on names as clues. section 4.2 section 4.9

Listing 1.4 if attempts is not set then it means..."
class ScheduledTask {
    private Instant scheduledAt; #A
    private int attempts;        #A
    
    public void reschedule() {  #B
        if (this.someSuperComplexCondition()) {
            this.scheduledAt = now();
            this.attempts += 1;
        } else if (this.someOtherComplexCondition()) {
            this.scheduledAt = now().plusSeconds(DELAY);
            this.attempts = 0;
        } else {
            this.scheduledAt = null;
            this.attempts = 0;
        }
    }
}

Inspect how three assignments to the same two fields encode three operational meanings without naming any of them. section 1.3.1

How Data-Oriented Programming in Java covers it

How deep it goes
Traces drift from ambiguous field assignments through defensive checks to conflicting record attributes and modeling remedies.
Examples
Uses scheduled-task retry fields and checklist-status completion details to expose hidden lifecycle states.
What it assumes
Assumes familiarity with Java records, enums, nulls, constructors, and ordinary object-oriented state handling.
What you can do after
Diagnose/debug with it by spotting contextual fields, null-dependent states, and defensive checks that signal misaligned representations.
How it connects
Connects semantic integrity, defensive programming, records, sealed interfaces, algebraic data types, product types, and sum types.

Related concepts

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.