$30 off During Our Annual Pro Sale. View Details »

Stop Obsessing About Primitives

Ted M. Young
PRO
November 17, 2020
87

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 ted@tedmyoung.com https://ted.dev @JitterTed (Twitch, Twitter, YouTube) Discord: https://discord.gg/9XDfBSZ
  2. Ted M. Young https://maketestable.com/ @jitterted Ask Questions As You Need

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

  4. Ted M. Young https://maketestable.com/ @jitterted Primitives: What Are They? •

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

    Instance variables (fields) • Method parameters • Return values • Not local variables
  6. Ted M. Young https://maketestable.com/ @jitterted Primitives: What’s Wrong? • Mixes

    responsibilities • Not self-documenting • Leads to dispersed logic
  7. Ted M. Young https://maketestable.com/ @jitterted Fixing Primitive Obsession Create New

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

    at Coding Time
  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
  10. Ted M. Young https://maketestable.com/ @jitterted Enum: Turn Logic into Constant

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

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

    May Change Dynamically
  13. Ted M. Young https://maketestable.com/ @jitterted unconstrained number constrained type 1

    100 Wager int -2,147,483,648 2,147,483,647
  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
  15. Ted M. Young https://maketestable.com/ @jitterted Other Value Objects Money •

    Contains Currency and Amount Birthdate Color Identifiers • UserId, CustomerId
  16. Ted M. Young https://maketestable.com/ @jitterted Stateful Type Usage: Holds Mutable

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

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

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

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

    data in another object
  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
  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
  23. Ted M. Young https://maketestable.com/ @jitterted Further Constrain Data Exposure •

    Look for public methods that: • Can now be private • Return unconstrained types
  24. Ted M. Young https://maketestable.com/ @jitterted Questions? Ted M. Young Java

    Trainer, Coach, & Live Coder ted@tedmyoung.com https://ted.dev @JitterTed (Twitch, Twitter, YouTube)
  25. Ted M. Young https://maketestable.com/ @jitterted Thank You Ted M. Young

    Java Trainer, Coach, & Live Coder ted@tedmyoung.com https://ted.dev @JitterTed (Twitch, Twitter, YouTube) Discord: https://discord.gg/9XDfBSZ