Open Source Code Smell Detective Solves Gilded Rose Kata

Open Source Code Smell Detective Solves Gilded Rose Kata

The Gilded Rose refactoring kata is a classic coding exercise that challenges developers to refactor legacy code while adding new functionality. Instead of jumping straight to a design pattern, let's take a methodical approach: identify code smells and fix them systematically.

In this article, I'll walk you through solving the Gilded Rose kata by acting as the Code Smell Detective—identifying each smell, understanding its impact, and refactoring it step by step. By the end, you'll have a clean, maintainable solution and a better understanding of how to approach legacy code refactoring. Note that I've previously published a solution to this kata using composition over inheritance, which manually applies the Strategy pattern. This article takes a different approach by using automated code smell detection to guide our refactoring process.

Credit to Emily Bache's GitHub repository for the excellent kata resources.

Instead of immediately reaching for design patterns, let's systematically identify and fix code smells. This approach helps us understand why the code is problematic before deciding how to fix it.

Before we start refactoring, let's use the code-smell-detector tool—the Code Smell Detective's automated analysis tool—to systematically identify all code smells in the Gilded Rose code. This automated analysis will give us a comprehensive view of what needs to be fixed.

The code-smell-detector is a Python application that performs static analysis on codebases, detecting code smells based on configurable thresholds. It analyzes metrics like cyclomatic complexity, nesting depth, lines of code, and more. Think of it as the Code Smell Detective's trusty sidekick that helps identify problems before we dive into fixing them.

First, let's set up the code we'll be analyzing. Create a new directory called gilded-rose-kata:

Then create gilded_rose.py and copy all the code from the code block in "The Problem" section above (both the GildedRose and Item classes). Note that GildedRose takes a list of Item objects via the items parameter in its constructor—this is the items property that we cannot modify according to the kata constraints.

First, we need to download and install the tool. Head over to the Code Smell Detective releases page and download the latest release. Once you've extracted the codebase, navigate to the directory and install it:

This installs the code-smell-detector command-line tool in editable mode, so you can use it from anywhere on your sy

Source: Dev.to