Object-Orientation AbusersMedium

Switch Statements: What It Costs and How to Fix It

Complex switch/case or if/else chains that duplicate type-checking logic across multiple locations.

Annual Cost$1.5k - $12k
Severity
3/5
CategoryObject-Orientation Abusers
Detection4 tools

What It Is

The Switch Statements smell occurs when the same conditional logic (switch/case or long if/else chains) checking the same type or status code appears in multiple places throughout the codebase. A single switch statement is fine. The smell arises when adding a new case requires updating switches scattered across 5 different files. This is a violation of the Open/Closed Principle: the code is not open for extension without modification.

Threshold: A single switch on a type is acceptable. When the same type check appears in 3+ locations, replace with polymorphism. Switches with 10+ cases almost always indicate a missing abstraction.

Why It Costs Money

1

Adding a new case requires finding and updating every switch on that type. In large codebases, developers regularly miss 1-2 locations, causing bugs that only manifest for the new case and are difficult to reproduce.

2

Each duplicated switch is a maintenance burden. When business rules for a case change, every switch must be updated consistently. Inconsistency between switches creates subtle behaviour differences.

3

Type-checking switches resist automated detection because they look different in each location (some use switch, some use if/else, some use ternary operators).

Specific Cost Mechanisms

  • Missed-case bugs: adding a new type and missing 1-2 switches costs 4-8 hours per incident
  • Consistency maintenance: keeping 5-10 parallel switches in sync requires manual discipline
  • Change amplification: each new type requires modifying 5-10 files instead of 1

Estimated Annual Cost

Cost per instance by team size and codebase size. Based on $120,000 average developer salary. See full methodology.

Team SizeSmall (<50k LOC)Medium (50k-200k)Large (200k+)
3 devs$1,500$3,600$7,200
5 devs$2,500$6,000$12,000
10 devs$3,750$9,000$12,000
20 devs$5,000$12,000$12,000

How to Detect It

Specific rules and thresholds for automated detection. See full tool comparison.

SonarQube
squid:S1479 / squid:S131

Excessive switch cases, missing default clause

CodeClimate
cognitive-complexity

Switch statements inflate cognitive complexity score

ESLint
no-complex-switch / complexity

Cyclomatic complexity catches complex switches

PMD
TooManyFields / SwitchDensity

Density metric identifies overloaded switches

Refactoring Patterns

Proven techniques to eliminate this smell. See all refactoring patterns.

Replace Conditional with Polymorphism

The same type check appears in 3+ locations

Effort: 4-8 hours
Impact: Eliminates all duplicate switches for that type hierarchy

Replace Type Code with State/Strategy

Behaviour varies by a type code or status field

Effort: 3-6 hours
Impact: New cases require adding a class, not modifying existing code

Introduce Null Object

One case in the switch handles a null or missing value

Effort: 1-2 hours
Impact: Removes null-checking branches throughout the codebase

Related Smells