Skip to content

Refactor: Modularize Gilded Rose Python Implementation#1

Open
krs-github16 wants to merge 12 commits intomainfrom
challenge/refactoring
Open

Refactor: Modularize Gilded Rose Python Implementation#1
krs-github16 wants to merge 12 commits intomainfrom
challenge/refactoring

Conversation

@krs-github16
Copy link
Copy Markdown
Owner

Summary

This pull request introduces a full, incremental refactor of the Gilded Rose Python implementation. The goal is to improve clarity, maintainability, extensibility, and correctness while preserving the original business rules through comprehensive testing.

Across a series of 12 commits, the legacy monolithic implementation has been progressively transformed into a modular, test-driven design.

Key Improvements

1. Introduced Comprehensive Tests

Added unit tests to preserve original behaviour and prevent regressions.

Added ApprovalTests for end-to-end behavioural validation.

Normalized fixture item names and expanded coverage

2. Reduced Deeply Nested Conditionals

Simplified initial logic by reducing nested if/else blocks to improve readability.

Clarified quality-change rules through small, targeted helper functions.

3. Added Item-Specific Update Helpers

Refactored update logic into explicit per-item functions:

_update_normal_item_quality

_update_aged_brie_quality

_update_backstage_pass_item_quality

_update_conjured_item_quality

Unified sell_in updates under _update_sell_in.

This eliminated duplication and clarified the behavioural differences across item types.

4. Centralized Quality Rules

Introduced:

MIN_QUALITY

MAX_QUALITY

LEGENDARY_QUALITY

Backstage threshold constants

Added centralized _normalize_quality to enforce bounds after every update.

5. Introduced a Dedicated Updater

Extracted a new class GildedRoseUpdater to encapsulate all update logic.

GildedRose now delegates updates to this updater, improving separation of concerns.

Future item types can be added cleanly by extending the updater.

6. Modularized the Codebase

Codebase reorganised into dedicated modules:

item.py — Item model

product_list.py — Enum for product identifiers

gilded_rose_updater.py — All item-specific update logic

gilded_rose.py — Orchestration and high-level update flow

debug_gilded_rose.py — Debug driver for manual inspection

Updated test suite structure under python/tests

This separation significantly improves readability and long-term maintainability.

Behaviour Preservation

All changes are validated by:

  • approval tests (golden master)
  • expanded unit tests / additional edge-case validation

Legendary items retain fixed quality (80)
Conjured items degrade at twice the normal rate
Backstage passes follow correct increasing behaviour and drop to zero after event

Why This Refactor Matters

  • clarifies the original business rules
  • provides a strong base for adding new item types
  • improves testability
  • reduces cognitive load in the update logic
  • establishes a maintainable module structure
  • follows clean-code practices (naming, separation of concerns, removing duplication)

…gic, and update tests

- Add min/max_quality, exceptional_items, and improving_items
- Simplify update_quality with capped increments for backstage passes
- Add ApprovalTests config and quiet fallback reporter for approvals tests
- Normalize test item names to 'Normal Item'
- Add legendary_items (previously exceptional), conjured_items (new change request) and quality bounds
- Simplify update_quality() logic and backstage increments

test(gilded_rose): expand tests and update approvals

- Add factory setUp, item creation tests and conjured tests
- Update approvals golden file for new fixture items
… with predicate and helper fucntions. Add MIN/MAX quality constants and backstage thresholds for clarity.

Factor item-type predicates and small helper methods (aged brie, backstage, conjured, legendary).
Implement legendary behavior (Sulfuras fixed at quality 80) and conjured extra degradation (degrades twice as fast).
Normalize/cap quality at entry and after updates to prevent out-of-range values.
Replace long if-chains with clearer increment/decrement helpers and targeted checks.
Add docstrings
… bounds, add conjured & legendary handling

Factor update logic into per-item helpers and add explicit normalization and conjured/legendary handling.

- Add MIN/MAX quality and normalize_quality() to bound item quality.
- Split update logic into _update_* helpers for normal/Aged Brie/backstage/conjured.
- Normalize Sulfuras to fixed LEGENDARY_QUALITY (80).
- Centralize sell_in decrement for non-legendary items.

See tests for edge cases (conjured semantics and normalization).
…tem update logic

- Add GildedRoseUpdater with quality bounds, Sulfuras handling (quality=80), and backstage thresholds
- Move update logic for normal, Aged Brie, Backstage, and Conjured items into updater
- Normalize quality and update sell_in consistently in one place
…r; rename update_quality -> update and update usages in tests and debug scripts
@krs-github16 krs-github16 self-assigned this Nov 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant