Do I need a default request in this scenario?

Often at school, our lecturers will tell us to always include instructions Defaultat the end of the statement about the case of switching. However, I was always wondering if this is necessary for ALL (or most) scenarios?

Consider the following example in C ++:

int num = rand()%3;
switch (num)
{
   case 0: methodOne();
           break;
   case 1: methodTwo();
           break;
   case 2: methodThree();
           break;
}

In the case above, I feel it is impossible to have a case where it can be> 2 or <0, so do I still need to include the operator Default?

There are similar issues in SO that require Defaulta switch. The answers provided said that we should include at almost any time Default. But of all the cases that I personally encountered, it just seems redundant, as Defaultit can never be achieved.

. , , Default? Default. error-handling, , ?

+4
6

, , , switch.

, / . :

// V1.0.0: Initial version.
int num = rand()%3;
switch (num)
{
   case 0: methodOne();
           break;
   case 1: methodTwo();
           break;
   case 2: methodThree();
           break;
}

...

// V1.0.0: Initial version.
// V1.0.1: Added a fourth method.
int num = rand()%4;
switch (num)
{
   case 0: methodOne();
           break;
   case 1: methodTwo();
           break;
   case 2: methodThree();
           break;
}

# 2 rand, num == 4. default , , . :

// V1.0.0: Initial version.
// V1.0.1: Added a fourth method.
int num = rand()%4;
switch (num)
{
   case 0: methodOne();
           break;
   case 1: methodTwo();
           break;
   case 2: methodThree();
           break;
   default:
           assert(false);
           throw InvalidNumException("BUG: Method has not been specified for value of num");
}

assert, ( ) case , , - .

EDIT:

, - . , , case ( ).

2:

, , case, , default switch. . Phresnel.

+6

, , . - print "This should NOT happen" ( ), , -, . , !

+3

default?

, num - , , .

, . , , .

. , ?

. logic_error , , , , .

+2
, . , .

, , / . switch , , . , NotImplementedException , .

+2

, . .

, -

int num = rand()%3;

int num = rand()%4;

switch . :

default:
    throw std::logic_error("Oh noes.");

std::logic_error . , switch, (, ) .

default

default -clause. switch enum...

enum class Color {
    Red, Green, Blue
};

....

Color c = ....;
switch(c) {
case Color::Red: break; 
case Color::Green: break;
};

... , . , :

Color c = ....;
switch(c) {
case Color::Red: break;
case Color::Green: break;
default: break;
};

Color c = ....;
switch(c) {
case Color::Red:  break;
case Color::Green:  break;
case Color::Blue:  break;
};

, . . - , , , , .

( ) , , :

Color c = ....;
switch(c) {
case Color::Red: return;
case Color::Green: return;
};
throw std::logic_error(...);


MYSQL mysql = {0};
switch(c) {
case Color::Red: mysql = red_database(); break;
case Color::Green: mysql = green_database(); break;
};
if (!mysql)
    throw std::logic_error(...);
+2
source

So, change your code a bit, something more realistic:

void processNumber(int maxNum) {
   int num = rand()%maxNum;
   switch (num)
   {
      case 0: methodOne();
           break;
      case 1: methodTwo();
           break;
      case 2: methodThree();
           break;
   }
}

and here you need to make sure that it is in the set of valid values [1, 2, 3]. You can check this in several ways, but you need precautions and carefully check the input and cause errors, even if it is an internal function.

0
source

Source: https://habr.com/ru/post/1535347/


All Articles