Why avoid increment (“++”) and decrement (“--”) operators in JavaScript?

前端 未结 16 1003
庸人自扰
庸人自扰 2020-11-22 06:23

One of the tips for jslint tool is:

++ and --
The ++ (increment) and -- (decrement) operators have been known to contribute

相关标签:
16条回答
  • 2020-11-22 06:47

    I think programmers should be competent in the language they are using; use it clearly; and use it well. I don't think they should artificially cripple the language they are using. I speak from experience. I once worked literally next door to a Cobol shop where they didn't use ELSE 'because it was too complicated'. Reductio ad absurdam.

    0 讨论(0)
  • 2020-11-22 06:49

    Consider the following code

        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++;
    

    since i++ gets evaluated twice the output is (from vs2005 debugger)

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

    Now consider the following code :

        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;
    

    Notice that the output is the same. Now you might think that ++i and i++ are the same. They are not

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

    Finally consider this code

        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++;
    

    The output is now :

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

    So they are not the same, mixing both result in not so intuitive behavior. I think that for loops are ok with ++, but watch out when you have multiple ++ symbols on the same line or same instruction

    0 讨论(0)
  • 2020-11-22 06:51

    If you read JavaScript The Good Parts, you'll see that Crockford's replacement for i++ in a for loop is i+=1 (not i=i+1). That's pretty clean and readable, and is less likely to morph into something "tricky."

    Crockford made disallowing autoincrement and autodecrement an option in jsLint. You choose whether to follow the advice or not.

    My own personal rule is to not do anything combined with autoincrement or autodecrement.

    I've learned from years of experience in C that I don't get buffer overruns (or array index out of bounds) if I keep use of it simple. But I've discovered that I do get buffer overruns if I fall into the "excessively tricky" practice of doing other things in the same statement.

    So, for my own rules, the use of i++ as the increment in a for loop is fine.

    0 讨论(0)
  • 2020-11-22 06:52

    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.

    I disagree with the claim that this over-strictness is due to a lot of JS programmers being inexperienced. This is the exact kind of writing typical of "overly-clever" programmers, and I'm sure it's much more common in more traditional languages and with JS developers who have a background in such languages.

    0 讨论(0)
提交回复
热议问题