Tales Of TDD: The Big Refactoring
Tags: unittests, tdd, testing, softwaredesign
Published: 2022-02-02 09:22:05

I’ve heard that you’ve been working on this new feature for the payroll system based on the newly voted government legislation?

_Yes, I am._

How is it going? Seems like a major change.

_Well, I’m glad you asked. I’ve refactored a large part of the domain based on some new insights I’ve had. This morning I’ve pushed all the code to the git repository. It took me almost two weeks to make the necessary changes._

Great!

_However, I could use some help though._

Sure thing. How can I help?

_You see, in order to refactor the code base I commented out most of the tests. I just hate it when they get in the way. Anyway, can you fix these tests? There shouldn’t be more than 350 of them. That way I’m able to start working on the next user story._

Uhm, … that’s … uhm … interesting.

_What’s interesting?_

A couple of things come to mind.

_OK. Tell me._

For starters, you mentioned the term _refactoring_ a couple of times. I’m interested in the process that you call refactoring.

_A process?_

Yes. How did you take on this endeavour?

_Well, I did what I always do. I started out by changing a bunch of existing methods on several classes, adding and removing parameters, moving code around, etc. … I’ve also added a couple of new classes. Just the usual stuff._

How soon did you execute the tests?

_After a couple of hours I wanted to compile the source code. Then I noticed that a bunch of the tests didn’t compile anymore. There were just too many, so I’ve put them in comments. You know, this bothers me the most about tests. They prevent me from refactoring the code. Maybe we should just get rid of these unit tests. We still have the acceptance tests, right?_

So you didn’t run the tests?

_No. They didn’t even compile._

Did you execute the acceptance tests?

_Are you kidding me? They take more than two hours to run. I don’t have time for that. Besides, that’s why we pay the QA folks. It’s their job to make sure that the acceptance tests run smoothly. They’re also a mess. I won’t go anywhere near those._

In summary, you’ve been “restructuring” code for two weeks without executing any kind of automated tests?

_I’m afraid so. I could have done a better job on that department, which reminds me to try running the application later on to see if it still works. But I’m curious, you just said “restructuring the code”. Isn’t that the same as refactoring?_

Refactoring is a form of restructuring, but they aren’t necessarily the same thing. Refactoring is making small adjustments to the code without altering its observable behaviour. By adding up all these small changes you basically end up with a bigger change.

_I don’t get it. That’s exactly what I’ve been doing. It’s not that I changed thousands of lines of code at once._

There are some very important aspects that I didn’t mention yet.

_Ok. What are those aspects then?_

The most important aspect of all when it comes to refactoring is to execute the tests after each and every change, no matter how small. That way you know that the system still works as expected. Whenever a test fails, you also know exactly where to find and fix the issue.

_I’ve tried that once while doing a code kata with a bunch of people. It made me feel ridiculous. I mean, the code changes became so small that it was almost impossible to mess them up._

That’s what you want, right?

_I don’t know. Maybe. But who has time for such things?_

You just mentioned that you want to try running the application to see if most of the features still work, right?

_Yes_

What are you going to do when you encounter something that doesn’t work?

_In that case, I’ll try to find the code for that broken feature, set out some breakpoints and start debugging to see what’s going on._

Do you have time for that?

_Uhm, … I guess so._

More often than not, a debugging session can easily waste a lot of time. Alternatively, by enabling a tight feedback loop we keep ourselves out of such a mess.

_Suppose that I only make small changes as you suggest, and I’m unable to find the issue whenever a test fails. Then I still have to debug the code right?_

Another important aspect is to also commit the code changes after each cycle. That way you’ll be able to revert to the last good commit and start over, only now using even smaller steps.

_So you’re saying that besides making only tiny changes, I have to commit those changes as well? Isn’t that a lot of overhead?_

Not if you want to have a fast feedback loop in place.

_Who ever said that I want to have a feedback loop?_

Well, didn’t you just mention that you wanted to move on to the next user story?

_Yes_

Why’s that? You didn’t even finish your current user story.

_I just want to work on something else. I’m kind of fed up with the current user story._

You could have moved on if you’d only made small refactorings, ensured that all the tests still passed and committed the code after each short cycle.

_I don’t understand._

Well, if you’d rigorously followed this short feedback cycle that I’ve been describing, then the code would always be in a releasable state, wouldn’t it?

_Yeah, maybe that’s true._

Be quick but don’t [hurry](https://principal-it.eu/2020/08/tdd-tales-stressed-and-always-in-a-hurry/).

_You always say things like that. I still have one question though._

Shoot!

_What if I don’t want to refactor a particular part of the code? What if I just want to replace it with some entirely new implementation? Surely I can’t fall back to having a short feedback cycle in that case, right?_

When you want to make changes on a larger scale, you can use the “Branch by Abstraction” technique.

_What’s that all about?_

Going back to your specific case, you develop the new part of the domain just as you would implement it from scratch. This way you can flesh out the design of the new code using Test-Driven Development alongside the existing implementation. When this is finished, you can gradually replace the old implementation with the new implementation at the “seams”.

_What’s a seam? I never heard about this in the context of software development before._

This is a term that Michael Feathers uses in his excellent book [Working Effectively with Legacy Code](https://www.goodreads.com/book/show/44919.Working_Effectively_with_Legacy_Code). According to his definition, a seam is a place where you can alter behavior in your program without editing in that place. This involves that some kind of abstraction layer is in place that is used by the client code. This abstraction layer enables you to swap out the old implementation with the new implementation.

_That way I can work on the new implementation using short feedback cycles. I’ll be able to commit the code without affecting the existing implementation, while also keeping the application in a deployable state._

Exactly.

_I’ll have to try that some time for one of the next user stories._

Let me know how that works out.

_I will. Now, about those 350 failing tests …_