jslint工具的一个提示是:

++和—— ++(递增)和——(递减) 众所周知,操作符会导致糟糕的代码 鼓励过度狡诈。他们 仅次于有缺陷的架构 使病毒和其他 安全威胁。这是一个加分项 选项,禁止使用这些 操作符。

我知道PHP结构像$foo[$bar++]可能很容易导致off-by-one错误,但我想不出一个更好的方法来控制循环比:

while( a < 10 ) do { /* foo */ a++; }

or

for (var i=0; i<10; i++) { /* foo */ }

jslint突出显示它们是因为有一些类似的语言缺少“++”和“——”语法或处理方式不同,还是有其他避免“++”和“——”的理由,我可能忽略了?


当前回答

在我看来,“显性总是比隐性好。”因为在某些时候,你可能会对这个increments语句y+ = x++ + ++y感到困惑。一个好的程序员总是使他或她的代码更易于阅读。

其他回答

考虑以下代码

    int a[10];
    a[0] = 0;
    a[1] = 0;
    a[2] = 0;
    a[3] = 0;
    int i = 0;
    a[i++] = i++;
    a[i++] = i++;
    a[i++] = i++;

由于i++被求值两次,因此输出为 (from vs2005调试器)

    [0] 0   int
    [1] 0   int
    [2] 2   int
    [3] 0   int
    [4] 4   int

现在考虑下面的代码:

    int a[10];
    a[0] = 0;
    a[1] = 0;
    a[2] = 0;
    a[3] = 0;
    int i = 0;
    a[++i] = ++i;
    a[++i] = ++i;
    a[++i] = ++i;

注意,输出是相同的。现在你可能认为++i和i++是一样的。他们不是

    [0] 0   int
    [1] 0   int
    [2] 2   int
    [3] 0   int
    [4] 4   int

最后考虑下面的代码

    int a[10];
    a[0] = 0;
    a[1] = 0;
    a[2] = 0;
    a[3] = 0;
    int i = 0;
    a[++i] = i++;
    a[++i] = i++;
    a[++i] = i++;

现在的输出是:

    [0] 0   int
    [1] 1   int
    [2] 0   int
    [3] 3   int
    [4] 0   int
    [5] 5   int

所以它们是不一样的,两者混合会导致不那么直观的行为。我认为for循环适用于++,但当你在同一行或同一指令上有多个++符号时要小心

另一个例子,比其他一些简单的递增值返回更简单:

function testIncrement1(x) {
    return x++;
}

function testIncrement2(x) {
    return ++x;
}

function testIncrement3(x) {
    return x += 1;
}

console.log(testIncrement1(0)); // 0
console.log(testIncrement2(0)); // 1
console.log(testIncrement3(0)); // 1

如您所见,如果希望此操作符影响结果,则不应在return语句中使用后递增/递减操作。但是return不会“捕获”自增/自减操作符:

function closureIncrementTest() {
    var x = 0;

    function postIncrementX() {
        return x++;
    }

    var y = postIncrementX();

    console.log(x); // 1
}

As mentioned in some of the existing answers (which annoyingly I'm unable to comment on), the problem is that x++ ++x evaluate to different values (before vs after the increment), which is not obvious and can be very confusing - if that value is used. cdmckay suggests quite wisely to allow use of increment operator, but only in a way that the returned value is not used, e.g. on its own line. I would also include the standard use within a for loop (but only in the third statement, whose return value is not used). I can't think of another example. Having been "burnt" myself, I would recommend the same guideline for other languages as well.

我不同意这种过分严格是因为很多JS程序员缺乏经验的说法。这正是典型的“过于聪明”的程序员所写的东西,我敢肯定,在更传统的语言中,以及在具有此类语言背景的JS开发人员中,这种情况更为常见。

我认为在两种情况下应该避免使用:

1)当你有一个在很多行中使用的变量,你在使用它的第一个语句(或者最后,或者更糟糕的是,在中间)上增加/减少它:

// It's Java, but applies to Js too
vi = list.get ( ++i );
vi1 = list.get ( i + 1 )
out.println ( "Processing values: " + vi + ", " + vi1 )
if ( i < list.size () - 1 ) ...

在这样的例子中,您很容易忽略变量是自动递增/递减的,甚至会删除第一个语句。换句话说,只在非常短的块中使用它,或者变量只出现在块中的几个结束语句中。

2)在同一语句中出现多个关于同一个变量的++和-的情况。很难记住在这样的情况下会发生什么:

result = ( ++x - --x ) * x++;

考试和专业测试会询问类似上述的例子,事实上,我在寻找其中一个的文档时偶然发现了这个问题,但在现实生活中,人们不应该被迫对一行代码考虑这么多。

在我看来,“显性总是比隐性好。”因为在某些时候,你可能会对这个increments语句y+ = x++ + ++y感到困惑。一个好的程序员总是使他或她的代码更易于阅读。