This document describes the steps in the refactoring of the team project "Minesweeper-1" in the Telerik Academy High-Quality Code Course 2015
-
Redesigned the project structure: Team "Minesweeper-1"
- Created a project
Minesweeper.Logic
- Renamed the namespace
Mini
toMinesweeper.Logic
- Introduced the following namespaces:
Minesweeper.Logic.Boards
, containing the Board object and settings definitionsMinesweeper.Logic.Cells
, containing the Cell object definitionsMinesweeper.Logic.CommandOperators
, containing Play, ShowScoreboard, Exit and Restart command handling definitionsMinesweeper.Logic.Common
, containing common utils, constants and enumerations used in the entire Logic namespaceMinesweeper.Logic.Contents
, containg the cell contents and cell contents factory definitionsMinesweeper.Logic.Data
, containing the text file with the soreboard entriesMinesweeper.Logic.DataManagers
, containg JSON parsing and serializing logic, as well as encryption/decryption and file reading and writing logicMinesweeper.Logic.DifficultyCommands
, containing definitions of chained game modes with successors and predecessors aimed at providing an interface for implementing menu logic in some UI formMinesweeper.Logic.Engines
, containing the interfaces for an engine operating the different modulesMinesweeper.Logic.InputProviders
, containing the interfaces for an input providing module logicMinesweeper.Logic.Players
, containing the Player object definitionsMinesweeper.Logic.Renderers
, containing the interfaces for a rendering module logicMinesweeper.Logic.Scoreboards
, containing the Scoreboard object definitions
- Renamed the namespace
- Created a namespace
UI
:- Created a project
Minesweeper.UI.Console
- Introduced a class
ConsoleInputProvider
implementing theIInputPovider
interface from the logic, which uses the Console to receive input - menu item selection, cell selection, board and game interaction - Introduced a class
ConsoleRenderer
implementing theIRenderer
interface from the logic, which uses the Console to write output - the board, scoreboard and game messages - Introduced a class
ConsoleMenuHandler
which is a console-based class used for rendering a menu and introducing menu item-selecting logic for better UX - Introduced a class
StandardOnePlayerMinesweeperEngine
which utilizes theStandardGameInitializationStrategy
by filling the board using a concrete step-by-step algorithm
- Introduced a class
- Created a project
Minesweeper.UI.WPF
- For now it is just an example of how easy it can be implementing entirely different means of receiving input and sending output
- Created a project
- Created a project
-
Reformatted the source code:
Generally:
- Removed all unnecessary
using
directives - Moved all
using
directives inside thenamespace
s - Removed all unnecessary whitespace
- Removed all inadequate comments
- Removed all bad statics from the classes
- Refactored all one-line block statements
- Added angle brackets to one-line block statements
- Removed the bad
goto
- Abstracted as much as possible all class references
- Separated the class logic in accordance with Single Responsibility Principle
- All classes now conform to the Open/Closed Principle
- Inheritance chains conform to the Liskov Substitution Principle
- All interfaces conform to the Interface Segregation Principle
- Applied Dependency Inversion to the complex classes which have more or less a lot of class dependencies
- Character casing: variables and fields made camelCase; types and methods made PascalCase
- Formatted all other elements of the source code according to the best practices introduced in the course “High-Quality Programming Code”
- Achieved all dependencies through interfaces and abstract classes for a higher level of abstraction
- Removed 90% of the initial logic - impossible to split the spaghetti
The console rendering class:
- Changed the rendering symbols
- Added color to the rendered symbols
- Added more sophisticated board borders
- Replaced all inadequate game messages with better
- Removed all unnecessary
-
Implemented the following patterns:
- Creational:
- Singleton: The façade
- Simple Factory:
ContentFactory
class, producing Cell Content - Fluent Interface: The
Cell
class - Lazy Initialization: The façade
- Structural:
- Façade: The
Game
class - Bridge: Rendering and input providing logic is abstracted through interfaces
- Adapter: For the NetEncryptionLibrary
- Façade: The
- Behavioral:
- Strategy: The board initialization with empty cells and bombs
- Observer: The Engine observes the state of the board
- Command: The input is being translated to engine instructions by being processed by their corresponding commands
- Chain of Responsibility: The different stages of "reading" the play command - is the command valid, is it inside the board, is it a bomb, and finally - reveal the cell
- Creational:
-
Introduced tests for
Minesweeper.Logic
andMinesweeper.UI.Console
- Achieved around 90% code coverage
-
Refactored the newly designed source code the core -
Minesweeper.Logic
- Maintainability Index - 92/100
- Cyclomatic Complexity - 211
- Depth of Inheritance - 2
- Class coupling - 62
- Lines of Code - 239
- Around 80% Maintainability Index for
Minesweeper.UI.Console