Where TDD falls short, find yourself a good REPL
Tags: clojure, programming, productivity
Published: 2020-11-10 16:41:04

---
title: Where TDD falls short, find yourself a good REPL
published: true
canonical_url: https://danlebrero.com/2020/09/16/where-test-driven-development-falls-short-find-a-good-repl/
tags: Clojure,programming,productivity
description: TDD is an awesome workflow, but not for everything. 
cover_image: https://danlebrero.com/images/blog/tdd-repl/learning-exploration-doing.jpg
series: Efficient Development
---

Test-Driven Development (TDD) is by far the [best workflow](/danlebrero/other-reasons-for-tdd-baby-steps-roi-evil-manager-syndrome-and-watching-the-watchmen-lpg) to get things done. 

It has but one little problem: before you start doing, you need to understand and learn what must be done.

![learning before doing](https://danlebrero.com/images/blog/tdd-repl/learning-doing.jpg)  

The TDD workflow by itself does not help with the learning, but as most of our work is about changing existing code, the byproduct of TDD -- tests --, can help us to learn how things are working now.

*Side note: Unfortunately tests usually do not help to understand why things are the way they are.*

For novel work, neither the TDD workflow nor the existing test suite is of any use. For novel work, we need to use other sources and tools to acquire that knowledge.

We can split novel work into:

1. New business logic.
1. Unfamiliar libraries and frameworks.
1. Side effects.

## New business logic

To learn about the new business logic to implement, you cannot start by writing tests, but neither can you start writing production code. 

You will need to sit down with your business expert counterpart. 

As you implement the new logic, you will need clarification, you will find edge cases, you will discover contradictions with the existing logic, ... all of which will require again of the business expert input, creating a loop between *doing* and *learning*:

![learning doing loop](https://danlebrero.com/images/blog/tdd-repl/learning-doing-loop.jpg)  

To make this feedback loop fast, the [on-site customer practice](http://www.extremeprogramming.org/rules/customer.html) may help you.

But your business expert will not have all the answers, and as part of the learning, you will together explore what is possible, what data would be required, what third parties should we integrate, what edge cases we will need to cover, ...

![exploration](https://danlebrero.com/images/blog/tdd-repl/learning-exploration.jpg)  

This exploration phase requires you to read loads of documentation, dig into databases, investigate third-party APIs, mix and match internal services, and sniff into your event queues.

And a REPL is the best tool for this.

## Unfamiliar libraries and frameworks

Another area where you cannot jump into TDD is when you are using some library for the first time, or when using some part of a library that you are not exactly sure how it behaves.

For a long time, I used to write "assertion-less tests" with little pieces of code to exercise the library that I wanted to learn about and then set debugging breakpoints to inspect the data that was coming in and out or peak into the state of the system.

I would write dozens and dozens of those "exploratory" examples until I was confident enough to start *doing*. But as I was *doing*, I would need to go back to this technique when my knowledge of the library would falter again, moving from *doing* to exploring several times per day:

![learning, doing, exploration loop](https://danlebrero.com/images/blog/tdd-repl/learning-exploration-doing.jpg)  

I wrote that exploratory code in the form of tests for no other reason than the IDE made it very easy to run them.

And a REPL is the best tool for this.

## Side effects

Side effects make it very obvious all of our testing efforts are based on lies. 

Mocks, stubs, fakes, spies: all lies to make our testing feedback loop [robust and fast](/danlebrero/good-tests-bad-tests). 

*Side note: All types of testing are based on lies. Some lies are more truthful than others, but your unit tests are a lie, your TEST environment is a big lie, your performance tests are a lie. The only real testing is actual users bashing your production system, but we call this type of testing <a href="/danlebrero/testing-or-monitoring-mtbf-or-mttr-make-your-choice">"monitoring"</a>.*

The best thing is to avoid lies altogether. Why mock a database if running a local database can be robust and fast enough for your testing?

A lot of times this is not practical so we are forced to write those lies to keep our sanity. 

To make our lies truthful, you need to understand pretty well the behavior of both the external system and the libraries that we will use to interact with them.

For this, we need to go back to the exploration phase, where we write lots of little programs to exercise the external system, to understand its behavior and misbehavior so that we fill up our knowledge and convert those **unknown** side effects into **known** side effects that we are confident enough to mock, fake or stub. 

And a REPL is the best tool for this.

*Side note: This is why testing the frontend is so difficult. The side effect of the frontend is painting pixels on a screen and, no matter how many years of experience you have, this is a side effect that will never become a known side effect with so many screen sizes, browsers, and all their quirks.*

# What is a REPL? 

Is like a command-line shell but using your production language. 

It is like a Google DevTools console but within your IDE. 

It is like an all-powerful debugger in your production environment.

It is like having a fluid conversation with your application, without any interruptions.

## Not any REPL but a good REPL.

As not all REPL are born the same, you can read what to makes a [good REPL good](https://vvvvalvalval.github.io/posts/what-makes-a-good-repl.html), but I have a very simple test: 

> You have a good REPL if you write all your code (exploratory, proof of concepts, one-off scripts, tests, and production) using a REPL. 

Note that I said “using” a REPL and not “in” the REPL, which is an important distinction and what sets good REPLs apart.

## A REPL for exploring new business logic.

A REPL allows for your exploration to be done in the most efficient way: by [writing little programs](/danlebrero/a-more-efficient-developer-write-programs-for-himself) using a Turing complete language. 

And not any language, but your production language, the one you are the most familiar with, that makes you the most efficient, that you can use from your awesome IDE, and has all those libraries to connect to, dig into and inspect any data source.

## A REPL for exploring unfamiliar libraries.

To learn about unfamiliar libraries, you need to read their docs and write loads and loads of experiments.

As [James Ward](https://twitter.com/_JamesWard/status/1303530732615348224) puts it:
> Programming, no matter what level, is mostly trial & error. So before anything else, optimize for the shortest feedback loop possible.

With a REPL, the feedback loop is immediate: there is no build, compilation, deploy, or start-up delay. 

Compared with a script, a REPL allows you just chose which bits of the application or experiment you want to re-run, which makes it easier focus, instead of having to run a whole script all the time.

Once you get used to this feedback loop, even delays of a couple of seconds feel like ["living with lag"](https://www.youtube.com/watch?v=_fNp37zFn9Q). You want to have a conversation with your application, not one of those [Skype conversations](https://www.youtube.com/watch?v=j-WUJs0HYtI).

## A REPL for exploring side effects.

It is quite usual to use specific tools to explore databases and other external systems, but those tools do not compose.

By using a REPL, you can again use a full Turing complete language and mix and max data from any system.

As a big plus, the side effect of exploring side effects with a REPL is *production code* ready to be used, and data samples for your TDD workflow.

## A REPL for doing.

The immediate feedback loop is not only extremely useful during the exploration phase, but also for *doing*. 
 
And the most important, with a REPL you are carving your application from inside it, building it from zero without ever having to stop it, adding functionality in baby steps.

As we are constantly moving between *doing* and *learning*, using the same tool for both doing and learning removes the friction of switching between tools and makes the feedback loop way faster.

If you want to learn more about the REPL, maybe you want to watch [my talk about it](/danlebrero/repl-driven-development-immediate-feedback-for-your-backend-code-4b0o) or Stuart Halloway's [REPL Driven Development](https://vimeo.com/223309989).