如果“如果”是函数中的最后一个控制语句,它的块总是执行“return”,那么我应该使用“else”吗? [英] If "if" is the last control statement in function and its block always executes "return", then should I use "else"?

查看:115
本文介绍了如果“如果”是函数中的最后一个控制语句,它的块总是执行“return”,那么我应该使用“else”吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这段代码(用C编写来演示这个问题):

  int foo1(int a)

如果(a)
返回33;
返回22;


int foo2(int a)
{
if(a)
return 33;
else
return 22;

code


$ b

正如你所看到的, foo1(int a) foo2(int a)完全相同。 foo1(int a)更短,但我得到的印象是 foo2(int a)更合乎逻辑这只是我的印象)。哪一个更好?如果是功能代码中最后一个控制语句,并且它的块总是执行<$>,那么将 else c $ c> return

解决方案

至少对于任何如果立即返回的条件,省略 else 。原因在于 else 已经隐含了,所以显式地添加它不仅是多余的,而且增加了下面代码的嵌套级别,从而使得代码难以阅读。以下面的代码为例。

  public int Test()
{
if (条件)
返回0;
else
{
var ex1 = doCmd1();
var ex2 = doCmd2();
var result = ex1 + ex2;
返回结果;
}
Console.WriteLine(我在打印前返回);
返回-1;

$ / code>

如果你对这段代码进行简单的浏览,你可能会想到该方法可以退出的三种方式。然而,情况并非如此 - 它只能通过两种方式退出。 if-else 之外的所有内容都完全无法访问。现在,这里是相同的代码,但没有额外的 else

  public int Test()
{
if(condition)
return 0;

var ex1 = doCmd1();
var ex2 = doCmd2();
var result = ex1 + ex2;
Console.WriteLine(我在打印前返回);
返回结果;
}

这样更清晰地阅读并快速识别操作的结果将会是。

Consider this code (written in C to demonstrate the problem):

int foo1(int a)
{
    if(a)
         return 33;
    return 22;
}

int foo2(int a)
{
    if(a)
         return 33;
    else
         return 22;
}

As you can see, foo1(int a) does exactly the same as foo2(int a). foo1(int a) is shorter, however I get the impression that foo2(int a) is just more logical (that's only my impression). Which one is better? Is it good to use else with if that is the last control statement in function code and its block always executes return?

解决方案

I don't know about C specifically, but in C# at least the convention is to omit the else for any if conditions that immediately return. The reasoning is that the else is already implied, so explicitly adding it is not only redundant, but increases the nesting level of following code, thus making the code harder to read. Take a look at the following code as an example.

public int Test()
{
    if (condition)
        return 0;
    else
    {
        var ex1 = doCmd1();
        var ex2 = doCmd2();
        var result = ex1 + ex2;
        return result;
    }
    Console.WriteLine("Here I'm printing before returning");
    return -1;
}

If you take just a very brief glance at this code, you might think there are three ways the method can exit. However, that's not the case - it can only exit in two ways. Everything outside the if-else is completely unreachable. Now, here's the same code, but without the extra else:

public int Test()
{
    if (condition)
        return 0;

    var ex1 = doCmd1();
    var ex2 = doCmd2();
    var result = ex1 + ex2;
    Console.WriteLine("Here I'm printing before returning");
    return result;
}

This is much more clear to read and quickly identify what the results of the operation are going to be.

这篇关于如果“如果”是函数中的最后一个控制语句,它的块总是执行“return”,那么我应该使用“else”吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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