# Code Smell 221 - Missing Break in Switch

> TL;DR: Cases are GOTOs, but you might be missing them

# Problems

- Hidden [defects](https://maximilianocontieri.com/stop-calling-them-bugs)

- Readability

# Solutions

1. Add the missing break

2. Convert the switch into a polymorphic hierarchy

3. [Remove](https://maximilianocontieri.com/code-smell-110-switches-with-defaults) the default switch

# Context

In a switch statement, when a match is found in a particular case, the code execution will start from that case and continue executing all subsequent cases until a break statement is encountered or the switch block ends. 

This behavior is called "fall-through."

Forgetting a *break* clause will continue with the following case.

This is similar to a very [serious vulnerability](https://embeddedgurus.com/barr-code/2014/03/apples-gotofail-ssl-security-bug-was-easily-preventable/) that implied an urgent release.

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/d07af4a8fdbbc9be0392eefaa6934122)
```c
  switch (number) {
      case 1:
          printf("Number is 1.\n");
          break;
      case 2:
          printf("Number is 2.\n"); 
          // Missing break
      case 3:
          // Case 2 will continue here
          printf("Number is 3.\n"); 
          break;
      default:
          printf("Number is not 1, 2, or 3.\n");
  }

// If the number is 2 this will output numbers 2 and 3
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/006410b727680f0215d8fd2d84eb9f92)
```c
  switch (number) {
      case 1:
          printf("Number is 1.\n");
          break;
      case 2:
          printf("Number is 2.\n"); 
          break; // Added 'break' to prevent fall-through
      case 3:
          printf("Number is 3.\n"); 
          break;
      default:
          printf("Number is not 1, 2, or 3.\n");
  }

// This is correct even though switches AND defaults
// Are other code smells
```

# Detection

[X] Automatic 

Many [linters](https://rules.sonarsource.com/c/type/Code%20Smell/RSPEC-128/) and also ChatGPT detect this smell.

# Tags

- IFs

# Conclusion

Using switches and causes is problematic, your need to use higher-level sentences.

# Relations

%[https://maximilianocontieri.com/code-smell-110-switches-with-defaults]

%[https://maximilianocontieri.com/code-smell-36-switchcaseelseifelseif-statements]

%[https://maximilianocontieri.com/code-smell-100-goto] 

# More Info

%[https://rules.sonarsource.com/c/type/Code%20Smell/RSPEC-128/]

%[https://cwe.mitre.org/data/definitions/484]

%[https://maximilianocontieri.com/how-to-get-rid-of-annoying-ifs-forever]

%[https://embeddedgurus.com/barr-code/2014/03/apples-gotofail-ssl-security-bug-was-easily-preventable/]

# Disclaimer

Code Smells are my [opinion](https://maximilianocontieri.com/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned).

# Credits

Photo by [Nikola Johnny Mirkovic](https://unsplash.com/@thejohnnyme) on [Unsplash](https://unsplash.com/photos/Jp3v9MvH2oA)
    
* * *

> I am not terribly dogmatical about the goto statement. I have the uncomfortable feeling that others are making a religion out of it, as if the conceptual problems of programming could be solved by a single trick, by a simple form of coding discipline!

_Edsger Dijkstra_
 
%[https://maximilianocontieri.com/software-engineering-great-quotes]

* * *

This article is part of the CodeSmell Series.

%[https://maximilianocontieri.com/how-to-find-the-stinky-parts-of-your-code]
