开发者

Is using the increment operator in javascript discouraged?

I've seen code lik开发者_开发技巧e the following in some JS libraries:

var i;
for (i = 20; i >= 0; i -= 1) 
{
}

I wonder why they would choose to decrement i in this fashion. Is there something problematic about using ++ and -- in javascript?


The increment and decrement operators work just fine in JavaScript. A lot of people don't use them because Crockford recommends against using them in his book JavaScript: The Good Parts. I generally like Crockford but I do feel this particular recommendation is a bit draconian. I've yet to see any convincing situation or evidence that suggests they are harmful.


Douglas Crockford, aka the godfather of JavaScript, discourages the ++.

Confusing Pluses and Minuses

Be careful to not follow a + with + or ++. This pattern can be confusing. Insert parens between them to make your intention clear.

total = subtotal + +myInput.value;

is better written as

total = subtotal + (+myInput.value);

so that the + + is not misread as ++.

Personally, the -- or ++ is completely okay in a for loop as its meaning is very clear. I tend to follow most of Crockford's guidelines though because its generally the basis of JS conventions.

See more here.


I think that loopwise, the increment and decrement operators aren't really harmful (from what I've seen) but you shouldnt use it outside of loops.


As others pointed Douglas Crockford discouraged it as a bad feature of the language, most notably in his book "JavaScript: The Good Parts". It can be used in creating easy to miss errors and code harder to reason about.

What I don't see here, and I'll add, is a quote of Douglas Crockford from one of his lectures on JavaScript:

For example, the ++ operator, this was added for B for doing point arithmetic. We have since determined that point arithmetic is harmful, so we don't do it anymore. Modern languages do not support point arithmetic. The last popular language that had point arithmetic was C++. A language so bad it was named after this operator. But the operator refused to die. It's still in all of our languages even though we don't need it to increment pointers anymore.

Now we use it to add one to variables. Which is completely unnecessary. But we do it. Unfortunately it leads to a style of programming in which you try to write one-liners. In which you try to take a whole bunch of stuff and try to smush it down into one line. That leads to really bad code. Stuff which is very hard to maintain, very hard to correct. We've seen security errors, buffer overruns, those sort of things. This operator is always implicated in those sorts of security errors. h

I found in my own practice, any time I use ++ anywhere, this thing takes hold of me. I can't control it. It makes me want to take code and try to mush it down to one line. Even though I know that's a stupid thing to do, I can't control myself. This things takes hold of me, and I start writing really stupid stuff thinking I'm being really smart. Eventually I had to stop. I couldn't do it a little bit. I had to stop completely.

I said no more ++ from now on. It's +=1. I can relax. It's easy. I can just write good programs now. Food tastes better. Everything's great. For a while I thought it was just me. But now I'm recommending everybody +=1 all the time every time. +=1 it's great. So much better.

I hear from people all the time saying I want to be able to write x++ cause it means the same thing, instead of having to go... (mimics typing few keystrokes) I can't go... (mimics typing few less keystrokes) I don't have that kind of time! Except that the typing time is irrelevant. It is completely irrelevant. We don't spend our time typing. More than that it's ++x which means about the same thing as x+=1.

Every time I see someone writing x++ in increment position I have to ask does this colon understand the difference between pre-commit and post-commit. It means I have to look at every ++ in this program and ask did he get this one right. Did he get this one right? 'Cause it's a little dyslexic thing which it's really hard to tell when you've got them reversed. It causes an off by one error that's only off for an instant. But that's enough to cause a bug. It's really hard to debug those things.

The argument in favor of ++ is it improves readability, which is bullshit. It does not improve readability. It improves ambiguity. It improves confusion, which are things which are not desirable.

I was redoing some code and I saw ++x; ++x; So what's going on there? It's possible that it was a copy and paste error. Except the code seemed to be working. More likely what had happened was someone had done a ++x and then someone else noticed there's an off by one problem here. So they did it again. If the original code had said +=1 then the obvious solution would be +=2.

It raises the question why do we think we need completely different syntax for adding one to a variable than every other value? How does that make any sense? The answer is it does not make any sense. There's this emotional attachment we have to bad grammar in our languages that makes us feel ++ that's who I am. It's part of who I am. If you take ++ away from me what am I? What's left? (whimpers) For no cost, by adopting a more rigorous style, many classes of errors can be automatically avoided.

I agree with almost all of it (especially with the key ideas I made bold) and see no point of using ++ instead of += 1 anywhere.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜