liveBook · Manning

Code smell

As explained in Grokking Simplicity

A code smell is a characteristic of source code that may indicate deeper design or maintenance problems.

A code smell is a characteristic of source code that may indicate a deeper design or maintenance problem. It is a signal to investigate, not proof that the code is bad. section 5.1 section 10.3

Look for these clues when reviewing code:

  • Similar function implementations differ only in a value represented by the function name rather than an explicit argument. section 10.1
  • Duplication appears in multiple places, such as repeatedly adding an item price to a total. section 5.1
  • A business rule depends directly on a lower-level representation such as an array-based shopping cart. section 5.11

Smells guide refactoring. Make hidden values explicit, replace hard-coded differences with arguments, and extract changing behavior into callbacks when appropriate. section 10.1 section 11.1

An implicit argument in a function name

Functions such as setPriceByName, setQuantityByName, setShippingByName, and setTaxByName repeat the same implementation while encoding the changing field in their names. Pass the field as an argument instead, then reuse one implementation. The practical sequence is: identify the implicit argument, add an explicit argument, replace the hard-coded value, and update callers. section 10.3 section 10.1

Separating mixed responsibilities

An update_shipping_icons function can combine DOM access, button iteration, cart operations, and the free-shipping rule. Separating those concerns into functions makes each responsibility clearer, while recognizing that acceptable code may still have further improvements. section 5.11

How Grokking Simplicity covers it

How deep it goes
Defines smells, identifies their characteristics, and gives step-by-step refactorings, but does not provide a complete taxonomy.
Examples
Uses shipping calculations, repeated cart setters, and a mixed DOM-and-cart update function as diagnostic examples.
What you can do after
Diagnose potential design problems and apply refactorings that expose arguments, reduce duplication, and separate responsibilities.
How it connects
Connects code smells with duplication, refactoring, first-class values, higher-order functions, business rules, and layers.

Also explained in

Compare how 2 books explain code smell →

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.