Contextual Playground

**Context:** You have a Moldable Object, and you are ready to start exploring. page [⇒ explore]

**Problem:** When we code new behavior as methods, we must repeatedly change our context to incrementally develop the logic. Testing the code requires a separate setup. How can we efficiently prototype and test the new behavior?

**Forces:**

An editor for coding new methods typically provides no facilities for testing the code. Setting up code to prototype and test logic can be cumbersome. Writing tests first for parts of the logic of a complex method can be overkill.

**Solution:**

Prototype new behavior in the Playground of a Moldable Object, i.e., an Inspector on a live instance of the class. The Playground will be bound to the context of the instance, so self and all slots can be accessed exactly as they would in a running method. From the moldable object you can navigate to any parts of the instance, to explore the APIs, or to test experimental code. Code snippets that work as expected can then be copy-pasted to existing methods, or extracted to new methods using an Extract method refactoring.

**Steps:**

The following steps illustrate how the Inspector Playground can be leveraged to explore an object's state, experiment with code, extract methods, extract (test) examples, and add custom views to a live object. Suppose we are implementing a StackMachine class to simulate an RPN calculator. So far it just holds a stack slot initialized to an empty OrderedCollection , and nothing else. We want to implement operations to perform calculations with the stack machine.

[…] book

**Related patterns:**

Start with a Moldable Object, in order to get a live instance to prototype from.

**Known Uses:**

The JavaScript console of a web browser can be used to explore and experiment with the behavior of live objects in a web page. You can also see this process at work in the short video Exploring the GitHub REST API in 7'.

~

Contextual playground book

DOT FROM two-level-diagram