Chapter 16 - Refactoring SerialDate
-
Chapter is about a professional review of a code implementation of the SerialDate in Jcommon, follows the similar structure in previous chapter 15.
-
David Gilbert created this class when there is already a java.util.date and .calendar is because it is sometimes too precise. SerialDate is used to represent a particular day without concerning the time of day.
-
Robert Martin creates own independent tests because the current tests don't cover everything
-
Refactoring Steps: (Refer to the code found in the book)
- There are a couple of tests that are failing and Robert is trying to fix make those tests pass before refactoring
- Updated lines ~459 to use equalsIgnoreCase() instead of equals().
- Lines 685 the condition is > but fixed with >=
- Couple of boundary condition error fixes in the code. Fixing the equality statements.
- Boundary Condition Error is an error concerning edge cases of data handling. Example using a maximum value in a sum function which then causes an overflow.
- Passing Exception instead of error string allows tests 417 and 429 to pass. Now that all tests are passing, it is time for refactor.
- Javadoc comments has 4 languages in it, java, english, javadoc and html. Wraps entire comment with a pre element
- Robert thinks the class should just be called 'DayDate' because it is an abstract class and because 'Serial' means product id markers.
- Instead of MonthConstants convert the month constants into an enum. There were a lot of functions that relied on using the int for the month and had to be changed to use an enum
- Update variable names to be clearer on lines 97 to 100
- Found variables that don't belong in DayDate but should be moved to SpreadsheetDate
- Since the DayDate is an abstract class, it shouldn't know about the information about its implementation. Bad idea for base classes to know about their derivatives. Robert creates a DayDateFactory using the Abstract Factory pattern. To create instances of DayDate.
- Also creates the same pattern for SpreadsheetDateFactory
- Replace constants with enums
- Remove bad and redundant comments
- Moved variables and functions to where they belong
- Removed final keyword in arguments and variable declarations because it adds little value and creates clutter
- Connected if statements into single statement using ||
- Combining 2 functions that call each other into a single function
- Bad idea to pass a flag as an argument to a function if that flag selects the format of the output
- Renamed function names and make them more expressive and simplified
- Changed static methods to instance methods
- Read the code again from the top to see how it flows
- Moved static variables and methods into their own class. Abstract class will only contain abstract methods.
-
Small steps when refactoring and run the tests all the time.
-
The code has been cleaned, but can still be cleaner when the next person comes.