Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unreachable code detected in case statement

Tags:

c#

I have a code:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        switch (keyData)
        {
            case Keys.Alt|Keys.D1:

                if (this._condition1)
                {
                    return true;
                }
                else
                {
                    return base.ProcessCmdKey(ref msg, keyData);
                }

                break;

            case Keys.Control |Keys.U:

                if (this._condition2)
                {
                    return true;
                }
                else
                {
                    return base.ProcessCmdKey(ref msg, keyData);
                }

                break;

            default:

                return base.ProcessCmdKey(ref msg, keyData);
        }

        return true;

It gives me "unreachable code detected" warning on breaks.

Is it good practice not to use break operator here ? I don't want to turn off "unreachable code detected" warning.

PS: There are many case in my ProcessCmdKey method.

like image 366
Александр Д. Avatar asked Apr 15 '10 08:04

Александр Д.


People also ask

What is unreachable code detected?

In computer programming, unreachable code is part of the source code of a program which can never be executed because there exists no control flow path to the code from the rest of the program.

Why is code unreachable after return statement?

The JavaScript warning "unreachable code after return statement" occurs when using an expression after a return statement, or when using a semicolon-less return statement but including an expression directly after.

How do I fix unreachable code in Python?

You should just remove the return True line, and instead of print (result) , return result . That will be True if the matrix is square AND symmetric, and False if it's square and NOT symmetric.


2 Answers

There are three unreachable statements in your code, first two are the break statements and the last one int he last line "return true" is also unreachable, I dont know whether C# compiler detects that or not, but logically there is no way last return statement will also be reached.

There are multiple ways to solve this issue,

  1. Store a temp variable, called bool retVal, keep retVal and break your switch case and at the end of function return retVal.
  2. If you return value before break, break statement is useless.

Better Design Way

If you return values within switch cases, it may be difficult to analyze your code later on by you or someone else, usually it is better to keep a return value temp variable and return it at end of function, that becomes easier to debug and understand the code for new coder.

Switch can be complicated, and more returns within switch may not have better control, if you want to implement logging, debugging, returns from cases could be complicated. And it becomes way to difficult browsing the logic flow graphs.

So it is better to avoid return altogather from case, but still it depends on situtations as well, one needs to make an intelligent decision here.

like image 58
Akash Kava Avatar answered Sep 22 '22 15:09

Akash Kava


break is not necessary if all paths in a case statement end with a return. Do not use it then, otherwise you will get the mentioned warning.

like image 37
Danvil Avatar answered Sep 21 '22 15:09

Danvil