Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Stop Obsessing About Primitives

Ted M. Young
PRO
November 17, 2020
110

Stop Obsessing About Primitives

How do you make your code more testable? One way is to make your classes smaller, so that they're more directly testable. An easy way to do this is to look for the Primitive Obsession code smell and create new classes that better represent your domain than plain old integers and Strings. Then the magic happens: you'll see behavior that's spread throughout your codebase is Feature Envy, another code smell, and the new class will pull in that behavior, providing a better abstraction and an easier way to test it.

Ted M. Young
PRO

November 17, 2020
Tweet

Transcript

  1. Ted M. Young https://maketestable.com/ @jitterted
    Stop Obsessing
    About Primitives
    from the MAKE YOUR CODE MORE TESTABLE series
    Ted M. Young
    Java Trainer, Coach, & Live Coder
    [email protected]
    https://ted.dev
    @JitterTed (Twitch, Twitter, YouTube)
    Discord: https://discord.gg/9XDfBSZ

    View Slide

  2. Ted M. Young https://maketestable.com/ @jitterted
    Ask Questions As
    You Need To

    View Slide

  3. Ted M. Young https://maketestable.com/ @jitterted
    Primitive Obsession
    The Code Smell

    View Slide

  4. Ted M. Young https://maketestable.com/ @jitterted
    Primitives: What Are They?
    • int, long, char
    • boolean
    • String
    • List, Map, Set

    View Slide

  5. Ted M. Young https://maketestable.com/ @jitterted
    Primitives: Where Are They?
    • Instance variables (fields)
    • Method parameters
    • Return values
    • Not local variables

    View Slide

  6. Ted M. Young https://maketestable.com/ @jitterted
    Primitives: What’s Wrong?
    • Mixes responsibilities
    • Not self-documenting
    • Leads to dispersed logic

    View Slide

  7. Ted M. Young https://maketestable.com/ @jitterted
    Fixing Primitive Obsession
    Create New Types

    View Slide

  8. Ted M. Young https://maketestable.com/ @jitterted
    Enum
    Usage: Limited Options Known at Coding Time

    View Slide

  9. Ted M. Young https://maketestable.com/ @jitterted
    Enum Examples
    • Playing Card Suit or Rank
    • Hearts, Clubs; Ace, 2, 3…, Queen, King
    • Game Outcomes
    • Beat Dealer, Lost to Dealer, Went “Bust”, Won Blackjack
    • Option Contract Type
    • Put or Call
    • Coffee Ingredient

    View Slide

  10. Ted M. Young https://maketestable.com/ @jitterted
    Enum:
    Turn Logic into
    Constant

    View Slide

  11. Ted M. Young https://maketestable.com/ @jitterted
    Enum:
    Turn Logic into
    Constant

    View Slide

  12. Ted M. Young https://maketestable.com/ @jitterted
    Value Object
    Usage: Many Options, May Change Dynamically

    View Slide

  13. Ted M. Young https://maketestable.com/ @jitterted
    unconstrained number
    constrained type
    1 100
    Wager
    int
    -2,147,483,648 2,147,483,647

    View Slide

  14. Ted M. Young https://maketestable.com/ @jitterted
    unconstrained date
    constrained type
    2021-01-15
    ExpirationDate
    LocalDate
    -999999999-01-01 +999999999-12-31
    2021-02-19
    2020-11-20 2020-12-18 2021-03-19 2021-04-16

    View Slide

  15. Ted M. Young https://maketestable.com/ @jitterted
    Other Value Objects
    Money
    • Contains Currency and Amount
    Birthdate
    Color
    Identifiers
    • UserId, CustomerId

    View Slide

  16. Ted M. Young https://maketestable.com/ @jitterted
    Stateful Type
    Usage: Holds Mutable State

    View Slide

  17. Ted M. Young https://maketestable.com/ @jitterted
    Stateful Types
    Inventory Level
    Blackjack Hand
    Order Quantity

    View Slide

  18. Ted M. Young https://maketestable.com/ @jitterted
    Refactoring to New Type
    InventoryLevel

    View Slide

  19. Ted M. Young https://maketestable.com/ @jitterted

    View Slide

  20. Ted M. Young https://maketestable.com/ @jitterted
    Feature Envy
    Behavior
    implemented
    against data in
    another object

    View Slide

  21. Ted M. Young https://maketestable.com/ @jitterted
    Fix Scalar Primitive Obsession
    1. Fix Feature Envy: gather usages via getter and setter
    2. Remove getter & setter via inlining
    3. Move scalar field to a new class
    4. Change old scalar to instance of new type
    5. Replace getter with new type’s methods

    View Slide

  22. Ted M. Young https://maketestable.com/ @jitterted
    Fix Collection Primitive Obsession
    1. Move collection field to new class via getter
    2. Look for Feature Envy (usages of new getter)
    3. Extract and Move Methods to new class
    4. Continue until no more accesses via getter
    5. Inline & remove getter

    View Slide

  23. Ted M. Young https://maketestable.com/ @jitterted
    Further Constrain Data Exposure
    • Look for public methods that:
    • Can now be private
    • Return unconstrained types

    View Slide

  24. Ted M. Young https://maketestable.com/ @jitterted
    Questions?
    Ted M. Young
    Java Trainer, Coach, & Live Coder
    [email protected]
    https://ted.dev
    @JitterTed (Twitch, Twitter, YouTube)

    View Slide

  25. Ted M. Young https://maketestable.com/ @jitterted
    Thank You
    Ted M. Young
    Java Trainer, Coach, & Live Coder
    [email protected]
    https://ted.dev
    @JitterTed (Twitch, Twitter, YouTube)
    Discord: https://discord.gg/9XDfBSZ

    View Slide