如何处理“开关/外壳”当软件版本添加新参数时

发布于 2024-11-08 13:47:10 字数 534 浏览 0 评论 0原文

当有新的软件版本发布并且它向 switch case 添加另一个索引时如何处理。在这种情况下,索引代表一个参数。例如,

Rel1: i = 1-5, 7(不包括 6)

Rel2: i = 1-7

for (int i = 1; i<=7;i++)
{
  switch (i)
  {
    case 1: /*process data*/ break;
    case 2: /*process data*/ break;
    case 3: /*process data*/ break;
    case 4: /*process data*/ break;
    case 5: /*process data*/ break;
    // case 6: // REL 2
    case 7: /*process data*/ break;
    default: break;
  }
}

我实际上可以通过在这些情况之间添加 if 语句来检查吗?还有更好的主意吗?

How to handle when there is a new SW Release sometime and it adds another index to switch case. Index represents a parameter in this case. For example,

Rel1: i = 1-5, 7 (excluding 6)

Rel2: i = 1-7

for (int i = 1; i<=7;i++)
{
  switch (i)
  {
    case 1: /*process data*/ break;
    case 2: /*process data*/ break;
    case 3: /*process data*/ break;
    case 4: /*process data*/ break;
    case 5: /*process data*/ break;
    // case 6: // REL 2
    case 7: /*process data*/ break;
    default: break;
  }
}

Can I actually check by adding if statement between those cases? Any better idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

剑心龙吟 2024-11-15 13:47:11

在生产代码中,如果我有这样的意外结果,我经常尝试捕获并记录它。
抛出异常可能是可以的,具体取决于它的处理方式。为了面向未来,设计这样的东西来优雅地处理新值通常是好的。

In production code if I have an unexpected result like this I often try to capture and log it.
Throwing an exception may be ok depending on how it's handled. For future-proofing it's usually good to design stuff like this to gracefully handle new values.

远山浅 2024-11-15 13:47:11

不要使用“for”循环,而是使用带有版本号生成器函数的“while”循环。

伪代码:

typedef enum {rel1, rel2} rel_t;

// Where 'ReleaseSequence' is a generator functor. Class that takes rel_t
// in constructor and creates an appropriate functor that returns the
// required sequence of numbers for a particular release

ReleaseSequence seq(rel1);
while (i = seq()) {
    switch (i) {
        case 1:...
        case 2:...
        case 3:...
        case 4:...
        case 5:...
        case 6:...
        case 7:...
        default:
               break;
    }
}

Instead of using a 'for' loop, use a 'while' loop with a release number generator function.

Pseudo code:

typedef enum {rel1, rel2} rel_t;

// Where 'ReleaseSequence' is a generator functor. Class that takes rel_t
// in constructor and creates an appropriate functor that returns the
// required sequence of numbers for a particular release

ReleaseSequence seq(rel1);
while (i = seq()) {
    switch (i) {
        case 1:...
        case 2:...
        case 3:...
        case 4:...
        case 5:...
        case 6:...
        case 7:...
        default:
               break;
    }
}
蔚蓝源自深海 2024-11-15 13:47:11

我发现看到嵌套在循环中的 switch 很奇怪......

看起来你正在执行一个管道。那你为什么不简单地将 is 定义为一。

例如,一个简单的函数指针数组就可以了。您可以为每个版本定义一个这样的管道,并为要忽略的参数提供“noop”函数。

I find it very strange to see a switch nested within a loop...

It looks like you are executing a pipe. Then why don't you simply define is as one.

For example, a simple array of pointer to functions would do nicely. You can define one such pipe per release, and provide "noop" functions for the parameters to ignore.

缱绻入梦 2024-11-15 13:47:11

如果您想捕获意外的值,难道不应该将该检查放在 default 部分吗?

...
default:
    assert(false);   // We should never get here!

If you want to catch unexpected values, shouldn't you put that check in the default part?

...
default:
    assert(false);   // We should never get here!
梦里的微风 2024-11-15 13:47:10

#define 这是哪个版本,然后使用#ifdef

  switch (i)
  {
    case 1: /*process data*/ break;
    case 2: /*process data*/ break;
    case 3: /*process data*/ break;
    case 4: /*process data*/ break;
    case 5: /*process data*/ break;
#ifdef REL_2
    case 6: /*process data*/ break;  // <-- executed only for REL_2
#endif
    case 7: /*process data*/ break;
    default: break;
  }

#define which release is this and then use #ifdef.

  switch (i)
  {
    case 1: /*process data*/ break;
    case 2: /*process data*/ break;
    case 3: /*process data*/ break;
    case 4: /*process data*/ break;
    case 5: /*process data*/ break;
#ifdef REL_2
    case 6: /*process data*/ break;  // <-- executed only for REL_2
#endif
    case 7: /*process data*/ break;
    default: break;
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文