From 3d28a742b597f61c8cedfc1107c0e84706f8f4cb Mon Sep 17 00:00:00 2001 From: Sarah Ashri Date: Wed, 13 Mar 2024 14:04:40 +1000 Subject: [PATCH] DailyItemUpdate: extract increase/decrease quality into a separate method --- csharpcore/GildedRose/GildedRose.cs | 59 ++++++++++------------- csharpcore/GildedRose/RefactoringDiary.md | 4 ++ 2 files changed, 29 insertions(+), 34 deletions(-) diff --git a/csharpcore/GildedRose/GildedRose.cs b/csharpcore/GildedRose/GildedRose.cs index 0a25ae91..74c37f1b 100644 --- a/csharpcore/GildedRose/GildedRose.cs +++ b/csharpcore/GildedRose/GildedRose.cs @@ -30,45 +30,42 @@ public class GildedRose private static bool IsRegularItem(Item item) => (!IsLegendaryItem(item) && !IsBackstagePassesItem(item) && !IsBetterWithAgeItem(item)); + + + private static void IncreaseQuality(Item item, int byValue) + { + item.Quality = int.Min(item.Quality + byValue, MaxQuality); + } - - - + private static void DecreaseQuality(Item item, int byValue) + { + item.Quality = int.Max(item.Quality - byValue, MinQuality); + } + + private void DailyItemUpdate(Item item) { if (IsLegendaryItem(item)) return; if (IsRegularItem(item)) { - if (item.Quality > MinQuality) - { - item.Quality = item.Quality - 1; - } + DecreaseQuality(item, 1); } if(IsBetterWithAgeItem(item) || IsBackstagePassesItem(item)) { - if (item.Quality < MaxQuality) + IncreaseQuality(item, 1); + + if (IsBackstagePassesItem(item)) { - item.Quality = item.Quality + 1; - - if (IsBackstagePassesItem(item)) + if (item.SellIn < 11) { - if (item.SellIn < 11) - { - if (item.Quality < MaxQuality) - { - item.Quality = item.Quality + 1; - } - } + IncreaseQuality(item, 1); + } - if (item.SellIn < 6) - { - if (item.Quality < MaxQuality) - { - item.Quality = item.Quality + 1; - } - } + if (item.SellIn < 6) + { + IncreaseQuality(item, 1); } } } @@ -79,23 +76,17 @@ public class GildedRose { if (IsRegularItem(item)) { - if (item.Quality > MinQuality) - { - item.Quality = item.Quality - 1; - } + DecreaseQuality(item, 1); } if(IsBackstagePassesItem(item)) { - item.Quality = item.Quality - item.Quality; + DecreaseQuality(item, item.Quality); } if(IsBetterWithAgeItem(item)) { - if (item.Quality < MaxQuality) - { - item.Quality = item.Quality + 1; - } + IncreaseQuality(item, 1); } } } diff --git a/csharpcore/GildedRose/RefactoringDiary.md b/csharpcore/GildedRose/RefactoringDiary.md index 50585b6c..a19b7d4f 100644 --- a/csharpcore/GildedRose/RefactoringDiary.md +++ b/csharpcore/GildedRose/RefactoringDiary.md @@ -11,6 +11,10 @@ NOTES: Some of the tests failed due to bugs I discovered in the code/existing tests (e.g. relying on exact name instead of the mentioned "code word"). I decorated these tests with `[Ignore]` until I get to a stage in the refactoring where I can easily fix the issues and re-introduce the tests. 2. small refactorings of the UpdateQuality() method to make it more readable and separate the different (unrelated) handling of the different types.
+3. Extract Increasing/Decreasing Quality into a method.
+It feels like Quality should have been a TinyType that manages its own limits. However, since we're not allowed to change Item, I'm just extracting a method. + + This is the state of the code now