Scoped: Visualising the Scope Chain Within Source Code

Ivan Bacher, Brian Mac Namee, John D. Kelleher

Abstract

Scoped is an interactive visualization tool designed to assist developers in navigating the complex scope chains of JavaScript. By employing circular treemaps to represent nested code blocks, Scoped provides a real-time, spatial map of variable accessibility. This paper details the tool's design, which integrates a source code editor with synchronized visual components, and presents results from a pilot study indicating improved program comprehension and bug detection.

Introduction

Software maintenance and debugging consume more than half of total development effort, with a significant portion of that time dedicated to understanding existing code. In languages like JavaScript, "scope"—the set of rules determining variable visibility—is a frequent source of confusion due to deep nesting and the "scope chain" mechanism. If a variable is not found in the immediate function, the engine climbs the chain to parent functions until it reaches the global level.

For non-experts, this complexity leads to "scope smells": naming conflicts, confusion over the this keyword, and accidental global variable overrides. While modern debuggers display variable values at breakpoints, they rarely provide a high-level structural map of how these scopes relate to one another statically. Scoped aims to bridge this gap by providing a visual representation of the code's hierarchy.

The "bubbles" metaphor used to represent nested JavaScript scopes.

Methods

The authors developed Scoped by integrating a source code editor with a specialized visualization based on the "bubbles" metaphor often used in programming education. The tool's features were driven by a requirements analysis of the 50 most popular Stack Overflow questions tagged with "scope" and "JavaScript," which highlighted identifiers and context as primary pain points.

System Architecture

The Scoped interface consists of three synchronized components:

  1. Circular Treemaps (A): The core visualization uses nested circles to represent the code hierarchy. Color coding indicates accessibility: green for the current cursor location, blue for accessible parent scopes (the chain), and grey for inaccessible scopes.
  2. Identifier Panel (B): This panel lists every variable, function, and parameter in the selected scope, using icons to indicate their origin (e.g., global vs. local).
  3. Source Code Editor (C): A standard editor where cursor movement triggers real-time updates in the map and identifier list.
The Scoped interface components: (A) Circular Treemap, (B) Identifier Panel, and (C) Source Code Editor.

Key Results and Insights

A pilot user study with five participants followed a "think-aloud" protocol to evaluate the tool's effectiveness. The study yielded several key insights:

  • Verification over Discovery: Participants primarily used the tool to verify and correct their assumptions about variable origins rather than just discovering new information.
  • Visual Efficiency: Users found the spatial representation of hierarchy faster to process than scanning text for curly braces, noting that it was "easier than looking through the code."
  • Task Success: The tool effectively aided in identifying global identifiers and locating bugs related to variable shadowing—a common logic error where a local variable hides a parent variable.
  • Navigation Preference: Participants preferred clicking the circles in the visualization to navigate the scope hierarchy rather than scrolling through the text editor.

Limitations and Future Work

While the prototype was well-received, participants noted a lack of text labels in the visualization, making it difficult to distinguish between multiple nested functions. Future work will focus on improving scalability, adding navigation shortcuts from the identifier panel to the code, and expanding features to better address the this context. The authors also plan to conduct larger empirical studies to compare circular treemaps against other structures like sunburst diagrams.

Summary updated

BibTeX
@inproceedings{bacher2017scoped,
  author = {Ivan Bacher and Brian Mac Namee and John D. Kelleher},
  title = {{Scoped: Visualising the Scope Chain Within Source Code}},
  booktitle = {EuroVis (Short Papers)},
  year = {2017},
  doi = {10.2312/eurovisshort.20171143},
}
Figure