mobile menu icon

Sprouting or wrapping?

Published by Manuel Rivero on 14/03/2025

Testing, Legacy Code


Introduction.

In situations in which we really don’t have time to cover & modify, we can use techniques that allow us to write tested code somewhere else and use it from existing code doing minimal changes to it. These techniques must be used carefully because, even though they enable us to add tested code, its usage is not tested. This is not ideal but it’s still much better than giving up and using edit & pray[1].

There are two such techniques: wrapping and sprouting.

We have noticed that sometimes people find it difficult to know when to apply one or the other.

This post will only focus on discussing when to apply each technique[2].

Sprouting or wrapping?

We choose either sprouting or wrapping depending on the location where the requested change needs to be applied.

When to use sprouting.

If after identifying where we need to add a new behaviour, the change point is in the middle of an existing behaviour, we will use sprouting.

Changes for which we use sprouting (figure from Changing Legacy Code training.
Changes for which we use sprouting (figure from Changing Legacy Code training).

When to use wrapping.

If the new behaviour happens either before or/and after an existing behaviour, i .e., the change point is before or/and after an existing behaviour, we will use wrapping.

Changes for which we use wrapping (figure from Changing Legacy Code training.
Changes for which we use wrapping (figure from Changing Legacy Code training).

Summary.

In this short post we have discussed when to apply wrapping or sprouting.

We’ve seen how we will apply wrapping or sprouting depending on in which part of the existing behaviour the new one is going to happen.

Once we identify the change points, if they are before or/and after the existing behaviour, we’ll use wrapping. If not, it means that the change point is in the middle of the existing behaviour, and then we’ll use sprouting.

Decision flow to choose wrapping or sprouting (figure from Changing Legacy Code training.
Decision flow to choose wrapping or sprouting (figure from Changing Legacy Code training).

Remember, wrapping and sprouting are techniques that allow placing changes in new tested code without having to cover existing behaviour. As such, they are shortcuts for when we don’t have enough time to cover & modify. This is not ideal but it’s still much better than giving up and using edit & pray.

Acknowledgements.

I’d like to thank Antonio de la Torre and Fran Reyes for revising drafts of this post.

I’d also like to thank Greta Hoffman for the photo.

References.

Notes.

[1] If you are not familiar with the terms cover & modify, edit & pray, wrapping or sprouting, you may want to read Working Effectively with Legacy Code book or have a look at our Changing Legacy Code training.

[2] If you don’t know how to apply them, have a look at the following links for quick recipes, wrap method, wrap class, sprout method and sprout class, or, even better, read chapter 6, “I Don’t Have Much Time and I Have to Change It” of Working Effectively with Legacy Code book.

Volver a posts