Skip to content

No Ifs JS

kimschles edited this page Jan 18, 2019 · 1 revision

Programming Without Ifs

Kyle Coberly -- Node.js Meetup, January 17, 2019

Learning Objectives

  1. Define cyclomatic complexity
  2. Recall 3 ways to avoid ifs
  3. Discuss 3 ways to make an if statement better

Cyclomatic Complexity

  • The number of independent paths that are possible in code
  • Loops do not add cyclomatic complexity
  • Conditionals add a cc because they provide at least two possible paths
  • if, else, else if
  • case
  • catch
  • &&
  • a ternary

Complex Code is Problematic because...

  • You have to write more test cases (probably reduces test coverage)
  • More possible points of failure
  • Harder to extend
  • Harder to read

Recall 3 ways to avoid ifs

  1. Functional chains
  • best used for data transformations
  1. Dictionary Lookup
  • a good replacement for switch statements
  1. Dynamic Dispatch
  • variable behavior
  • to replace a switch statement that accepts different data types (?)

3 ways to make an if statement better

  1. Combine conditionals AKA flatten nested ifs
  2. Wrap blocks in functions
  3. Return a ternary
Clone this wiki locally