Some exercises have already been suggested in various sections of this chapter. Try those exercises. For example, split the unit tests so that you have separate modules for testing functionality from different classes. Add more unit tests to improve the code coverage. Also, try running nosetests
on the tests that we have already written.
There are several low-hanging fruits for refactoring! Review the AttackOfTheOrcs._occupy_huts
method. It creates hut objects, and puts an occupant in each of them. As the first step, you can rename it create_huts
. The code in this method could be better written. It uses if...else
conditions to decide which occupant to create. Although it works in this simple application, if you add other types of occupant (elves, dwarfs, wizards, and so on) it will become a maintenance headache.
What could we do here? One strategy is to let the Hut
class manage the creation of the occupant
object. The hut could ask a factory to randomly...