Skip to main content
Ben Nadel at cf.Objective() 2013 (Bloomington, MN) with: Johnson Tai
Ben Nadel at cf.Objective() 2013 (Bloomington, MN) with: Johnson Tai

GildedRose Refactoring Kata In ColdFusion

By
Published in

In a recent YouTube discussion, I heard someone mention the GildedRose refactoring kata popularized by Emily Bache. Her kata repository has dozens of different language implementations; but it doesn't have a ColdFusion / CFML version. As such, I wanted to try and build one myself. The goal of the kata, in part, is to use tests to aide in refactoring. But, I'm not a huge test person; so I wanted to build my own isolated kata repository for CFML to see what I could get working before I try to submit anything official.

View my GildedRose project for CFML on GitHub.

The GildedRose is an imaginary Inn that sells products that degrade in quality day-by-day. In the current state, the inventory for this Inn is managed by a set of wildly nested if-conditions. The main gesture of the refactoring kata is to take this inventory management method, updateQuality(), and turn it into something that's easy for ColdFusion developers to maintain going forward:

Note: I've purposefully left this code as ugly as possible (especially with regard to white-space usage and parenthesis grouping) in order to underscore how terrible the code is.

component {

	// ... truncated ... //

	public void function updateQuality() {

		for (var item in this.items) {
			if (item.name != "Aged Brie" && item.name != "Backstage passes to a TAFKAL80ETC concert") {
				if (item.quality > 0) {
					if (item.name != "Sulfuras, Hand of Ragnaros") {
						item.quality = item.quality - 1;
					}
				}
			} else {
				if (item.quality < 50) {
					item.quality = item.quality + 1;
					if (item.name == "Backstage passes to a TAFKAL80ETC concert") {
						if (item.sellIn < 11) {
							if (item.quality < 50) {
								item.quality = item.quality + 1;
							}
						}
						if (item.sellIn < 6) {
							if (item.quality < 50) {
								item.quality = item.quality + 1;
							}
						}
					}
				}
			}

			if (item.name != "Sulfuras, Hand of Ragnaros") {
				item.sellIn = item.sellIn - 1;
			}

			if (item.sellIn < 0) {
				if (item.name != "Aged Brie") {
					if (item.name != "Backstage passes to a TAFKAL80ETC concert") {
						if (item.quality > 0) {
							if (item.name != "Sulfuras, Hand of Ragnaros") {
								item.quality = item.quality - 1;
							}
						}
					} else {
						item.quality = item.quality - item.quality;
					}
				} else {
					if (item.quality < 50) {
						item.quality = item.quality + 1;
					}
				}
			}
		}

	}

}

I haven't performed any refactoring yet - I wanted to build the "starting state" for the repository first. Plus, I thought it would be fun to share this code to see if anyone else wanted to try the kata for themselves. I intend to do the refactoring in a branch so as not to mess with the starting state.

The project can be run using Lucee CFML with CommandBox. The instructions for running it are included in the repository README file.

The Lucee CFML app contains a single page (index.cfm) that executes a text-based comparison of the CFML output against a "golden master" of the "known good output" of the application. This comparison allows us to confidently refactor the CFML code without impacting the outcome of the applied business logic.

I haven't look at any other public refactoring of this code yet because I want to come at it with fresh eyes. Hopefully I can get to this tomorrow.

Want to use code from this post? Check out the license.

Reader Comments

Post A Comment — I'd Love To Hear From You!

Post a Comment

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel
Managed hosting services provided by:
xByte Cloud Logo