liveBook · Manning

Call graph

As explained in Grokking Simplicity

A graph representation in which program entities are connected according to which functions or procedures call which others.

A call graph represents program entities as connected nodes, with arrows showing which functions or procedures call which others. It turns code relationships into a visual map of abstraction layers. section 9.15 section 8.10

Use a call graph to:

  • Spot functions that mix levels of abstraction by comparing the lengths of their outgoing arrows. section 8.5
  • Inspect the whole system, a particular layer, or one function through global, layer, and function zoom levels. section 8.6
  • Guide extraction of more general functions that clarify code, improve testing, and enable reuse. section 8.8
  • Reason about where changing, test-critical, and reusable code belongs within the graph. section 9.15 section 9.16

A shopping-cart graph

An initial freeTieClip(cart) function combines a loop, array indexing, item construction, and cart updates. Its call graph exposes calls spanning different abstraction levels. Extracting isInCart(cart, name) produces a clearer graph in which freeTieClip calls functions at more similar levels of detail. section 8.5 section 8.8

function freeTieClip(cart) {
    var hasTie   = false
    var hasTieClip = false;
    for(var i = 0; i < cart.length; i++) {
        var item = cart[i];
        if(item.name === "tie") #1
            hasTie = true;
        if(item.name === "tie clip") #1
            hasTieClip = true;
    }
    if(hasTie && !hasTieClip) {
        var tieClip = make_item("tie clip", 0);
        return add_item(cart, tieClip); #2
    }
    return cart;
}

Inspect the original cart implementation whose loop, array indexing, item creation, and update calls reveal mixed abstraction levels. section 8.5

How Grokking Simplicity covers it

How deep it goes
Builds graphs from function calls, examines global, layer, and function views, and uses arrow patterns to diagnose design problems.
Examples
Uses JavaScript shopping-cart functions such as freeTieClip, isInCart, add_item, and remove_item_by_name.
What it assumes
Assumes familiarity with functions and basic code structure while developing abstraction layers through concrete examples.
What you can do after
Diagnose/debug with it by locating mixed abstraction levels and choosing functions to extract or inspect more closely.
How it connects
Connects call graphs to stratified design, abstraction barriers, straightforward implementations, testability, maintainability, and reusability.

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.