mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-04 17:21:38 +00:00
2.6 KiB
2.6 KiB
Gilded Rose Refactoring — Clean Code Edition (Python) Introduction This project is a clean-code refactor of the well-known Gilded Rose Kata. The goal of this refactoring effort was to apply python best practices, clean code, reduce cyclomatic complexity, and improve the maintainability and testability of the code. Key software design concepts such as the Open/Closed Principle (OCP), encapsulation, polymorphism, and modular architecture were used to restructure the core logic.
- Refactored
update_qualityUsing the Open/Closed Principle Originally, theupdate_qualitymethod inGildedRosecontained a complex set ofif-elifconditionals handling all item behaviors. This violated the Open/Closed Principle (OCP) by requiring changes to the method every time a new item type was added. We replaced this with a polymorphic structure, using item wrapper classes to handle item-specific behavior. Old style: if item.name == "Aged Brie": ... New style: wrapper = item_factory(item) wrapper.update_quality() - Introduced a Factory Function:
item_factory()The factory function is responsible for returning an instance of the appropriate wrapper class based on the item name. This makes the core logic inGildedRoseagnostic to item-specific rules and makes the system extensible without touching legacy code. - Created Dedicated Classes per Item Type Each item behavior is now encapsulated in a dedicated class that extends a base wrapper. The classes include: • AgedBrieItem • BackstagePassItem • SulfurasItem • ConjuredItem • DefaultItem This use of polymorphism keeps the rules modular, isolated, and testable.
- Maintained Interface Compatibility
The original
Itemclass anditemsproperty were left untouched as required. This ensured backward compatibility with existing code and met the constraints defined in the kata. - Restructured Codebase for Clarity Project structure: python/ ├── gilded_rose.py ├── item_wrappers.py ├── aged_brie.py ├── backstage.py ├── sulfuras.py ├── conjured.py └── tests/
- GitHub Actions CI/CD Pipeline
A GitHub Actions workflow was created to enforce code quality and automate tests. The workflow runs on pushes or PRs to the
mainandtest/my_changesbranches. Tasks include: • Code formatting (black) • Import sorting (isort) • Linting (flake8) • Static code analysis (pylint) • Type checking (mypy) • Unit + Approval tests (pytest,approvaltests) - Tests Refactored Tests were restructured into logical classes for each item type, improving readability and focus. Approval testing was preserved for regression validation.