liveBook · Manning

Closeness of mapping

As explained in The Art of Code

Closeness to the problem is the degree to which code mirrors the structure and concepts of the problem it represents.

Closeness of mapping measures how directly code mirrors the structure and concepts of the problem. Code feels closer when its names, cases, and transformations match the domain instead of forcing awkward detours through incidental language machinery. section 5.1

A useful test is whether the code makes the underlying idea visible without reconstruction. Direct domain modeling can reduce cognitive complexity, boilerplate, and hidden case-handling work, while also making invalid or missing cases easier to detect. section 5.1.5 section 5.3

Modeling book formats directly

Represent a Book as one of three explicit variants—DigitalBook, PocketBook, or HardcoverBook—and select its promotional message by pattern matching. The digital variant returns its download link; the physical variants return format-specific messages. The cases correspond directly to the domain, without an enum, temporary variable, or cast. section 5.1.5

Listing 5.5 Selecting a promotional message with Haskell pattern matching
data Book
  = DigitalBook String String
  | PocketBook String
  | HardcoverBook String
  deriving (Show, Eq)

getPromotionalMessage :: Book -> String
getPromotionalMessage (DigitalBook _ downloadLink) = downloadLink
getPromotionalMessage (PocketBook _)  = "Same book, smaller format!"
getPromotionalMessage (HardcoverBook _) = "Old-fashioned book..."

Inspect the complete Haskell definition and promotional-message function to see the domain variants mapped directly to behavior. section 5.1.5

Closeness is not a license to choose the shortest construct blindly. Prefer the form that keeps the problem visible while balancing readability, conciseness, safety, and the reader’s background knowledge. section 5.3

How The Art of Code covers it

How deep it goes
Defines domain alignment and demonstrates it through algebraic data types and exhaustive pattern matching.
Examples
Uses digital, pocket, and hardcover books mapped to promotional messages as a direct transformation.
What you can do after
Evaluate code by asking whether its structure exposes the domain directly without unnecessary verbosity or hidden case-handling.
How it connects
Connects closeness of mapping with expressiveness, cognitive complexity, readability, safety, domain modeling, and pattern matching.

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.