liveBook · Manning

Implicit output

As explained in Grokking Simplicity

An effect of a function that changes external state or performs an observable operation rather than returning the result directly.

An implicit output is information or an observable effect leaving a function through something other than its return value. Updating a global variable, modifying an array, or changing the DOM are examples. Return values make outputs explicit and easier to test, reuse, and control. section 4.7 section 4.8

Use these distinctions when examining a function:

  • Arguments are explicit inputs; other information entering the function is an implicit input. section 4.7
  • Return values are explicit outputs; other information or effects leaving the function are implicit outputs. section 4.7
  • Replace an implicit output with a local result and return it, then perform the external update at the calling boundary. section 4.10 section 4.9
  • Minimize implicit outputs even when an action cannot become a calculation; fewer hidden connections improve modularity, reuse, and testability. section 5.3 section 5.14

Making a cart update explicit

Instead of pushing directly into the global shopping_cart, copy the cart, add the item to the copy, return it, and assign that result outside the extracted function. The function then has explicit inputs and outputs rather than an implicit array mutation. section 4.10

function add_item_to_cart(name, price) { #1
    shopping_cart =
        add_item(shopping_cart, name, price);
    calc_cart_total();
}
     
function add_item(cart, name, price) {
    var new_cart = cart.slice(); #2
    new_cart.push({ #3
        name: name,
        price: price
    });
    return new_cart; #4
}

This listing shows the complete conversion from global-array mutation to copy-on-write and a returned cart value. section 4.10

Asynchronous work cannot expose a later result through an immediate return value. Pass a callback instead, so the calculated value becomes an explicit output when the asynchronous operations finish. section 15.29

How Grokking Simplicity covers it

How deep it goes
Explains definitions, testing consequences, copy-on-write extraction, and asynchronous callbacks, but does not develop implementation details beyond JavaScript examples.
Examples
Uses shopping-cart mutation, cart-total calculation, DOM updates, shipping icons, and asynchronous cost and shipping calculations.
What it assumes
Assumes familiarity with functions and JavaScript arrays while teaching explicit versus implicit inputs and outputs from basic examples.
What you can do after
Diagnose/debug with it by locating hidden outputs, then replace them with return values or callbacks to improve testing and reuse.
How it connects
Connects implicit outputs with actions, calculations, side effects, global variables, DOM updates, return values, and callbacks.

Also explained in

Compare how 2 books explain implicit output →

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.