Posts

Showing posts from 2023

GildedRose Refactoring in Ruby

Image
  There is dirty code and someone asked you to refactor it. Where will I start? What is the aim of refactoring code? Should I separate some classes and methods? I am gonna not mention the whole refactoring concept here. I will go by using an example. We will refactor step by step through the dirty code. Refactor consists of improving the internal structure of an existing program’s source code, while preserving its external behavior. I am gonna share directly the dirty GildedRose class for now. require 'test/unit' class GildedRose attr_reader :name , :quality , :days_remaining def initialize ( name, quality, days_remaining ) @name = name @quality = quality @days_remaining = days_remaining end def tick if @name != 'Aged Brie' && @name == 'Backstage passes to a TAFKAL80ETC concert' if @quality > 0 if @name != 'Sulfuras, Hand of Ragnaros' @quality -= 1 end end else