Working Effectively With Legacy Code — Changing Software (Part 1: Chapter 4)

Birat Rai
3 min readFeb 28, 2019

--

This is Chapter 4 of the Working Effectively With Legacy Code series. If you haven’t read the previous Chapter 3.

Part 1 : Mechanics of Change:

Chapter 4: The Seam Model

A seam is a place where you can alter behavior in your program without editing in that place.

What is Seam in Software?

The analogy is a seam in clothing: A line where two piece of fabrics are stitched together.

A seam in software as a place where two parts of the code meet and where something else can be injected. The piece on each side only touches the other right at the seam.

Why Seams?

One of the biggest challenges in getting legacy code under test is breaking dependencies.

The seam view of software helps us see the opportunities that are already in the code base. If we can replace behavior at seams, we can selectively exclude dependencies in our tests.

We can also run other code where those dependencies were if we want to sense conditions in the code and write tests against those conditions. Often this work can help us get just enough tests in place to support more aggressive work.

Enabling PointEvery seam has an enabling point, a place where you can make the decision to use one behavior or another.

Types of Seam

The types of seams depends on the programming language we use. Each types of seams are categorized by the steps involved in compiling the code.

  • Preprocessing Seams: C and C++ provides with preprocessing tool. This preprocessing, a macro preprocessor runs before the compiler. We can use the preprocessing seams to replace calls to another independent piece of code.
#include “localdefs.h” // Example of macro preprocessor which can be used to provide some definition to external independent code
  • Link Seams: In programming languages, when a source file contains an import statement, the compiler checks to see if the imported class really has been compiled. We import those classes from classpath environment variables for eg; JDK path. We can replace those imported class and alter their behavior during by using dynamic linking. This link seams, should be different for test and production environments.
  • Object Seams: They are the most useful seams available in object-oriented programming languages. Object seams are mostly used to isolate the behavior while testing.
Figure: Cell hierarchy. (Source: Working Effectively with Legacy Code, page 41;

Here, the Cell Object provides Object Seams, since, Cell is an abstract class, and the value of object depends which we can isolate during test.

public Spreadsheet buildMartSheet(Cell cell) { …cell.Recalculate();… }

It is important to choose the right type of seam, when you want to get pieces of code under test. In general, Object seams are the best choice in object-oriented languages.

Chapter 3: Legacy Series

Chapter 5: Tools

References:

--

--

Birat Rai
Birat Rai

No responses yet