liveBook · Manning

Implicit argument

As explained in Grokking Simplicity

A code smell in which a function's name encodes a value that would more appropriately be supplied explicitly as an argument.

An implicit argument is a code smell: similar functions differ by a value embedded in their names instead of receiving it explicitly. Making that value first-class can clarify intent and reduce duplication. section 10.1 section 10.3

Use the express implicit argument refactoring in four steps:

  • Identify the value encoded in the function name. section 11.1
  • Add an explicit parameter for that value. section 11.1
  • Replace the hard-coded value in the function body with the parameter. section 12.1
  • Update calling code, then add runtime checks when only certain fields are valid. section 10.7

From named fields to one function

Instead of maintaining incrementQuantityByName(cart, name) and incrementSizeByName(cart, name), use incrementFieldByName(cart, name, field). The function reads item[field], increments the value, and writes it back. A check can reject every field except size and quantity. section 10.7 section 14.2

function setPriceByName(cart, name, price) {
    var item = cart[name];
    var newItem = objectSet(item, 'price', price); #1
    var newCart = objectSet(cart, name, newItem);
    return newCart;
}
 
function setQuantityByName(cart, name, quant) {
    var item = cart[name];‡
    var newItem = objectSet(item, 'quantity', quant); #1
    var newCart = objectSet(cart, name, newItem);
    return newCart;
}
 
function setShippingByName(cart, name, ship) { #2
    var item = cart[name];
    var newItem = objectSet(item, 'shipping', ship); #1
    var newCart = objectSet(cart, name, newItem);
    return newCart;
}
 
function setTaxByName(cart, name, tax) { #2
    var item = cart[name];
    var newItem = objectSet(item, 'tax', tax); #1
    var newCart = objectSet(cart, name, newItem);
    return newCart;
}
 
function objectSet(object, key, value) { #3
    var copy = Object.assign({}, object);
    copy[key] = value;
    return copy;
}

Compare nearly identical cart-update functions whose field names are hard-coded, then generalize the field as an argument. section 10.3

The same smell can appear at deeper levels: making nested keys and modification behavior explicit leads toward reusable update functions and callbacks. section 14.10 section 14.11

How Grokking Simplicity covers it

How deep it goes
Explains recognition, four refactoring steps, validation, nested updates, and callbacks, but stops short of a full language-internals treatment.
Examples
Uses shopping-cart field setters, quantity and size incrementers, multiplication functions, and nested shirt options.
What it assumes
Assumes basic functions and objects while teaching first-class values, higher-order functions, and the smell from practical examples.
What you can do after
Diagnose/debug with it by spotting name-encoded differences and refactoring them into explicit, validated arguments.
How it connects
Connects code smells with refactoring, first-class values, higher-order functions, callbacks, and nested data updates.

Also explained in

Compare how 2 books explain implicit argument →

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.