简体   繁体   English

在 C++ 中使用嵌套 switch 语句是不好的做法吗?

[英]is it bad practice to use nesting switch statments in c++?

I've been working on a project in class and the code I have written so far works well.我一直在课堂上从事一个项目,到目前为止我编写的代码运行良好。 A big part of my code uses nested switch statements.我的大部分代码使用嵌套的 switch 语句。 While I'm going to turn it in the way it is now, I would like to know for future reference if nested switch statements are generally frowned upon.虽然我将按照现在的方式改变它,但我想知道嵌套的 switch 语句是否通常不受欢迎以供将来参考。

As you've probably noticed, because you asked, they're not exactly the easiest things to follow, so I would generally try to avoid them where possible.正如您可能已经注意到的那样,因为您问过,它们并不是最容易遵循的东西,所以我通常会尽量避免它们。

This doesn't mean you can't do that kind of control flow - the trick being to split the cases out into their own functions.这并不意味着您不能执行那种控制流 - 技巧是将案例拆分为它们自己的功能。 This lets someone read the top level switch, comprehend its branches and what happens (because you'll of course have given your functions good, descriptive names), and then they can examine each function in turn to understand the next level if they need to.这让人们可以阅读顶层 switch,理解它的分支和发生的事情(因为你当然会给你的函数指定好的、描述性的名称),然后他们可以依次检查每个函数以了解下一级,如果他们需要的话.

To avoid the cost of a function call which would previously have been unnecessary you can mark the functions inline to make the compiler effectively copy the body of the function to the call site.为了避免以前不必要的函数调用成本,您可以将函数标记为inline以使编译器有效地将函数体复制到调用站点。

It'd end up looking something like this incredibly genericised and incomplete skeleton:它最终看起来像这个令人难以置信的通用化和不完整的骨架:

int main() {
    int thing;
    char second_level_thing;

    // various code which produces thing and second_level_thing

    switch (thing) {
    case 0: do_zero_case(second_level_thing); break;
    case 1: do_one_case(); break;
    default: do_default_case(); break;
    }

    // the rest of main
}

inline void do_zero_case(char second_level_thing) {
   switch (second_level_thing) {
     case 'a': // 0 -> a branch code
     // etc...
   }
}

// etc...

Do not call your functions things like do_zero_case !不要叫你的函数之类的东西do_zero_case Name them after what it actually does .以其实际作用命名它们。

I would add that if multiple levels are inspecting the same value there's something very odd going on.我要补充的是,如果多个级别正在检查相同的值,那么会发生一些非常奇怪的事情。

There are also alternative control flows available through use of OOP techniques (virtual method calls in a variety of forms), templates, higher-order functions and so forth.通过使用 OOP 技术(各种形式的虚拟方法调用)、模板、高阶函数等,还可以使用替代控制流。 Sometimes a nice simple switch statement or two is exactly what you need though!有时,一两个简单的switch语句正是您所需要的!

Generally speaking, if and switch statements are more "costy" in terms of calculations for the proccessor, because you force the assembler to make new guesses (for what the next instructions will be) every time you jump from case to case.一般来说,就处理器的计算而言,if 和 switch 语句更“昂贵”,因为每次从一个案例跳转到另一个案例时,您都会强制汇编器进行新的猜测(对于下一条指令将是什么)。 Try to use as less possible if your prime concern is algorithm efficiency.如果您最关心的是算法效率,请尽量少使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM