为什么“继续” JavaScript中的语句不好? [英] Why are "continue" statements bad in JavaScript?

查看:172
本文介绍了为什么“继续” JavaScript中的语句不好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Douglas Crockford所着的 Javascript:The Good Parts 一书中,这是作者对继续声明所说的全部内容:

In the book Javascript: The Good Parts by Douglas Crockford, this is all the author has to say about the continue Statement:


continue 语句跳转到循环的顶部。我从未见过一段没有通过重构来删除 continue 语句的代码。

The continue statement jumps to the top of the loop. I have never seen a piece of code that was not improved by refactoring it to remove the continue statement.

这让我很困惑。我知道Crockford对JavaScript有一些非常自以为是的观点,但这对我来说听起来完全没错。

This really confuses me. I know Crockford has some very opinionated views on JavaScript, but this just sounds entirely wrong to me.

首先,继续不仅仅是跳到循环的顶部。默认情况下,它也会进入下一次迭代。那么Crockford的陈述不是完全错误的信息吗?

First of all, continue does more than just jump to the top of a loop. By default, it also progresses to the next iteration. So isn't Crockford's statement just completely false information?

更重要的是,我不完全理解为什么继续会甚至被认为是坏的。这篇文章提供了似乎一般的假设:
为什么在循环中继续是一个坏主意?

More importantly, I do not entirely understand why continue would even be considered to be bad. This post provides what seems to be the general assumption: Why is continue inside a loop a bad idea?

虽然我理解继续可能会如何在某些情况下难以阅读的代码,我认为它可以使代码更具可读性。例如:

Although I understand how continue may make code difficult to read in certain instances, I think it is just as likely that it can make code more readable. For instance:

var someArray=['blah',5,'stuff',7];
for(var i=0;i<someArray.length;i++){
    if(typeof someArray[i]==='number'){
        for(var j=0;j<someArray[i];j++){
            console.log(j);
        }
    }
}

这可以重构为:

var someArray=['blah',5,'stuff',7];
for(var i=0;i<someArray.length;i++){
    if(typeof someArray[i]!=='number'){
        continue;
    }
    for(var j=0;j<someArray[i];j++){
        console.log(j);
    }
}

继续在这个具体的例子中并不是特别有用,但它确实证明了它减少了嵌套深度的事实。在更复杂的代码中,这可能会增加可读性。

continue isn't particularly beneficial in this specific example, but it does demonstrate the fact that it reduces the nesting depth. In more complex code, this could potentially increase readability.

Crockford没有解释为什么继续不应该是使用过,那么这个意见背后是否有一些更深层的意义我错过了?

Crockford provides no explanation as to why continue should not be used, so is there some deeper significance behind this opinion that I am missing?

推荐答案

这句话很荒谬。 继续可以被滥用,但它通常帮助可读性。

The statement is ridiculous. continue can be abused, but it often helps readability.

典型用途:

for (somecondition)
{
    if (!firsttest) continue;

    some_provisional_work_that_is_almost_always_needed();

    if (!further_tests()) continue;

    do_expensive_operation();
}

目标是避免'烤宽面条'代码,你有深度嵌套的条件。

The goal is to avoid 'lasagna' code, where you have deeply nested conditionals.

编辑添加:

是的,这最终是主观的。 这是我的决定指标。

Yes, this is ultimately subjective. Here's my metric for deciding.

最后一次编辑:

当然,这个例子太简单了,你总是可以用函数替换嵌套的条件调用。但是你可能不得不通过引用将数据传递给嵌套函数,这可能会产生至少与你试图避免的那些一样糟糕的重构问题。

This example is too simple, of course, and you can always replace nested conditionals with function calls. But then you may have to pass data into the nested functions by reference, which can create refactoring problems at least as bad as the ones you're trying to avoid.

这篇关于为什么“继续” JavaScript中的语句不好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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