?

Test-Driven Development Guided Lab: Refactoring Existing Code

Bookmark
75 Launches
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.

Prerequisites:

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.

Goals & Objectives

In this lab, you will learn techniques to refactor the provided procedural code to allow making future changes to the code easier to implement without altering its current behavior. You will follow refactoring processes to break down the code into functions/classes with a single responsibility. Named refactoring methods that will be covered are:

  • Extract Function
  • Move Function
  • Replace Temp with Query
  • Encapsulate Record
  • Split Loop
  • Split Phase
  • Replace Loop with Pipeline

Hardware & Software

  • JavaScript (ECMA Script 6)
  • Node.js
  • Mocha
  • Chai.js

What's Next?

Learn more about Application Development, stay up-to-date with the industry and the new technology we have at WWT.