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.

Model cost, team of 8$4K-$12K/yr
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 one of the switches
  • ●Consistency maintenance: keeping parallel switches in sync requires manual discipline
  • ●Change amplification: each new type requires modifying several files instead of one

Estimated Annual Cost

Cost of one instance a year by team size, from our model at $180K fully-loaded cost per engineer. Low and high use the bottom and top of the Medium inputs; mid is halfway. See full methodology.

Team SizeLowMidHigh
3 devs$2K$4K$5K
5 devs$3K$5K$8K
8 devs$4K$8K$12K
10 devs$5K$10K$15K
20 devs$9K$19K$28K

These are model outputs, not measurements. Every input is on the cost model page, and the same model produces the figures on the 22 smells page. Put in your own team and cost in the smell profile calculator.

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 (our estimate): 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 (our estimate): 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 (our estimate): 1-2 hours
Impact: Removes null-checking branches throughout the codebase

Related Smells

Updated November 2026