Refactoring the Sprite Classes
At this point the game has three different sprite classes that all share a lot of the same boilerplate code. This means it’s time to apply the Rule of Three.
As described by Wikipedia, the rule is:
Rule of three is a code refactoring rule of thumb to decide when a replicated piece of code should be replaced by a new procedure. It states that the code can be copied once, but that when the same code is replicated three times, it should be extracted into a new procedure. The rule was introduced by Martin Fowler in Refactoring and attributed to Don Roberts.
http://en.wikipedia.org/wiki/Rule_of_three_(computer_programming)
Even though Alien Invasion is a one-off game engine that isn’t intended to be turned into a general-purpose engine, it still pays to put in a little bit of time to refactor the code when it makes sense to clean up any rampant duplication and make the game easier to fix and extend.
No one writes perfect code the first time, especially when prototyping and trying out new features. When that code works, however, failing to refactor and clean up code during development leads to technical debt. The more technical debt you have on a project, the more painful it is to make changes and add new features. Refactoring can clean up technical debt by removing unused code, reducing code duplication, and cleaning up abstractions, all things that don’t make your game better necessarily but make your life as a game developer better.
In Alien Invasion ...
Get Professional HTML5 Mobile Game Development now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.