简体   繁体   English

写这个if..then逻辑的最干净的方法是什么?

[英]What is the cleanest way to write this if..then logic?

They both do the same thing. 他们都做同样的事情。 Is one way better? 一种方式更好吗? Obviously if I write the code I'll know what I did, but how about someone else reading it? 显然,如果我编写代码,我会知道我做了什么,但其他人如何阅读呢?

if (!String.IsNullOrEmpty(returnUrl))
{
    return Redirect(returnUrl);
}
return RedirectToAction("Open", "ServiceCall");

OR 要么

if (!String.IsNullOrEmpty(returnUrl))
{
   return Redirect(returnUrl);
}
else
{
    return RedirectToAction("Open", "ServiceCall");
}
return String.IsNullOrEmpty(returnUrl) ? 
            RedirectToAction("Open", "ServiceCall") : 
            Redirect(returnUrl);

I prefer that. 我更喜欢。

Or the alternative: 或替代方案:

return String.IsNullOrEmpty(returnUrl)  
            ? RedirectToAction("Open", "ServiceCall")  
            : Redirect(returnUrl);

I believe it's better to remove the not (negation) and get the positive assertion first: 我认为最好删除not(否定)并首先得到肯定的断言:

if (String.IsNullOrEmpty(returnUrl))
{
   return RedirectToAction("Open", "ServiceCall");
}
else
{
    return Redirect(returnUrl);
}

-or- -要么-

// Andrew Rollings solution
return String.IsNullOrEmpty(returnUrl) ? 
                    RedirectToAction("Open", "ServiceCall") : 
                    Redirect(returnUrl);

One style issue: 一个风格问题:

if (String.IsNullOrEmpty(returnUrl))
{
    return RedirectToAction("Open", "ServiceCall");
}
return Redirect(returnUrl);

When you cancel out the double negation it reads a whole lot better, no matter which brace style you choose. 当您取消双重否定时,无论您选择哪种支撑样式,它都会更好地读取。 Code that reads better is always best ;) 读得更好的代码总是最好的;)

第二种方式更好,没有混淆你的意思......

I think this is a fairly small matter of style. 我认为这是一个相当小的风格问题。 I'd argue that your two samples are equally readable. 我认为你的两个样本同样可读。

I prefer the former, but other people prefer only one exit point from a function and would probably suggest something like: 我更喜欢前者,但其他人只喜欢一个函数的一个退出点,可能会建议:

if (!String.IsNullOrEmpty(returnUrl))
{
   result = Redirect(returnUrl);
}
else
{
    result = RedirectToAction("Open", "ServiceCall");
}

return result;

I like the first example because it's more obvious that this excerpt will return. 我喜欢第一个例子,因为这个摘录更明显地返回。 If both return s are in indented blocks, it takes just a little more mental effort to tell. 如果两个return s都是缩进的块,那么需要花费更多精力去辨别。

Eliminate the 'double-negative' and use the fully-expanded style. 消除'双重否定'并使用完全展开的风格。 I am sure most compilers now can suitably optimize the code on your behalf, so no reason to take short-cuts for readability. 我相信大多数编译器现在可以代表您适当地优化代码,因此没有理由采用捷径来提高可读性。

If it's the entirety of the method then I'd say the second (using the else) is a bit more elegant. 如果它是整个方法,那么我会说第二个(使用else)更优雅一点。 If you have preceding code or (especially) much more code before the return in the else case, I'd say it's better not to put the else. 如果你在else的情况下返回之前有代码或(特别是)更多的代码,我会说最好不要把else。 Keeps from code becoming too indented. 保持代码变得过于缩进。

ie either: 即:

void myfunc()
{
    if (!String.IsNullOrEmpty(returnUrl))
    {
        return Redirect(returnUrl);
    }
    else
    {
        return RedirectToAction("Open", "ServiceCall");
    }
}

or 要么

void myfunc()
{
    // ... maybe some code here ...

    if(!String.IsNullOrEmpty(returnUrl))
    {
       return Redirect(returnUrl);
    }

    // ... a bunch of other code ...

    return RedirectToAction("Open", "ServiceCall");
}

This code feels messy because of the unavoidable double-negative logic (and shuffling things around isn't going to clear it up). 这段代码因为不可避免的双重负面逻辑而感到凌乱(而且随之而来的东西也不会清除它)。 Whichever arrangement you use, I think you should add some comments so that the reader doesn't need to do a double-take: 无论您使用哪种安排,我认为您应该添加一些注释,以便读者不需要进行双重操作:

if (!String.IsNullOrEmpty(returnUrl))
{
  // hooray, we have a URL
  return Redirect(returnUrl);
}
else
{
  // no url, go to the default place
  return RedirectToAction("Open", "ServiceCall");
}

How about: 怎么样:

if (!String.IsNullOrEmpty(returnUrl))
    return Redirect(returnUrl);
else
    return RedirectToAction("Open", "ServiceCall");

Don't think there's a better way... it depend on each developer. 不要认为有更好的方法......它取决于每个开发人员。 That's why before starting a project you should decide what is the coding standard ... 这就是为什么在开始一个项目之前你应该决定什么是编码标准......

I like the first better. 我更喜欢第一个。

if ( ) 
{
    return ... 
}
return 

For me it reads like a "default" you can chain more conditions but at the end there is a default. 对我而言,它看起来像一个“默认”,你可以链接更多的条件,但最后有一个默认。

Of course it is a matter of style. 当然这是一个风格问题。

Additional question. 补充问题。

Is it C# style to put the square brackets and in a single line? 将方括号放在一行中是C#样式吗?

if ( ) 
{
}
else
{
}

I have seen this permeated into Java code samples in SO, and I'm wondering this is the root cause. 我已经看到这渗透到SO中的Java代码示例中,我想知道这是根本原因。

EDIT 编辑

@Owen. @Owen。 I mean, Is it C# style using this form? 我的意思是,使用这种形式的是C#风格吗?

if ()  
{
    code ... 
}
else
{
    code...
}

Rather than this ( which would be Java preffered ) 而不是这(这将是Java优先

if ( ) { 
    code ... 
} else { 
   code ...
}

I have had some arguments in the past about this, but most of the times, only with people that come from C# background. 我过去曾经有过一些争论,但大多数时候,只有来自C#背景的人。

As here , but more readable: 这里 ,但更具可读性:

return
  string.isNullorEmpty(returnUrl) ? 
     RedirectToAction("Open", "ServiceCall") :
     Redirect(returnUrl);

This way works, is readable and non-redundant. 这种方式有效,可读且无冗余。

From a maintenance perspective I prefer the first version. 从维护的角度来看,我更喜欢第一个版本。 I've seen less errors with a consistent "get out early" style. 我已经看到了一致的“早出”风格的错误。

The ternary operator (?:) is OK, but only if it is very unlikely that new code will be inserted before the 2nd return statement. 三元运算符(?:)没问题,但只有在第二个返回语句之前插入新代码的可能性很小。 Otherwise when the time comes to add something new into the 2nd code path, the ternary statement has to go back to being an if/else block anyway. 否则,当需要在第二个代码路径中添加新内容时,三元语句无论如何都必须返回到if / else块。

When I have only one line sometimes I compact the if statement like this: 当我只有一行时,我会压缩if语句,如下所示:

if(String.IsNullOrEmpty(returnUrl)) { 
  return RedirectToAction("Open", "ServiceCall"); 
}
else{ return Redirect(returnUrl); }

Although when I look at it, Andrew Rollings might have the best solution although I had never thought of using it until today. 虽然当我看到它时,安德鲁罗林斯可能有最好的解决方案,虽然我从未考虑过使用它直到今天。

LFSR's "else" solution is the most maintainable and readable. LFSR的“其他”解决方案是最易于维护和可读的。

Using a separate else clause allows logic to be added without inadvertently changing the flow. 使用单独的else子句允许添加逻辑而不会无意中更改流。 And it's bad enough having multiple exit points without hiding two of them in a single ternary operator statement! 并且它有足够多的出口点而不会在一个三元运算符语句中隐藏它们中的两个!

The first one. 第一个。 You don't need any else if you are returning a value inside the if statement. 如果要在if语句中返回值,则不需要任何其他内容。 So: 所以:

if (!String.IsNullOrEmpty(returnUrl))
    return Redirect(returnUrl);
return RedirectToAction("Open", "ServiceCall");

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

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