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

Stop Obsessing About Primitives

Ted M. Young
November 17, 2020
150

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

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
  2. Ted M. Young https://maketestable.com/ @jitterted Primitives: What Are They? •

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

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

    responsibilities • Not self-documenting • Leads to dispersed logic
  5. 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
  6. 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
  7. Ted M. Young https://maketestable.com/ @jitterted Other Value Objects Money •

    Contains Currency and Amount Birthdate Color Identifiers • UserId, CustomerId
  8. 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
  9. 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
  10. Ted M. Young https://maketestable.com/ @jitterted Further Constrain Data Exposure •

    Look for public methods that: • Can now be private • Return unconstrained types
  11. 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)
  12. 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