liveBook · Manning

Recursive comparison

As explained in Data-Oriented Programming: Reduce software complexity

Recursive comparison is the process of comparing nested data structures by recursively comparing their contained values.

A data-manipulating function can be tested without mocks: prepare input, calculate the expected output, and compare both nested data structures field by field. Recursive comparison handles maps and arrays without requiring hand-written recursion. section 6.1

Use the recurring test pattern:

Equal and non-equal collections

Comparing two collections with the same name and ISBN array returns true; changing the ISBN to "bad-isbn" returns false. The same approach supports positive and negative test cases. section 6.1 section 6.6

Listing 6.1 Comparing an equal data collection recursively
_.isEqual({
  "name": "Alan Moore",
  "bookIsbns": ["978-1779501127"]
}, {
    "name": "Alan Moore",
    "bookIsbns": ["978-1779501127"]
  });
// → true

Inspect a concrete _.isEqual call comparing nested map and array values. section 6.1

How Data-Oriented Programming: Reduce software complexity covers it

How deep it goes
Explains field-by-field recursion and generic implementation, without detailing the recursion algorithm.
Examples
Uses equal and non-equal maps containing arrays, plus a generic f(dataIn) test pattern.
What it assumes
Assumes basic primitive comparisons and teaches collection comparison from there.
What you can do after
Apply it via Lodash's _.isEqual to test nested data outputs against expected data.
How it connects
Connects recursive comparison to unit testing, data manipulation, JSON parsing, and data-oriented programming.

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.