C & C++

C & C++

Made by DeepSource

Switch statement with a single-clause CXX-W1197

Anti-pattern
Minor

Switch statements should have more than one clause for them to be any more useful over an if-else construct.

Consider rewriting the switch as if-else, thus making the code more readable.

Bad practice

int foo(int bar) {
    switch bar {
        case 0: {
            // ..
        } break;
        default: {
            // ..
        }
    }
}

Recommended

int foo(int bar) {
    if bar == 0 {
        // ..
    } else {
        // ..
    }
}