Tag Archives: Legacy Code

Legacy Coderetreat: Part 16 – Refactor Conditionals by Decomposing Conditional

Refactor conditionals by Decompose Conditional

Blog post series

This blog post is part of a series about legacy coderetreat and legacy code techniques you can apply during your work. Please click to see more sessions about legacy code.

IF-THEN-ELSE-END_flowchart

Purpose

We often see code with conditionals that are hard to understand. The purpose of this technique is to make the code look readable by extracting a function with the condition. It will be easier to test the code if the condition is extracted and injected as a dependency.

This technique is useful when having good variable names is not enough to explain the condition. We can have some of the following reasons for wanting to extract a function with the condition:

  • The condition is duplicated in several parts of the class
  • The condition is duplicated along different classes
  • Test the condition in isolation, because of its complex nature
  • Test the code without understanding how the conditions work (inject conditions as stubs)

 

Read More →

Legacy Coderetreat: Part 15 – Refactor Conditionals by Explaining Variable

Refactor conditionals: Explain Variable

Blog post series

This blog post is part of a series about legacy coderetreat and legacy code techniques you can apply during your work. Please click to see more sessions about legacy code. Refactor Conditionals - Explain Variable

Purpose

When you try to read a code base that has many conditionals, often the problem is that the condition itself is very hard to understand. This technique improves the conditionals by making them clear and very easy to read. Read More →

Legacy Coderetreat: Part 14 – Document possible Defects with tests

Document possible Defects with Tests

Blog post series

This blog post is part of a series about legacy coderetreat and legacy code techniques you can apply during your work. Please click to see more sessions about legacy code.

Purpose

When writing unit tests on legacy code, we often discover behaviours of the system that seem wrong. The main rule is: DO NOT change the production code without being absolutely sure that the change does not introduce defects. Instead we need to mark all the tests that characterize a possible defective behaviour. Let’s look at some ways to document possible defects with tests.

Document Possible Defects with Tests

Document Possible Defects with Tests

Read More →

Legacy Coderetreat: Part 12 – Extract class

Extract Class

Blog post series

This blog post is part of a series about legacy coderetreat and legacy code techniques you can apply during your work. Please click to see more sessions about legacy code.

Purpose

When working with existing code we need to look at the responsibilities of existing classes. If one class is too big, we need to extract new classes from that initial class. A class too big means that it’s not respecting the Single Responsibility Principle.

Extract Class

Extract Class

Read More →

Legacy Coderetreat: Part 11 – Refactoring Rule of Three

Rule of Three

Blog post series

This blog post is part of a series about legacy coderetreat and legacy code techniques you can apply during your work. Please click to see more sessions about legacy code.

Purpose

The rule of three says:

Extract duplication only when you see it the third time.

This concept is extremely useful when you want to improve legacy code, refactor in the TDD cycle or just improve existing code that is covered by tests.

The Rule of Three brings higher coherence and more clarity to the system, because the duplicated code starts to be moved into specialized areas. In this way we optimize the code base for changeability.

The Rule of Three prevents from prematurely extracting possible duplication and defers the duplication minimization until we have enough proof.

minimize-duplication

Read More →

Legacy Coderetreat: Part 10 – Extract pure functions

Extract Pure Functions

Blog post series

This blog post is part of a series about legacy coderetreat and legacy code techniques you can apply during your work. Please click to see more sessions about legacy code.

Purpose

We have some class that is very big. We want to divide the existing code into more methods and classes so that we have in the end some code that is easier to understand. If the methods are small, they are easy to change. In the same time the code would respect the Single Responsibility Principle. We optimize our software for changeability. In the following we will see how we can extract pure functions to achieve that.

Pure functions

Pure functions

Read More →

Legacy Coderetreat: Part 6 – Basic rules of refactoring

Basic Rules of Refactoring

Blog post series

This blog post is part of a series about legacy coderetreat and legacy code techniques you can apply during your work. Please click to see more sessions about legacy code.

Purpose

This technique is useful to minimize the mistakes one does while changing existing code. When refactoring existing code, one can introduce defects because of rushing through the changes.

During the next sections you will find some rules (guidelines) and some explanations about why these rules are useful.

 

Basic Rules of Refactoring

Basic Rules of Refactoring

Read More →

Legacy Coderetreat: Part 1 – Introduction

Legacy Coderetreat

Blog post series

This blog post is part of a series about legacy coderetreat and legacy code techniques you can apply during your work. Please click to see more sessions about legacy code.

A bit of history

On 26 November 2011 I had the honour of being an attendee at the second ever Legacy Coderetreat, which was supposed to be the first one in the world. But my friend Johan Martinsson from Grenoble beat Erik and me to it. Anyway I was part of the second ever Legacy Coderetreat in the world, facilitated by JB Rainsberger. JB had come with this concept of using the Coderetreat format, but for legacy code.

At the beginning of the day he presented us the problem, like in any other coderetreat. The problem was an ugly trivia game and you can find the sources here. At that time the code base was translated, from Java, only in a couple of other languages. Now you can find almost any language you want, thanks to the worldwide community of passionate developers who translated the code base.

JB facilitated the event like you could find in here, with a lot of details. But in short we would start with a free session, then follow with Golden Masters, continue with Subclass to Test, Replace Inheritance with Delegation and then Pure Functions. After each iteration we deleted the code like in any other coderetreat and swapped the pairs.

I liked a lot the fact that JB started this idea, because I am a big lover of working with legacy code. And this Legacy Coderetreat is such a good way to practice. But I had some other ideas on how to make it different.

legacy-coderetreat

Legacy Coderetreat

Read More →

We love legacy code!

We love legacy code!