liveBook · Manning

Read operation

As explained in Grokking Simplicity

A read operation obtains the current value of data without modifying that data.

A read operation gets information from data without modifying it. Reads that obtain information from their arguments are calculations because they have no hidden inputs or outputs. section 6.2

Use these distinctions to classify common operations:

  • Reads retrieve information without changing data; writes modify data. section 6.2
  • Getting a shopping cart's item count or an item by name is a read; adding, removing, or updating items is a write. section 6.2
  • A function can replace a write with a read by copying data, modifying the copy, and returning it. section 6.3

Separate a read from a write

Instead of using array.shift() for both behaviors, define first_element(array) to return array[0] without changing the array. Keep removal separate, then make drop_first(array) copy the array, shift the copy, and return it. Splitting the operation makes the read independently reusable. section 6.9

function first_element(array) {
    return array[0]; #1
}

Inspect the minimal JavaScript implementation of a read that returns the first array element without modification. section 6.9

How Grokking Simplicity covers it

How deep it goes
Defines reads, distinguishes them from writes, and separates combined behavior, but defers operations that both read and write.
Examples
Uses shopping cart queries, item access, array indexing, and separated first-element and drop-first functions.
What it assumes
Assumes basic data operations and JavaScript arrays while defining the read/write distinction explicitly.
What you can do after
Evaluate operations as reads or writes and separate retrieval from mutation in JavaScript functions.
How it connects
Connects reads with calculations, writes, immutable data, and copy-on-write.

Also explained in

Compare how 3 books explain read operation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Simplicity. The section links point to the parts of the book it draws on.