Code Smell 221 - Missing Break in Switch
Tags: webdev, beginners, programming, tutorial
Published: 2023-08-07 00:03:09

*You abuse cases in switches and make subtle mistakes*

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

# Problems

- Hidden [defects](https://dev.to/mcsee/stop-calling-them-bugs-57gl)

- Readability

# Solutions

1. Add the missing *break*

2. Convert the switch into a polymorphic hierarchy

3. [Remove](https://dev.to/mcsee/code-smell-110-switches-with-defaults-1ibb) 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

{% post https://dev.to/mcsee/code-smell-110-switches-with-defaults-1ibb %}

{% post https://dev.to/mcsee/code-smell-36-switch-case-elseif-else-if-statements-h6c %}

{% post https://dev.to/mcsee/code-smell-100-goto-b3a %} 

# More Info

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

[Common Weakness Enumeration](https://cwe.mitre.org/data/definitions/484)

{% post https://dev.to/mcsee/how-to-get-rid-of-annoying-ifs-forever-1jfg %}

[Apple's Security Defect](https://embeddedgurus.com/barr-code/2014/03/apples-gotofail-ssl-security-bug-was-easily-preventable/)

# Disclaimer

Code Smells are my [opinion](https://dev.to/mcsee/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned-1n3a).

# 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_
 
{% post https://dev.to/mcsee/software-engineering-great-quotes-26ci %}

* * *

This article is part of the CodeSmell Series.

{% post https://dev.to/mcsee/how-to-find-the-stinky-parts-of-your-code-1dbc %}