Replace Conditional With Polymorphism

This refactoring replaces conditional logic with classes and polymorphism. The code smell that leads you to it is having a lot of similar switch statements that switch on type. In this session we’ll practice doing it, and hopefully see just how good modern IDE tooling is for this kind of task.

Learning Goals

Session Outline

Connect - Refactoring and productivity

If you were advising a junior developer which tools to use for refactoring, what would you say?

Explain the Parrot exercise

Show the starting position of the Parrot Refactoring Kata and explain the code smell - the switch statement that switches by type. Explain and sketch the refactoring that addresses this smell: ‘Replace Conditional with Polymorphism’.

It’s also useful to run the tests and show they have good coverage and should be reliable for finding refactoring errors.

Demo Parrot

You want to help people become more productive at refactoring, and this demo is about inspiring them with what’s possible. Make it slick, make it fast. Put the tests on auto-run, or run them using a shortcut after every change.

It’s worth doing some research into the capabilities of your IDE here. In my experience, you can do nearly everything with shortcuts, almost no typing required. Not every language has equivalent support though.

Timebox the demo and stop wherever you’ve got to. Leave them scope for improvements they can come up with themselves, to keep it interesting.

I suggest you write the keyboard shortcuts you used on a whiteboard. You’d like them to refer to this list when they do the exercise themselves. For example you might end up with something like this (IntelliJ 2019.3.1, Java):

or (CLion 2022.2.3, C++)

Do Parrot

People should work in pairs to do the same exercise again from the start. If they lack IDE experience, they should follow your list of shortcuts from the whiteboard. More confident pairs may know other shortcuts and can try to achieve the same thing in a different way. Ask them to write down their sequence of steps if so.

People who are very skilled with the IDE already may complete the exercise very quickly. In that case ask them to do it with strict strong style pairing. The navigator will only say the high-level intent and the names of the refactorings. The driver is expected to know the shortcuts. They shoud do it twice, so each person experiences each role.

Reflect: IDE shortcuts and productivity

Ask the group:

Note the answers on a flipchart. You’re hoping they will say things like:

Put the flipchart up in the team area.