Programming Search⌘K

Organizing data and simplifying conditional logic

The refactorings of chapters 9 and 10 that organize data and simplify conditional logic, with Replace Magic Literal and Replace Control Flag with Break from the online catalogue.

Techniques

  • Change reference to value

    When a small object that is held inside another is changed in place, make it immutable: give it value equality, and replace it with a fresh object each time its contents change.

    core
  • Change value to reference

    When many copies of one logical object exist, and an update to it must be seen by all holders, keep one shared object for it in a repository, and give each holder a reference to that object.

    core
  • Consolidate conditional expression

    When several checks in a row give the same result, combine them into one condition with or and and, and extract it into a function whose name says the single rule that the checks make together.

    core
  • Decompose conditional

    When an if statement has a long condition and long branches, extract the condition and each branch into a function with a name that says what it means, so that the if statement reads as the rule.

    core
  • Introduce assertion

    When code works only if some condition is true, state that condition with an assertion, so that readers see it and a programming error fails at once; not for checking input from outside.

    core
  • Introduce special case

    When many callers check for the same special value, such as a missing customer, and then do the same thing, make an object for the special case that gives that behavior, and remove the checks.

    core
  • Rename field

    Change the name of a field of a record or class, and of its accessors, so that the data says what it means; when the name is also stored or sent, rename in stages.

    core
  • Replace conditional with polymorphism

    When a switch on a type code is repeated in several functions, make a class for each case and move each branch into a method of its class, so that the call dispatches to the right code with no switch.

    core
  • Replace control flag with break

    When a loop sets a boolean variable to tell later code to stop, use break, continue or return at that point instead, and delete the variable.

    core
  • Replace derived variable with query

    Replace a stored value that the code keeps in step with other data by a function that computes it from that data when asked, so that the two can never disagree.

    core
  • Replace magic literal

    When a number or a string in the code has a meaning that the code does not say, give it a name: declare a constant, and use the name in place of the literal.

    core
  • Replace nested conditional with guard clauses

    When a function nests its special cases in if and else, check each special case at the top and return at once, so that the normal case is the unindented code at the end of the function.

    core
  • Split variable

    When one variable is assigned more than once to hold different things, give each thing its own variable, and make each one a constant where the language allows it.

    core