9 simple practices for writing better object-oriented code

Consider a fantasy game that must track a collection of items, each having a certain amount of quality (or value) that increases or decreases after time passes. This collection contains the following six items:

  • +5 Dexterity Vest
  • Aged Brie
  • Elixir of the Mongoose,
  • Sulfuras, Hand of Ragnaros
  • Backstage passes to a TAFKAL80ETC concert
  • Conjured Mana Cake

How much each item’s quality increases or decreases over time depends on an intricate algorithm that involves the quality and sell-in (days) properties of each item. Fortunately a prior C# developer encoded this for us. Let’s see what this algorithm looks like:

private static void UpdateQuality(Item item)
{
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.Equals("Backstage passes to a TAFKAL80ETC concert"))
{
if (item.SellIn < 11)
{
if (item.Quality < 50)
{
item.Quality = item.Quality + 1

--

--

Dennis "The Continuous Improver" Doomen
Dennis "The Continuous Improver" Doomen

Written by Dennis "The Continuous Improver" Doomen

Dennis is a veteran architect in the .NET space with a special interest in writing clean code, Domain Driven Design, Event Sourcing and everything agile.