Search code examples
programming-languagesreadability

Using break statement even though previous line results in exit


I was reading through some code (C, if it makes a difference to anyone) today and got curious about a switch-block.

switch (c_type) {
case -1:
    some_function(some_var);
    break;
[...]
default:
    abort();
}

Now, this is a perfectly simple switch-block. It's the some_function(some_var)-call I'm curious about: If you, the programmer, is absolutely, positively, super duper sure that the call will result in the process exiting, do you still put the break-statement underneath even though it is completely unnecessary? Would you say that it is best-practice?


Solution

  • I would say best practice would be to have a bomb-out assert() below the function call. This serves a dual purpose: it documents the fact that this point in the runtime should be unreachable, and it produces an error message if the code somehow does reach that spot.