为什么C / C ++编码标准说我们不应该在函数中有多个返回? [重复]

时间:2022-09-11 18:47:49

This question already has an answer here:

这个问题在这里已有答案:

In my organisation, we follow MISRA coding guidelines. This coding guideline says that we should not have the multiple returns in a function.Do you have any idea on it? Why is it so?

在我的组织中,我们遵循MISRA编码指南。这个编码指南说我们不应该在一个函数中有多个返回。你有什么想法吗?为什么会这样?

3 个解决方案

#1


2  

I disagree that functions should never have more than one return. There are times when an early return can make the code easier to read and maintain.

我不同意函数应该永远不会有多个返回。有时候早期返回可以使代码更易于阅读和维护。

function DoStuff
{
  if ( simpleCondition ) return result;

  DoComplcicatedProcess
  return complicatedOperatotionResult;
}

Yes it's possible to wrap this a conditional but in this case it's easy to read and potentially more efficient.

是的,它可以包装一个条件,但在这种情况下,它易于阅读,可能更有效。

#2


2  

First a single return is somewhat easy to maintain and understand. Second having multiple returns could most likely mean we are returning a value directly rather than quantity. It makes a function much easier to understand. Let me give you a simple example.

首先,单个退货有点容易维护和理解。第二个有多个回报可能意味着我们直接返回一个值而不是数量。它使函数更容易理解。让我举个简单的例子。

Multiple return example

多个返回示例

CalculateInterest(int cost)
{
    if (cost < 100)
       return cost * .2;
    else
       return cost * .3;
}

Same function with single return

单次返回功能相同

CalculateInterest(int cost) 
{
    float interest;

    if (cost < 100)
        interest = cost * .2;
    else
        interest = cost * .3;

    return interest;
}

#3


1  

A function with multiple return statements is confusing and is not easy to understand if the method is too big. You may accidentally skip few lines that should be executed.

具有多个return语句的函数令人困惑,如果方法太大,则不容易理解。您可能会意外跳过应该执行的几行。

If you are writing a small program that doesn't need any future maintenance it may be okay to ignore that rule. However for longer programs that may be maintained in future, when people do bug fix after some time, it is quite possible to overlook some block of code returning back to the calling function and put some code below that block assuming that it will run. One such example may be clean up code.

如果您正在编写一个不需要任何日后维护的小程序,则可以忽略该规则。但是对于将来可能维护的较长程序,当人们在一段时间后进行错误修复时,很可能会忽略一些代码块返回到调用函数并将一些代码放在该块下面,假设它将运行。一个这样的示例可以是清理代码。

It can be easily argued that programmer should be more careful, but following one return policy will make it easier to avoid such mistakes.

可以很容易地认为程序员应该更加小心,但遵循一个退货政策将更容易避免这种错误。

#1


2  

I disagree that functions should never have more than one return. There are times when an early return can make the code easier to read and maintain.

我不同意函数应该永远不会有多个返回。有时候早期返回可以使代码更易于阅读和维护。

function DoStuff
{
  if ( simpleCondition ) return result;

  DoComplcicatedProcess
  return complicatedOperatotionResult;
}

Yes it's possible to wrap this a conditional but in this case it's easy to read and potentially more efficient.

是的,它可以包装一个条件,但在这种情况下,它易于阅读,可能更有效。

#2


2  

First a single return is somewhat easy to maintain and understand. Second having multiple returns could most likely mean we are returning a value directly rather than quantity. It makes a function much easier to understand. Let me give you a simple example.

首先,单个退货有点容易维护和理解。第二个有多个回报可能意味着我们直接返回一个值而不是数量。它使函数更容易理解。让我举个简单的例子。

Multiple return example

多个返回示例

CalculateInterest(int cost)
{
    if (cost < 100)
       return cost * .2;
    else
       return cost * .3;
}

Same function with single return

单次返回功能相同

CalculateInterest(int cost) 
{
    float interest;

    if (cost < 100)
        interest = cost * .2;
    else
        interest = cost * .3;

    return interest;
}

#3


1  

A function with multiple return statements is confusing and is not easy to understand if the method is too big. You may accidentally skip few lines that should be executed.

具有多个return语句的函数令人困惑,如果方法太大,则不容易理解。您可能会意外跳过应该执行的几行。

If you are writing a small program that doesn't need any future maintenance it may be okay to ignore that rule. However for longer programs that may be maintained in future, when people do bug fix after some time, it is quite possible to overlook some block of code returning back to the calling function and put some code below that block assuming that it will run. One such example may be clean up code.

如果您正在编写一个不需要任何日后维护的小程序,则可以忽略该规则。但是对于将来可能维护的较长程序,当人们在一段时间后进行错误修复时,很可能会忽略一些代码块返回到调用函数并将一些代码放在该块下面,假设它将运行。一个这样的示例可以是清理代码。

It can be easily argued that programmer should be more careful, but following one return policy will make it easier to avoid such mistakes.

可以很容易地认为程序员应该更加小心,但遵循一个退货政策将更容易避免这种错误。