Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Port tslint-microsoft-contrib's no-increment-decrement #3556

Closed
JoshuaKGoldberg opened this issue Dec 6, 2017 · 3 comments
Closed

Port tslint-microsoft-contrib's no-increment-decrement #3556

JoshuaKGoldberg opened this issue Dec 6, 2017 · 3 comments

Comments

@JoshuaKGoldberg
Copy link
Contributor

See #1142.

I remember reading a book (JavaScript: The Good Parts? Clean Code? The Art of Unit Testing?) by one of the big influential programming folks and encountering a passage arguing in favor of this type of rule. The justification was something like these points:

  • It's easy to type +i or -i instead of --i
  • Standardizing widths of increments/decrements makes them easier to read
  • It's easy to forget the difference between ++i and i++

Perhaps this rule should be named increment-decrement and only ban the pre- operators, to offend fewer people? The first point is the only one of the three I feel would withstand a real debate.

@ajafff
Copy link
Contributor

ajafff commented Dec 6, 2017

It's easy to type +i or -i instead of --i

when used as an ExpressionStatement, no-unused-expression would warn you about that.
This is only an issue when assigning the result.
But even then you get an error from prefer-const because you never reassign the variable (if this is the only place you might have modified it).

@JoshuaKGoldberg
Copy link
Contributor Author

Counter-example:

const items = ["apple", "banana", "cherry"];
let index = 0;

while (+index) {
    // ^
    if (items[index][0] === "b") {
        ++index;
    }
}

@JoshuaKGoldberg
Copy link
Contributor Author

Fixed by #3575.

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

No branches or pull requests

2 participants