No Ifs JS - kimschles/schlesinger-knowledge GitHub Wiki
Programming Without Ifs
Kyle Coberly -- Node.js Meetup, January 17, 2019
Learning Objectives
- Define cyclomatic complexity
- Recall 3 ways to avoid
ifs
- 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
ifs
Recall 3 ways to avoid - Functional chains
- best used for data transformations
- Dictionary Lookup
- a good replacement for switch statements
- Dynamic Dispatch
- variable behavior
- to replace a switch statement that accepts different data types (?)
if
statement better
3 ways to make an - Combine conditionals AKA flatten nested ifs
- Wrap blocks in functions
- Return a ternary