Look at the Lightbulb


I've been reading Working Effectively with Legacy Code by Michael Feathers recently. I'm currently about half way through the book, and there has been a lot of good information so far.

The chapters I've read so far have largely been about changing legacy software via refactoring. There have been many times in my career that I've had to find ways to maintain and extend legacy code. I find that working with legacy code is a lot like taking care of a garden - the key is to do a little bit of work each day to nurture it and make it a little bit better. Overtime, this "tending to the garden" results in software that is easier to maintain and test.

Chapter five of the book focuses on tools that can be used to help enable changing of a code base. It introduces concepts like unit tests, mocking objects, and automated refactoring tools. While I find lots of value in creating unit tests and following techniques that help make code easier to test, I personally find a little bit of refactoring can have a great ROI when attempting to understand a newly acquired code base.

Many IDEs contain lots of automated refactoring tools. I strongly recommend taking a look at them. Both Visual Studio and Visual Studio Code will hide suggestions behind a lightbulb that shows up to the left of the line of code that is currently being edited. You can see the suggestions by clicking on the lightbulb, or via a keyboard shortcut (Alt + Enter on my machine). There are many suggestions behind that lightbulb worth following.

Two of my most used automated refactoring tools are Extract Method and Extract Interface. Extract Method in Visual Studio allows for a few lines of code to be extracted into its own private method in the class that is currently under edit. This helps me ensure that my methods follow the Single Responsibility Principle, and that the code is easier to read. Extract Interface allows for an interface to be created from a class that does not currently have one, and that often is the first step to breaking a dependency and putting a piece of code under test.

There's a lot on this topic that I will continue to share here on my blog. In the meanwhile, I'll leave you with a talk that I saw once by Sandro Mancuso that I think demonstrates the value of using automated refactoring tools. In this video, he refactors an old piece of Java code and puts it under test. He fully embraces the tools his IDE offers the entire way.

© 2023 Phil Busch. Crafted in Milwaukee, Wisconsin, USA.