Search
Close this search box.
Search
Close this search box.
Search
Close this search box.

Legacy Code: Extract-FirstUT-Cover-Refactor-TDD

Facebook
Twitter
LinkedIn
Pinterest
WhatsApp

Recently, I had the opportunity to work on legacy code with several teams from various organizations. I would like to share my experience.

We usually start by choosing a piece of code that is “painful”: changing frequently and “scary” to touch because of its complexity. We explain that our purpose is to make the code simpler, readable, and easy to change. Establishing the motivation for what we do is important!

In essence, the steps we take are:

  1.       Use extract method/rename to make the code more readable (specifically applicable for very long methods).
  2.       Write and execute the first unit test (that’s usually the toughest part) as described by Michael Feathers.
  3.       Add more unit tests until the area you want to refactor is satisfactorily covered.
  4.       Refactor to make the code more maintainable (working in very small steps, as described by Joshua Kerievsky).
  5.       Make the required change using TDD.

The purpose of (1) is to see the forest, not the trees. Long methods tend to be unreadable. Using the “extract method” helps you see clearly what’s going on. Once you gain vision, you can start to rename. Arlo Belshee talks about this.

As an example, look at these two statements:

At the first, if statement, we have extracted the condition to a method. Remember that what you do most of the time with code is read it. You need to make it readable.

Item (2), as mentioned above, is the difficult part. You need both to master the technique and have the resolution to do it. I usually do it with the entire team and so, together, we have the required courage.

For instance, take a look at this behemoth method.

Pure fun, eh? This is something I call an amusement park method. We usually start by trying to call it null. Sometimes it actually works and we have a first-unit test. Then we start to slowly fill in the parameters. Maybe instead of a null send an empty dictionary. Maybe instead of an empty dictionary send a dictionary with two entries. And if there’s no choice sometimes we run the actual application and serialize the parameters, to be deserialized in the unit test later.

Sometimes we change a method from private to public, sometimes we add a method to better control a member, and there are more vicious things we do. Sometimes it can take a whole morning to do this. However, once you understand this, it becomes very simple.

Then you start looking at coverage.

Once you have the first test, things start to move faster (3). You start adding more and more tests. You start looking at coverage reports to see which lines of code are covered and which aren’t. If something is not covered, you can add another unit test to cover it.

Now (4) we can start to make bigger changes. Once you have the unit tests in place you feel free. You make a small change, you run the test. Another small step and the tests run again. Some IDEs have plug-ins that run the tests every time something is changed.

This is the time to get better familiar with the automatic refactoring tools of your IDE. Make sure you are familiar with introducing parameters, fields, and variables. Extract class is a very nice one and so is the ability to convert a method to static and move a method. The trick here is to make as fewer manual changes as possible and move the code around fluently.

Many times by this point, there is a small disappointment. The code you feared in the morning now looks quite simple. The real challenge is making the code simple and solving the puzzle.

Now we reached the point when we can quite easily add some code to fulfill a new requirement (5). We can add a new test, see it fail, make the required change, see it pass, and maybe do a little refactoring. Nothing like the joy of seeing unit tests turn from red to green.

(the above are unit tests from a very nice exercise called Gilded Rose)

And that’s it.

Subscribe for Email Updates:

Categories:

Tags:

Agile Release Management
Continuous Integration
Agile Games and Exercises
SPC
ROI
speed @ scale
LPM
Sprint Planning
Introduction to ATDD
Risk Management in Kanban
Scrum With Kanban
Agile Assembly Architecture
Releases Using Lean
Jira
User stories
Code
Continuous Delivery
Process Improvement
Kanban Basics
Jira Cloud
QA
Operational Value Stream
Managing Risk on Agile Projects
Jira admin
Agility
PI Planning
System Integration Environments
Kaizen
Lean Software Development
Risk-aware Product Development
Product Management
SAFe DevOps
Story Slicing
Rapid RTC
An Appreciative Retrospective
Product Ownership
Professional Scrum with Kanban
Kanban Game
The Kanban Method
Test Driven Development
Agile Basics
Built-In Quality
Kanban
Scrum and XP
Lean Budgeting
The Agile Coach
TDD
Agile for Embedded Systems
Professional Scrum Master
Agile Delivery
Agile Techniques
Agile Development
chatgpt
Atlaassian
A Kanban System for Software Engineering
Scrum Master Role
POPM
Atlassian
GanttBan
ScrumMaster Tales
Scaled Agile Framework
Acceptance Test-Driven Development
EOS®
DevOps
Reading List
Kanban Kickstart Example
Planning
Managing Projects
RTE Role
Agile in the Enterprise
Agile Program
Agile Mindset
Lean Agile Management
Entrepreneurial Operating System®
Elastic Leadership
Covid19
Value Streams
Nexus vs SAFe
Frameworks
SA
Portfolio for Jira
AgileSparks
Software Development Estimation
BDD
Self-organization
Games and Exercises
Business Agility
Agile Testing Practices
ARTs
Lean Agile Organization
Agile Release Planning
Tips
Lean Risk Management
Legacy Code
Implementation of Lean and Agile
ALM Tools
Change Management
Development Value Streams
Engineering Practices
System Archetypes
ATDD
IT Operations
agileisrael
Video
Lean-Agile Software Development
AI
Agile Project
NIT
Continuous Planning
Slides
ATDD vs. BDD
Agile Project Management
Agile and DevOps Journey
lean agile change management
Lean Startup
Lean and Agile Principles and Practices
AI Artificial Intelligence
Spotify
Applying Agile Methodology
Large Scale Scrum
Software Development
Scrum Guide
PI Objectives
Nexus
Manage Budget Creation
System Team
Introduction to Test Driven Development
What Is Kanban
Achieve Business Agility
Agile Israel
Pomodoro Technique
Keith Sawyer
Enterprise DevOps
Agile Games
Program Increment
Lean-Agile Budgeting
LAB
Lean Agile Basics
Continuous Improvement
Principles of Lean-Agile Leadership
Scrum Master
Perfection Game
Release Train Engineer
predictability
Advanced Roadmaps
Kaizen Workshop
SAFe
Coaching Agile Teams
Professional Scrum Product Owner
WIP
Tools
Agile India
RTE
Kanban 101
Presentation
Scrum
Webinar
Sprint Iteration
Sprint Retrospectives
SAFe Release Planning
Effective Agile Retrospectives
Scrum.org
Artificial Intelligence
Daily Scrum
Agile Marketing
Continuous Deployment
RSA
Risk Management on Agile Projects
Agile Product Development
Lean and Agile Techniques
Amdocs
Implementing SAFe
Limiting Work in Progress
Agile
Certification
Agile Community
LeSS
Agile Contracts Best Practices
Hybrid Work
Scrum Values
Quality Assurance
Certified SAFe
Agile Risk Management
Agile Outsourcing
Nexus and Kanban
Legacy Enterprise
ART Success
speed at scale
Iterative Incremental Development
Lean Agile Leadership
Nexus Integration Team
Team Flow
Accelerate Value Delivery At Scale
Systems Thinking
Agile Israel Events
Agile Product Ownership
Jira Plans
Lean Agile
Nexus and SAFe
Scrum Primer
AgileSparks
Logo
Enable registration in settings - general

Contact Us

Request for additional information and prices

AgileSparks Newsletter

Subscribe to our newsletter, and stay updated on the latest Agile news and events

This website uses Cookies to provide a better experience
Shopping cart