Test-Driven Development Guided Lab: Refactoring Existing Code

Solution Overview

TDD is an effective practice for writing code, but what if the code is in place already? Worse yet, what if there are no tests (or very few tests) checking the behavior of the code? This is a situation often faced by software engineers as they inherit "legacy code." Legacy code carries a stigma of being difficult to change and/or maintain. This is usually due to the fact that the logic that it was designed to provide was not passed to the engineer who becomes responsible for the code. In that sense the true behavior of the legacy code is unknown, and it becomes a black box that cannot be altered due to fear of repercussions.

This fear does not have to exist, and legacy code can be adopted and improved through refactoring practices. This lab will walk you through the process of adding "characterization tests" to code that has been provided for you, and then guide you through several refactoring cycles to make the code easier to understand and maintain. Through this, you will learn and practice effective refactoring techniques.


This lab requires a working knowledge of JavaScript (specifically ECMA Script 6) syntax. It can be launched by anyone who is interested in the content of the lab, however, it may be challenging to complete without some prior debugging experience. There are a few key points in the lab where more complete code blocks are provided that should get you back on track if things are not working as expected.

Lab Diagram