什么是如果终止的好处......否则,如果带有else子句的结构? [英] What is the benefit of terminating if … else if constructs with an else clause?

查看:256
本文介绍了什么是如果终止的好处......否则,如果带有else子句的结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我们的组织有一个要求编码规则(没有任何解释)是:

Our organization has a required coding rule (without any explanation) that:

如果...否则,如果结构应与else子句终止

if … else if constructs should be terminated with an else clause

例1:

if ( x < 0 )
{
   x = 0;
} /* else not needed */

例2:

if ( x < 0 )
{
    x = 0;
}
else if ( y < 0 )
{
    x = 3;
}
else    /* this else clause is required, even if the */
{       /* programmer expects this will never be reached */
        /* no change in value of x */
}

什么边缘情况是这样设计来处理?

What edge case is this designed to handle?

什么也涉及我说说原因是示例1 不需要一个其他,但示例2 做。如果原因是可重用性和可扩展性,我觉得其他应该在这两种情况下使用。

What also concerns me about the reason is that Example 1 does not need an else but Example 2 does. If the reason is re-usability and extensibility, I think else should be used in both cases.

推荐答案

作为另一个答复中提到,这是从MISRA-C编码指导原则。其目的是防御式编程,这往往是在关键任务编程中使用的一个概念。

As mentioned in another answer, this is from the MISRA-C coding guidelines. The purpose is defensive programming, a concept which is often used in mission-critical programming.

也就是说,每个如果 - 否则如果必须有一个其他结束,每个开关必须以结束默认

That is, every if - else if must end with an else, and every switch must end with a default.

有两个原因:


  • 自文档code。如果你写了其他,但离开它清空它的意思是:我绝对认为该方案既如果时,也不如果是真实的。

  • Self-documenting code. If you write an else but leave it empty it means: "I have definitely considered the scenario when neither if nor else if are true".

不写了其他有表示:无论是我考虑的方案,其中既没有如果也不否则,如果都是真的,不然我完全忘了考虑,并可能在这里有一个错误的脂肪在我的code。

Not writing an else there means: "either I considered the scenario where neither if nor else if are true, or I completely forgot to consider it and there's potentially a fat bug right here in my code".

停止失控code。在关键任务软件,你需要编写一个解释,即使是极不可能的健壮的程序。所以,你可以看到code像

Stop runaway code. In mission-critical software, you need to write robust programs that account even for the highly unlikely. So you could see code like

if (mybool == TRUE) 
{
} 
else if (mybool == FALSE) 
{
}
else
{
  // handle error
}

这code将是完全陌生的电脑程序员和计算机科学家,但它使关键任务软件完美的意义,因为它抓住这里的mybool已经损坏,无论出于何种原因的情况。

This code will be completely alien to PC programmers and computer scientists, but it makes perfect sense in mission-critical software, because it catches the case where the "mybool" has gone corrupt, for whatever reason.

从历史上看,你会害怕,因为EMI /噪声的RAM内存的损坏。这在今天不是大问题。更有可能的是,内存损坏的发生是因为错误在code别处:指向错误的位置,排列出界外的错误,堆栈溢出,失控code等

Historically, you would fear corruption of the RAM memory because of EMI/noise. This is not much of an issue today. Far more likely, memory corruption occurs because of bugs elsewhere in the code: pointers to wrong locations, array-out-of-bounds bugs, stack overflow, runaway code etc.

所以大部分的时间,code这样回来拍自己的脸当你在实施阶段写入错误。这意味着它也可以用来作为调试技巧:你正在写的程序会告诉你,当你写的错误。

So most of the time, code like this comes back to slap yourself in the face when you have written bugs during the implementation stage. Meaning it could also be used as a debug technique: the program you are writing tells you when you have written bugs.

修改

至于为什么其他不需要经过每一个如果

Regarding why else is not needed after every single if:

这是的if-else 的if-else if-else的完全覆盖所有可能的值,一个变量可以有。但是,一个普通的如果语句不一定有涵盖所有可能的值,它具有更广泛的使用。大多数情况下,你只是想检查某个条件,如果不符合,然后什么也不做。然后,它根本就没有意义编写防御性编程覆盖其他情况。

An if-else or if-else if-else completely covers all possible values that a variable can have. But a plain if statement is not necessarily there to cover all possible values, it has a much broader usage. Most often you just wish to check a certain condition and if it is not met, then do nothing. Then it is simply not meaningful to write defensive programming to cover the else case.

另外,如果你写它会彻底扰乱了code空其他之后每如果

Plus it would clutter up the code completely if you wrote an empty else after each and every if.

MISRA-C:2012 15.7没有给出理由,为什么其他是不需要的,它只是规定:

MISRA-C:2012 15.7 gives no rationale why else is not needed, it just states:

请注意:不需要一个简单的最后其他语句如果
  声明。

Note: a final else statement is not required for a simple if statement.

这篇关于什么是如果终止的好处......否则,如果带有else子句的结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆