Graduate Program KB

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)

    1. There are a couple of tests that are failing and Robert is trying to fix make those tests pass before refactoring
    2. Updated lines ~459 to use equalsIgnoreCase() instead of equals().
    3. Lines 685 the condition is > but fixed with >=
    4. 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.
    5. Passing Exception instead of error string allows tests 417 and 429 to pass. Now that all tests are passing, it is time for refactor.
    6. Javadoc comments has 4 languages in it, java, english, javadoc and html. Wraps entire comment with a pre element
    7. Robert thinks the class should just be called 'DayDate' because it is an abstract class and because 'Serial' means product id markers.
    8. 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
    9. Update variable names to be clearer on lines 97 to 100
    10. Found variables that don't belong in DayDate but should be moved to SpreadsheetDate
    11. 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.
    12. Also creates the same pattern for SpreadsheetDateFactory
    13. Replace constants with enums
    14. Remove bad and redundant comments
    15. Moved variables and functions to where they belong
    16. Removed final keyword in arguments and variable declarations because it adds little value and creates clutter
    17. Connected if statements into single statement using ||
    18. Combining 2 functions that call each other into a single function
    19. Bad idea to pass a flag as an argument to a function if that flag selects the format of the output
    20. Renamed function names and make them more expressive and simplified
    21. Changed static methods to instance methods
    22. Read the code again from the top to see how it flows
    23. 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.