简体   繁体   English

对于if / else检查,返回false

[英]Returning false for a if / else check

Sometimes you need to check if something is set or not, then you perform an operation. 有时您需要检查是否设置了某些内容,然后执行操作。 Sometimes there are many of these checks and I hate the indentation that is causes. 有时,其中有许多检查,而我讨厌造成这种缩进。

To minimize the nesting, I always check the opposite and return and then continue on the same indentation. 为了最大程度地减少嵌套,我总是检查相反的方向并return ,然后继续使用相同的缩进。

For instance, say the following 例如,说以下

if (user.isOwner)
{
    // Do some operation
    // Set a value $accepted to true or false

    if ($accepted)
    {
        // Do more operation
        // And so on
    }
}

Instead, I write the above code as 相反,我将上面的代码写为

if ( ! user.isOwner) return;

// Do some operation
// Set a value $accepted to true or false

if ( ! $accepted) return;

// Do more operation
// And so on

Could this ever lead to any problem? 这会导致任何问题吗? The function above when called, is not expected to return any result (ie I call is as doThisForMe() ). 上面的函数在被调用时,不会返回任何结果(即我调用的方法与doThisForMe() )。 My coding here is done in PHP and Javascript. 我的编码是用PHP和Javascript完成的。 I don't know if this question would be language dependent. 我不知道这个问题是否取决于语言。

As long as you don't want to return something this looks fine to me. 只要您不想退货,这对我来说就很好。 Otherwise you could use the one-liner if. 否则,您可以使用单线。

return (<statemen>) ? <handles true> : <handles false>;

I am not sure if it is working in PHP too, but it is surely working with javascript. 我不确定它是否也可以在PHP中使用,但肯定可以在javascript中使用。

This is something that is usually quite useful when you try to minimise the indentation in your script files. 当您尝试最小化脚本文件中的缩进时,这通常非常有用。 There are generally no problems with it. 通常没有问题。

The only problem that could possibly arise is if you want to add an else block to your functions. 可能出现的唯一问题是,如果要向功能添加else块。 With the former syntax, it'd be a simple addition of else to the end of it. 使用前一种语法,将在其末尾简单地添加else If you're using return instead, that'd be impossible, and you'd have to convert it to the former syntax. 如果使用的是return ,那将是不可能的,并且必须将其转换为以前的语法。

This applies to both PHP and JavaScript. 这适用于PHP和JavaScript。 It also applies to most languages that have a similar kind of syntax. 它也适用于语法相似的大多数语言。

In the case of Javascript this should do no harm at all since javascript functions return undefined either way. 对于Javascript,这完全没有害处,因为javascript函数以任何一种方式返回undefined (you writing return; or not) (您写的回报还是没有)

For PHP I'm not really sure, but I can't imagine any problems right now. 对于PHP,我不确定,但是现在无法想象任何问题。

But I would think of this as bad practice since indentation helps to see what's going on. 但是我认为这是不好的做法,因为缩进有助于了解发生了什么。 If you have many of those checks in one function, maybe you should think about creating more, smaller functions instead of one big function, because this too, helps reading your code. 如果您在一个函数中进行了许多检查,那么也许您应该考虑创建更多,较小的函数,而不是一个大函数,因为这也有助于阅读代码。

if-elseif-else statements stop doing comparisons as soon as it finds one that's true. if-elseif-else找到正确的if-elseif-else语句,它将停止进行比较。 if-if-if does every comparison. if-if-if每个比较。 The first is more efficient. 第一种是更有效的。

In your case, or in cases where control will leave the method (exceptions), there is no difference between doing multiple if statements and doing if-elseif-else statements. 在您的情况下,或者在控制权将离开方法的情况下(例外),执行多个if语句和执行if-elseif-else语句之间没有区别。

However, it's best practice to use if-elseif-else anyhow. 但是,最好还是使用if-elseif-else Suppose you change your code such that you don't do a return in every if block. 假设您更改代码,以免在每个if块中都不返回。 Then, to remain efficient, you'd also have to change to an if-elseif-else idiom. 然后,为了保持效率,您还必须更改为if-elseif-else惯用语。 Having it be if-elseif-else from the beginning saves you edits in the future, and is clearer to people reading your code. 从一开始就将它设为if-elseif-else可以节省将来的编辑工作,并且对于阅读您的代码的人来说更加清晰。

These days to write a program is not a big task whereas the maintainabililty of the code is the major requirement. 如今,编写程序并不是一项艰巨的任务,而代码的可维护性是主要要求。 Because of the the chaning world and there requirements, We are required to update or change the code time to time. 由于变化的世界和那里的要求,我们需要不时更新或更改代码。 Thus it is very much required to indent the code for better readability . 因此,非常需要缩进代码以提高可读性

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

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