Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increment/decrement operator required?? #198

Open
Paladin opened this issue Jul 25, 2017 · 0 comments
Open

Increment/decrement operator required?? #198

Paladin opened this issue Jul 25, 2017 · 0 comments

Comments

@Paladin
Copy link

Paladin commented Jul 25, 2017

Just curious why something like that is the case. There are a number of instances where the increment operator can be, at the least, jarring. Most obvious example is whether after "y = x++" are y and x set to the same value, but that's just one case of potential confusion and is easily avoided by breaking it into two statements.

And that's the point. When it's an operation all by itself, it's clear. But when it's part of a series of operations, it can hide the meaning of what you're doing. At the base, "increment" and "add one" have the same meaning, but they shift the focus. "increment" hides what you're adding, and focuses on the variable, useful when it's merely a counter or index into a table. But when the variable has a meaning in itself, beyond "counter" or "index," the focus should include what you're adding, how the the state is changing.

A pertinent (but simplistic) example off the top of my head: when rolling internal calendar variables over to the next year, the sequence:

month -= 12;
year += 1;

maintains a stylistic symmetry that makes it easier to see at a glance the two operations are related. Switch in the increment operator, you gain no clarity and lose symmetry. It takes you a moment of extra thought to realize the two operations are related. And that difference means you might momentarily lose track of the idea that you're turning a calendar page and have to think about it to remember those two operations are part of a single calendar operation. And every moment where you have to stop and think about the nuances of a statement creates a crack for a bug to slip in. Off-by-one errors, anyone?

In overall usage, the increment operator can be more confusing than other operators (as noted above) so making it the preferred usage seems a little bizarre. Crockford's jsLint, for example, bans them completely. I wouldn't go that far, because there are some cases where their utility outweighs their potential ambiguity, but to force people to use them in all cases is, I think, wrong.

Edit: I thought I'd seen that before: #16

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant