liveBook · Manning

Lazy evaluation

As explained in Object-Oriented Software Design in C++

The Lazy Evaluation Principle is a design principle that delays an expensive calculation until its result is needed.

Lazy evaluation postpones an expensive calculation until its result is needed. This avoids unnecessary work while preserving the class's public behavior. section 5.3.3

A hybrid Date representation

A Date object stores both calendar fields and a Julian day number. Validity flags record which representation is current: getters validate year, month, and day only before access, while date arithmetic validates the Julian value only before use. Each conversion therefore happens on demand and can be reused. section 5.3.3

How Object-Oriented Software Design in C++ covers it

How deep it goes
Defines the principle and demonstrates synchronized representations, validity flags, and demand-triggered conversion; it stops short of general algorithms.
Examples
Uses a Date class that converts between calendar fields and Julian day numbers only when access or arithmetic requires synchronization.
What it assumes
Assumes basic classes, private state, public getters, and encapsulation while teaching demand-triggered calculation through a concrete example.
What you can do after
Apply lazy evaluation by tracking representation validity and validating each representation immediately before its use.
How it connects
Connects lazy evaluation with implementation hiding, encapsulation, getter functions, and efficient date arithmetic.

Also explained in

Compare how 13 books explain lazy evaluation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Object-Oriented Software Design in C++. The section links point to the parts of the book it draws on.