Category Archives: Code Cast

Legacy Coderetreat: Episode 13 – Document Possible Bugs with Tests – Code Cast

Document Possible Bugs with Tests – code cast

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.

Code Cast

This is a code cast in Java.

In the previous episodes we reach the moment when we extracted one simple class. We used the The Rule of Three and pure functions. This newly extracted class is covered with characterization tests. Then we wrote some unit tests in the following episode. But some of the behaviours seemed strange, and we thought they might be bugs.

Because we are never sure when working on existing code if some behaviour is a bug or a feature, we want to document all the suspicious cases. In order to do that, I am presentin three methods: use an annotation, prefix the test, use a diffent class.

Bug or feature? Check out the code cast to see all of them in action.

Read here more about this concept in my blog post.

Legacy Coderetreat: Episode 12 – Unit Testing on Legacy Code – Code Cast

Unit Testing on Legacy Code – code cast

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.

Code Cast

This is a code cast in Java.

In the previous episodes we reach the moment when we extracted one simple class. We used the The Rule of Three and pure functions. This newly extracted class is covered with characterization tests. But that is not enough, we want to continue adding other types of tests to understand the system better.

This is why during this episode you will see how to add unit tests to code extracted from a legacy code class. These tests have a small granularity level than the characterization tests we already have. This is the moment to dive more into details.

See this video to understand how you can document the current state of the system, by unit testing on legacy code.

Read here more about this concept in my blog post.

Legacy Coderetreat: Episode 11 – Extract Class – Code Cast

Extract Class – code cast

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.

Code Cast

This is a code cast in Java.

In the previous episodes we restructured some parts of the code base. We also extracted pure functions and applied The Rule of Three when restructuring the code.

Because we have extracted some pure functions, now we need to think if those pure functions belong to new classes. So we structure the functions in a way that they belong together from the point of view of their responsibilities. After structuring the functions we extract the class.

Learn from this video how to extract a class in legacy code in such a way that you do not introduce side-effects to the existing behaviour.

Read here more about this concept in my blog post.

Legacy Coderetreat: Episode 10 – Refactoring the Rule of Three – Code Cast

Refactoring the Rule of Three – code cast

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.

Code Cast

This is a code cast in Java.

When refactoring legacy code we need to make sure the structures we extract have a correct meaning. This is why The Rule of Three is very useful: extract to a new structure only when you find an identical duplication three times. Usually we do not have identical duplication, so in that case we need to make the duplication clear, check that it exists three times and only after that perform the extraction.

This code cast will show you also when it is not a good idea to extract a new variable, even if you see it duplicated many times. This is a mechanical kind of refactoring, but reasoning is very important even when we want to get fast by getting mechanical refactorings into our tool bag.

Read here more about this concept in my blog post.

Legacy Coderetreat: Episode 9 – Extract Pure Functions – Code Cast

Extract Pure Functions – code cast

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.

Code Cast

This is a code cast in Java.

In the first episode we wrote some characterization tests with the purpose to create a basic safety net. Now we continue to refactor the code, starting from there. We already have some tests running and we want to have methods that do only one thing; we want to apply the Single Responsibility Principle. A really good tool that can help is using pure functions. We extract functions, make them static and then make sure the function does not have any side effects. These functions help me clean the code and help me start structurig the code according to clear responsibilities. At a later time we will extract all these functions that have similar responsibilities to a new class.

Read here more about this concept in my blog post.

 

Legacy Coderetreat: Episode 8 – Use Mocking Framework – Code Cast

Use Mocking Framework – code cast

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.

Code Cast

This is a code cast in Java.

In this code cast you can see how to start using mocking framework on an existing code base. Once we have an interface, we can start using the mocking framework to test external dependencies.

After extracting new and clear methods from the messy code and after extracting classes, we now have a code base with clearer dependencies. However, once we understood how to do dependency inversion, testing the core system becomes a bit more difficult. We need to write tests to show how the core system collaborates with the dependencies we inverted. For testing collaboration with inverted dependencies we use mocking framework.

Read here more about this concept in my blog post.

 

Legacy Coderetreat: Episode 7 – Extract and Override – Code Cast

Extract and Override – code cast

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.

Code Cast

This is a code cast in Java.

Extract and Override is a very useful technique to break static dependencies. Whenever we cannot write tests for a piece of code, it is often because we have static dependencies. In the code cast below you can see how to use extract and override having a random number generator very tightly coupled. Because of that, we cannot have the same output for some clear steps. The solution found in this video is to extract the random to a new method and then override the random generator, just for tests. In this way we have predictability for tests.

Read here more about this concept in my blog post.

 

Legacy Coderetreat: Episode 6 – Dependency Inversion – Code Cast

Dependency Inversion – code cast

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.

Code Cast

This is a code cast in Java.

During this code cast you will see how to use the concept of Dependency Inversion on a legacy code base. Dependency Inversion is one way of transforming a tightly coupled system into a system that has a core and many small external dependencies. These external dependencies can be called also plugins. Read more about this concept in my previous blog post.

 

Acknowledgements

Many thanks to Thomas Sundberg for proofreading this post.

Legacy Coderetreat: Episode 5 – Basic Rules of Refactoring – Code Cast

Basic Rules of Refactoring – code cast

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.

Code Cast

This is a code cast in Java.

We start by checking what can be extracted to another method. The Basic Rules of Refactoring describes the detailed steps I use.

To ensure that we have a safety net, let us start with covering the code with some system tests and then write tests on the newly created method. These are also characterization tests, but they are often simpler than the system tests we wrote before. When the new method have been covered by tests, call it from the initial place where the code was copied from. Run all the system tests. At the end do some manual testing to see that everything works fine.

This technique is very useful to have small and safe steps while refactoring. Remember to never cut and paste. Always duplicate the implementation, cover it with tests and then reroute the call to the new code.

 

 

Acknowledgements

Many thanks to Thomas Sundberg for proofreading this post.

Legacy Coderetreat: Episode 4 – Add Feature on Legacy Code – Code Cast

Add feature on legacy code – code cast

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.

Code Cast

This is a code cast in Java. We have a new feature request and we need to add it to a legacy code base.

We start by understanding what we need to change. After that we run the existing system tests, that we wrote during the session From Nothing to System Tests. We use a test coverage tool to understand how much of the code is covered with tests. After this step we start writing new system tests until all the code
that will be changed is covered by tests.

When all of the system is covered with tests, we refactor the code to make room for a new feature. We extract a method, then a class and then an interface. The interface is implemented in a new way so that we add the new functionality. Adding the new functionality is done with tests written up-front.

At the end we write one system test to verify that the whole system can work well with the new feature. This is a kind of partial acceptance test that helps us understand if all the system works well together. And of course, manual testing and refactoring is essential before we can say we are done.

See more about the technique in the blog post Add Features on Legacy Code.

 

 

Acknowledgements

Many thanks to Thomas Sundberg for proofreading this post.