---
title: "Switch Statements"
type: "code-smell"
slug: "switch-statements"
url: "http://localhost:3000/en/smells/switch-statements.md"
category: "Object-Orientation Abusers"
description: "You have a complex switch operator or sequence of if statements."
---
# Switch Statements

> You have a complex switch operator or sequence of if statements.

## Signs and Symptoms

You have a complex `switch` operator or sequence of `if` statements.

## Reasons for the Problem

Relatively rare use of `switch` and `case` operators is one of the hallmarks of object-oriented code. Often code for a single `switch` can be scattered in different places in the program. When a new condition is added, you have to find all the `switch` code and modify it.

As a rule of thumb, when you see `switch` you should think of polymorphism.

## Payoff

* Improved code organization.
## Relations

**Treated by**

- [Extract Method](/en/extract-method.md)
- [Move Method](/en/move-method.md)
- [Replace Type Code with Subclasses](/en/replace-type-code-with-subclasses.md)
- [Replace Type Code with State/Strategy](/en/replace-type-code-with-state-strategy.md)
- [Replace Conditional with Polymorphism](/en/replace-conditional-with-polymorphism.md)
- [Replace Parameter with Explicit Methods](/en/replace-parameter-with-explicit-methods.md)
- [Introduce Null Object](/en/introduce-null-object.md)


## Detected by

- **sonar** `S1479` — "switch" statements should not have too many "case" clauses (https://rules.sonarsource.com/java/RSPEC-1479/)
- **eslint** `sonarjs/max-switch-cases` — "switch" statements should not have too many "case" clauses (https://github.com/SonarSource/eslint-plugin-sonarjs/blob/master/docs/rules/max-switch-cases.md)
