Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4044498
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:15:48+00:00 2026-05-20T13:15:48+00:00

In one of my first code reviews (a while back), I was told that

  • 0

In one of my first code reviews (a while back), I was told that it’s good practice to include a default clause in all switch statements. I recently remembered this advice but can’t remember what the justification was. It sounds fairly odd to me now.

  1. Is there a sensible reason for always including a default statement?

  2. Is this language dependent? I don’t remember what language I was using at the time – maybe this applies to some languages and not to others?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T13:15:48+00:00Added an answer on May 20, 2026 at 1:15 pm

    Switch cases should almost always have a default case.

    Reasons to use a default

    1.To ‘catch’ an unexpected value

    switch(type)
    {
        case 1:
            //something
        case 2:
            //something else
        default:
            // unknown type! based on the language,
            // there should probably be some error-handling
            // here, maybe an exception
    }
    

    2. To handle ‘default’ actions, where the cases are for special behavior.

    You see this a LOT in menu-driven programs and bash shell scripts. You might also see this when a variable is declared outside the switch-case but not initialized, and each case initializes it to something different. Here the default needs to initialize it too so that down the line code that accesses the variable doesn’t raise an error.

    3. To show someone reading your code that you’ve covered that case.

    variable = (variable == "value") ? 1 : 2;
    switch(variable)
    {
        case 1:
            // something
        case 2:
            // something else
        default:
            // will NOT execute because of the line preceding the switch.
    }
    

    This was an over-simplified example, but the point is that someone reading the code shouldn’t wonder why variable cannot be something other than 1 or 2.


    The only case I can think of to NOT use default is when the switch is checking something where its rather obvious every other alternative can be happily ignored

    switch(keystroke)
    {
        case 'w':
            // move up
        case 'a':
            // move left
        case 's':
            // move down
        case 'd':
            // move right
        // no default really required here
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The first one is definitely something that works, but which one below is the
First, what does one call the ghost caption that appears in a text edit
This came up as one of the code review comments. Is it a good
Following are two pieces of jquery code. First one prints a text message when
i inherited this code from some-one and this only returns the first row it
I'm using C++ .NET 2.0 I have 2 forms the first one is declared
I'm trying to add a space before every capital letter, except the first one.
Everything is an object was one of the first things I learned about Ruby,
I've installed PowerShell recently and one of the first things I started looking for
When you take your first look at an Oracle database, one of the first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.