An enum and a switch make command handling explicit.

enum Use an enum when a value must be one of a small named set.
dispatch A switch turns the command into the project action that should happen next.

Command Dispatch

code
command_dispatch.cpp
Replay: real traced execution (multi-file project)
#include <iostream>

enum class Command {
    Start,
    Stop,
    Pause
};

Command decode(int code) {
    if (code == 1) {
        return Command::Start;
    }
    if (code == 2) {
        return Command::Stop;
    }
    return Command::Pause;
}

int main() {
    int code = 2;

    Command command = decode(code);
    int action = 0;
    switch (command) {
        case Command::Start:
            action = 10;
            break;
        case Command::Stop:
            action = 20;
            break;
        case Command::Pause:
            action = 30;
            break;
    }

    std::cout << "code=" << code << std::endl;
    std::cout << "action=" << action << std::endl;
    return 0;
}
#include <iostream>

enum class Command {
    Start,
    Stop,
    Pause
};

Command decode(int code) {
    if (code == 1) {
        return Command::Start;
    }
    if (code == 2) {
        return Command::Stop;
    }
    return Command::Pause;
}

int main() {
    int code = 1;

    Command command = decode(code);
    int action = 0;
    switch (command) {
        case Command::Start:
            action = 10;
            break;
        case Command::Stop:
            action = 20;
            break;
        case Command::Pause:
            action = 30;
            break;
    }

    std::cout << "code=" << code << std::endl;
    std::cout << "action=" << action << std::endl;
    return 0;
}
#include <iostream>

enum class Command {
    Start,
    Stop,
    Pause
};

Command decode(int code) {
    if (code == 1) {
        return Command::Start;
    }
    if (code == 2) {
        return Command::Stop;
    }
    return Command::Pause;
}

int main() {
    int code = 3;

    Command command = decode(code);
    int action = 0;
    switch (command) {
        case Command::Start:
            action = 10;
            break;
        case Command::Stop:
            action = 20;
            break;
        case Command::Pause:
            action = 30;
            break;
    }

    std::cout << "code=" << code << std::endl;
    std::cout << "action=" << action << std::endl;
    return 0;
}
  1. code ← 2

    19int main() {20    int code→ 2 = 2; //@code=1, 32122    Command command = decode(code2);23    int action = 0;
  2. Command decode(int code)

    9Command decode(int code2) {10    if (code == 1) {
  3. if (code == 2)

    12}13if (code2 == 2) {14    return Command::Stop;15}
  4. command ← 1, action ← 0

    22Command command→ 1 = decode(code2);23int action→ 0 = 0;24switch (command) {
  5. switch (command)

    23int action = 0;24switch (command1) {25    case Command::Start:
  6. action ← 20

    27    break;28case Command::Stop:29    action→ 20 = 20;30    break;
  7. action = 20;

    28case Command::Stop:29    action = 20;30    break;31case Command::Pause:
  8. std::cout << "code=" << code << std::endl;

    36    std::cout << "code=" << code2 << std::endl;37    std::cout << "action=" << action20 << std::endl;38    return 0;39}
    outputcode=2
    action=20
  1. code ← 1

    19int main() {20    int code→ 1 = 1;2122    Command command = decode(code1);23    int action = 0;
  2. Command decode(int code)

    9Command decode(int code1) {10    if (code == 1) {
  3. if (code == 1)

    9Command decode(int code) {10    if (code1 == 1) {11        return Command::Start;12    }
  4. command ← 0, action ← 0

    22Command command→ 0 = decode(code1);23int action→ 0 = 0;24switch (command) {
  5. switch (command)

    23int action = 0;24switch (command0) {25    case Command::Start:
  6. action ← 10

    24switch (command) {25    case Command::Start:26        action→ 10 = 10;27        break;
  7. action = 10;

    25case Command::Start:26    action = 10;27    break;28case Command::Stop:
  8. std::cout << "code=" << code << std::endl;

    36    std::cout << "code=" << code1 << std::endl;37    std::cout << "action=" << action10 << std::endl;38    return 0;39}
    outputcode=1
    action=10
  1. code ← 3

    19int main() {20    int code→ 3 = 3;2122    Command command = decode(code3);23    int action = 0;
  2. Command decode(int code)

    9Command decode(int code3) {10    if (code == 1) {11        return Command::Start;12    }13    if (code == 2) {14        return Command::Stop;15    }16    return Command::Pause;17}
  3. command ← 2, action ← 0

    22Command command→ 2 = decode(code3);23int action→ 0 = 0;24switch (command) {
  4. switch (command)

    23int action = 0;24switch (command2) {25    case Command::Start:
  5. action ← 30

    30    break;31case Command::Pause:32    action→ 30 = 30;33    break;
  6. action = 30;

    31    case Command::Pause:32        action = 30;33        break;34}
  7. std::cout << "code=" << code << std::endl;

    36    std::cout << "code=" << code3 << std::endl;37    std::cout << "action=" << action30 << std::endl;38    return 0;39}
    outputcode=3
    action=30