liveBook · Manning

Generic data structure

As explained in Data-Oriented Programming: Reduce software complexity

A generic data collection is a collection whose representation is not tied to a particular domain class or component implementation.

When a system must handle changing records, representing each entity as a generic data structure avoids tying its data to a particular domain class. A record becomes a heterogeneous string map, while collections use lists or indexed maps. Code depends on the field names it needs, not on the complete representation, creating a weaker dependency between data and the functions that manipulate it. section 3.0 section 3.6

The approach trades some compile-time safety for flexibility:

  • Represent records with string-keyed maps whose values may be primitives, lists, or nested indexes. section 3.6
  • Access nested values through information paths and manipulate them with generic functions. section 3.0
  • Add, remove, rename, merge, filter, group, or select fields without changing a class hierarchy. section 3.6 section 10.5
  • Accept weaker field-access safety in exchange for late binding, genericity, visualization, and easier adaptation to changing requirements. section 3.6

A web-service request and response

A search endpoint can parse a JSON request such as {"title":"habit","fields":["title","weight","number_of_pages"]} into a map. Business logic selects fields by name, combines database data with an external API result, and serializes the resulting map for the client. Nesting avoids field conflicts but produces a non-flat result; merging produces a flat map but requires a conflict policy. section 11.3 section 11.5

The same representation suits database results: relational rows become lists of maps, and generic functions can operate across different entities. It also lets inner web-service components communicate through data rather than importing one another’s class definitions, keeping components loosely coupled. section 10.0 section 11.2

Lodash functions introduced in this chapter

Function

Description

get(map, path)

Gets the value of map at path

has(map, path)

Checks if map has a field at path

merge(mapA, mapB)

Creates a map resulting from the recursive merges between mapA and mapB

values(map)

Creates an array of values of map

filter(coll, pred)

Iterates over elements of coll, returning an array of all elements for which pred returns true

map(coll, f)

Creates an array of values by running each element in coll through f

Use the Lodash reference to browse concrete operations for reading paths, merging maps, extracting values, filtering collections, and mapping elements. section 3.6

How Data-Oriented Programming: Reduce software complexity covers it

How deep it goes
Explains representations, paths, trade-offs, generic operations, database use, web-service flow, and serialization, but defers state management.
Examples
Demonstrates string maps, database row collections, JSON search payloads, API-map merging, Lodash operations, and debugger serialization.
What it assumes
Assumes basic relational databases and SQL for database examples, while defining records, collections, indexes, maps, and paths.
What you can do after
Apply it via maps, lists, generic functions, JSON serialization, and Lodash to manipulate adaptable application and database data.
How it connects
Connects data-oriented programming with records, maps, arrays, generic functions, JSON serialization, databases, web services, and loose coupling.

Related concepts

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