0
switch(number){
case 2:
    a+=b;
    break;
case 3:
    a+=b;
    break;
case 4:
    a+=b;
    d=f;
    break;
case 5:
    d=e;
    break;
}

how to minimize first three switch cases which which does similar work?

  • You can move the `a+=b;` statement to an if-statement outside, but I doubt it'll make much difference, why not just leave it as it is? – Lasse V. Karlsen Feb 02 '19 at 11:11
  • Ah, [the perfect use case for a `goto`](https://stackoverflow.com/questions/4756084/use-a-goto-in-a-switch). – Cody Gray - on strike Feb 02 '19 at 11:12
  • because "a+=b;" is 60 lines of code – Niranjan Rai Feb 02 '19 at 11:14
  • 1
    a+=b is 60 lines of code? don't you just oversimplify your problem. answers that you get are probably just for a+=b not 60 lines of code :) – M.kazem Akhgary Feb 02 '19 at 11:17
  • 1
    @NiranjanRai if a+=b is 60 lines of code.. does this lines require to be performed first and then d=f - case 4? if yes, one of provided answer will be horribly wrong. – Amit Feb 02 '19 at 11:19
  • as question suggests I am only looking solution to minimize switch statement and those code wont affect the case – Niranjan Rai Feb 02 '19 at 11:48
  • @CodyGray `goto` command not criminal anymore? – Zam Feb 02 '19 at 17:18
  • @Zam It never really was. Only certain usages that turned your code into spaghetti. An implicit fall-through is really no different than an explicit `goto` in this case. If anything, one could make the argument it is better and clearer to be explicit. Exceptions are `goto`s by a different name, as are function calls. The difference is, their syntax forces you to use them in structured ways. With `goto`, you have to be disciplined yourself to ensure that you are using it in structured ways. – Cody Gray - on strike Feb 03 '19 at 02:12

3 Answers3

4

If you using C# 7, you can make use of Pattern Matching, even though this is an overkill as rightly pointed by Jon Skeet. But in case, you want to stick to switch case, and want to reduce 'case', you could do the following

 switch(number)
   {
    case var _ when number == 2 || number==3 || number==4:
        a+=b;
        if(number ==4)
        d=f
     break;
    case 5:
        d=e;
        break;
    }

You can also replace the first case with variants like

case var _ when new[]{2,3,4}.Contains(number):

Or

case var _ when number >= 2 || number <= 3: // As pointed by earlier answer

Without pattern matching, you could do the following as well

switch(number)
{
case 2:
case 3:
case 4:
    a+=b;
    if(number ==4)
        d=f;
    break;
case 5:
    d = e;
    break;
}

Btw, if your problem is "a+b" is about 60 lines of code, you always have the option to make it a function (and move it out of switch case) to increase its readability.

switch(number)
{
case 2:
case 3:
case 4:
    MethodAbAction();
    if(number ==4)
        MethodDFAction();
    break;
case 5:
    MethodDEAction();
    break;
}

btw, a 60 line method is never fun to read. It would be better if you can split up.

Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
3
if (2 <= number && number <= 4) {
  a += b;
}
if (number == 4) {
  d = f;
} else if (number == 5) {
  d = e;
}
  • first statement could be simplified `if (number != 5) { a += b; }` – Zam Feb 02 '19 at 11:39
  • 1
    @Zam, no. You are assuming that `number` will only contain 2,3,4 or 5. But nowhere in the question is stated what the actual value range of `number` will be. `number` could very well be 0, 1, or any other number. The code in the question is executing `a+=b` only if `number` is either 2,3 or 4. This is not the same as `number != 5`... –  Feb 02 '19 at 11:55
-1
if (number != 5)
{
    a += b;
}

if (number == 4)
{
    d = f;
}
else
if (number == 5)
{
    d = e;
}
Ahmad Khan
  • 2,655
  • 19
  • 25
Zam
  • 2,880
  • 1
  • 18
  • 33
  • That does not necessarily correlate with the code in the question. You make an assumption that the only numbers in `number` will be 2,3,4 or 5. But there is nothing in the question supporting your assumption. The code in the question includes situations where `number` could be 0, 1 or any other number, in which case no operations should take place. However, your answer here diverges from this, assuming that `a+=b` should always take place if `number` is any number different from `5` (note the code in the question is saying: do `a+=b` only if `number` is either 2, 3 or 4). –  Feb 02 '19 at 11:51
  • @elgonzo my answer is based and optimized for OP question and his code sample. – Zam Feb 02 '19 at 11:57
  • No, your answer is not based on the question; your answer is based on your assumption you made about the question (as i explained already in my first comment) –  Feb 02 '19 at 11:58