This post is the first in a series of refactorings of the command line video store exercise. You can find the original version in Jave on Rick’s repository. I will be working on the corresponding C# version.
The goal of this series is to start with procedural code and turn it into object-oriented code. This series presumes you are using Resharper.
In this step, we will introduce a new Movie class and move the corresponding data there.
Before you start, I suggest you take a look at the full main class here.
The movies are loaded from a file:
The code uses a dictionary to store the movies by an integer key. You can see that a movie is represented by a primitive structure: an array of string. This array contains the following information about a movie:
- movie: the key
- movie: the name
- movie: the category
We want to replace this structure (primitive obession smell) with a Movie class that contains the data above.
To do so, we will proceed by introducing a parallel change: create a new dictionary of Movie instead of string.
The key to the parallel change is to find all writes to the old structure and duplicating those writes to the new structure. When all writes also go to the new structure, we can start replacing the reads from the old structure to the new one.
Introduce Movie class
To introduce the Movie class, duplicate the movies variable (Ctrl + D) to movies1 Dictionary<int, Movie>. Since the Movie class does not exist yet, the code does not compile. We will fix that soon.
In the loop where the movies dictionary is filled, we add a new Movie to movies1. The Movie constructor takes the three elements from the string array as parameters: key, name and category.
Once this is done, we are ready to generate the Movie class. Place the cursor on the Movie constructor and press Alt + Enter, Create class ‘Movie’ to let Resharper create a class for us:
Note that we introduced a local variable for key to avoid having the parse code three times inside the loop, and that we renamed the movie array to movieData for more clarity.
Now that we have the Movie class, we have to remove the NotImplementedException from the constructor.
By default, Resharper will create the new class inside the current file, so we will need to use Alt + Enter on the class name to move the class to a separate file.
We create and initialise fields by hitting Alt + Enter on each constructor parameter and by choosing the option Introduce and intialise field.
Since we know that these fields will be used by the MainClass, we can encapsulate them so that we have public getters. This is the result:
Now the code compiles again and the tests are still green.
Parallel change: replace reads
The next step is to replace the reads on the old string array structure with our new Movie accessors. In the while loop, we replace all reads to movie with the corresponding property from the movie1 instance of Movie.
Every thing looks good, the tests are still green. Now we can remove the reads and writes to the old structure, since we are now using the new one everywhere.
We notice that movie is not used anymore in the while loop: remove it. The old movies dictionary is now only used to add the old string array to it. Remove it as well. Now that the old structure is gone, we can rename movies1 and movie1 to movies and movie:
There is no behaviour that we can move to the Movie class, so we will leave it at that for this step.
In this step, we saw how to:
- recognise the code smell primitive obsession.
- quickly creating a new class by typing “new MyClass(parameters)” and using Resharper to generate the class.
- using the parallel change strategy:
- introduce a new structure,
- duplicate all writes to the old structure for the new structure,
- replace reads from the old structure to use the new structure,
- remove the old structure.
You can find the full code on this branch.
After this refactoring, the MainClass is still pretty messy and does too many different things. In the next step, we will introduce a new class for the concept of rental.