Topic

If This, Then That: Tools for Refactoring Conditional Logic
By: Paul Zuradzki
Date: April 10, 2025, 6 p.m.

Outline

  • Setting up a test harness. How to use parameterized tests to refactor safely.

  • Simplifying conditionals 

    • Guard clauses and early returns

    • Method extraction vs inlining. Tradeoffs (“Clean Coder Uncle Bob” vs “Philosophy of Software Design Ousterhout).

    • Pattern matching with Python 3.10+ `match-case`

  • Boolean logic tricks (from discrete math)

    • DeMorgan’s Law

    • Double negation

    • Distributive laws

  • Understanding Python boolean semantics

    • Short-circuit evaluation

    • `and` / `or` behavior return-value behavior

    • Tripwires: when truthy expressions return unexpected values

  • Decoupling Behavior from Branching

    • Intro example

    • Strategy pattern in functional style. Dispatching callables with dicts.

    • functools.singledispatch for type-based dispatch

    • Strategy pattern in OOP style

      • With duck typing

      • With inheritance-based polymorphism

      • With protocol-based structural typing

  • If IF ain’t broke, then don’t fix it. 

    • Evaluating design tradeoffs. 

    • When the overhead of encapsulation or indirection might not be worth it.

  • Recap: when to use what

    • Summary of techniques, tradeoffs, and when each is favorable