Lift-Up Conditional on Gilded Rose
Using the “Lift-Up Conditional” refactoring to detangle complex if statements.
This is a powerful, satisfying and almost magical refactoring technique. It’s really useful for detangling complex knots of if statements. Llewelyn Falco showed the technique to Emily Bache, and she named it. There’s a good video by Emily demonstrating the technique here.
The basic principle is to find conditions that are repeated throughout the code and then “lift them up” so that each condition has its own dedicated branch of the code. The result is much simpler and easier to read.
In this learning hour we demonstrate the technique, and then let the participants try it out for themselves on the Gilded Rose Refactoring Kata. You can do this in one hour, but as the technique takes a bit of time to really understand, it’s a good idea to do this intro learning hour first.
The slide deck (also available as a pdf) contains all the resources on this page plus more - many of which may be useful in preparing for this learning hour.
Learning Goals
- Use Keyboard shortcuts to do Lift-Up Conditional on Gilded Rose
- Recognize when Lift-Up Conditional would be an appropriate technique to try
Session Outline
- 10 min connect: Favourite keyboard shortcuts
- 15 min concept: Lift-Up Conditional
- 30 min concrete: Do Lift-Up Conditional on Gilded Rose
- 5 min conclusions: What’s been learnt
Connect: Favourite Keyboard Shortcuts
As a whole group, ask participants to answer the question “What are your favourite keyboard shortcuts in [insert relevant IDE, eg Visual Studio]?”
You can ask them to write their answers on stickies using a tool like Miro, Mural or Jamboard, or in bullet points in a shared doc, or on actual stickies in person.
This is a good exercise to get them thinking about keyboard shortcuts, which will be useful in this exercise and are demonstrated in the videos linked to below. Hopefully some of them will learn new shortcuts as a result of this exercise.
Concept: Lift-Up Conditional Demo
I find it helpful to use schematics to visualise what’s happening, as if you haven’t seen the technique before it can be confusing at first. I also show the Gilded Rose code before and after, to show what we’re aiming for and get them primed for what I’m about to demonstrate.
This slide deck (also available as a pdf) contains two versions of the schematic above, as well as a more detailed example using the same principle. I recommend only using the more detailed version if you’re planning to devote two learning hours to this activity. Even then, you may prefer the shorter simpler version.
I follow the schematics with this video of a C# demo of the technique, played at double speed, with me talking over the top. This branch of the Gilded Rose kata contains a commit for every step I made in the video. The tests were passing at each commit. If you like, you can share this link to show them what they’re aiming for when they’re done. There is also this video of Emily Bache demonstrating the technique in Java, but it’s quite long so you might want to make your own or select only some short snippets to show.
Concrete: Do Lift-Up Conditional
In pairs, ask them to attempt the technique themselves on the Gilded Rose code base. Encourage them to move in small steps and make sure the tests pass after each tiny change. For C#, I give them this C# starting point. The following will get them moving as quickly as possible:
- All other languages have been removed from this branch, to avoid distraction and make it clear where to start
- The approval tests are set up for maximum coverage and will pass as soon as they open the code
- It’s best for participants to download the code and make sure they can build it and make the tests pass before you start the workshop (I have notes on how to handle any Visual-Studio-related gotchas)
I also added the above schematic and these instructions on using the approval tests to a central location (a Mural board and a Google doc) which all participants could access.
Conclusions: When should you use this?
Ask them to discuss, in pairs, when they would use this technique. A good outcome to aim for is at least one sticky note from each pair on a central board (or a bullet point added to a shared doc).