Code Smells - Introduction

It’s useful to have a vocabulary for talking about refactoring. It will help you to communicate when doing strong-style pairing or mob programming. Code smells is perhaps a less-known concept than refactoring and knowing these names will also help you to communicate about code, and identify targets for refactoring. In this learning hour we’ll talk about what a code smell is and learn the names of some common ones.

Session Outline

How do you know when you have bad code?

Have you ever encountered bad code? If so, what characteristics does it have?

Discuss in pairs and note down “bad code” characteristics. If you have never encountered bad code, note down why you think that is.

Note down your ideas and share them with the group when the facilitator asks.

Code smells

Explain Martin Fowler’s description of code smells. The list of Code Smells on this site uses the same names as Martin Fowler’s book ‘Refactoring’. There is another list of common code smells on wikipedia. Go through some of the descriptions of the most common smells they are likely to encounter. For example:

Find code smells

Have them review some code and note down any code smells they find. In particular, they should look for the common smells you just explained. The idea is just to read the code and identify smells, not do anything about it. Note that you can identify code smells without necessarily understanding the code or being able to safely change it.

Example katas that work well:

Ask them to work in small groups looking for smells. Give them a printout of the code that they can write notes on or attach sticky notes to. After a timebox of about 10 minutes, ask them to share what they have found. Tell them the ‘official’ names for smells if they don’t know them.

Repeat with another code sample or however many you have time for.

You are hoping that they will find smells like these:

What code smells does your production code often suffer from?

Are any of these smells common in your production code? Or are there other smells? Discuss in pairs and note down names of smells you have noticed in your production code. Collect the names on a shared whiteboard.

In the coming days as you work with your production code, keep a look out for smells and note where you find them.