Javascript: No rule for avoiding single letter names

Created on 16 Aug 2016  路  9Comments  路  Source: airbnb/javascript

While the readme says: Avoid single letter names. Be descriptive with your naming https://github.com/airbnb/javascript#naming--descriptive

The rule is deactivated: https://github.com/airbnb/javascript/blob/be6e13f7b91ede245602c9760d112905ac497dcc/packages/eslint-config-airbnb-base/rules/style.js#L48

What about activating the rules with min 2 characters?

question

Most helpful comment

@ljharb @swist even if you use loops why not use index instead of i? Wouldn't that match with Avoid single letter names. Be descriptive with your naming.

All 9 comments

a case against enabling it:

let array = getArrayOfItems()

for (let i in array) {
  array[i] = modify(array[i])
}

This is a common pattern that would get flagged up by switching this rule on

@swist using loops at all is discouraged by this guide, so that's not a use case (also, never use for..in on arrays, use for or .forEach; for..in is for iterating object properties).

This rule tends to be more annoying than useful in a large codebase. In an ideal world, we'd never use loops or index arguments, and this rule wouldn't even apply - but if we ever do have those things (legacy code, edge cases, etc) then the rule would just be noise.

@ljharb @swist even if you use loops why not use index instead of i? Wouldn't that match with Avoid single letter names. Be descriptive with your naming.

I didn't mean writing new code because you probably won't end up with a loop like this anyway. But in legacy code there are going to be a lot of loops like this that get flagged, because well... Hasn't this been the convention since forever?

What has been a conventions since forever? i for index or not introducing new rules that would require you to change code?

People coming from c-like languages would probably write a loop with i. Introducing a rule that results in a major code change is fine, as long as it doesn't require you to go manually through everything. Off the top of my head I can't come up with a transform that would clean up a big codebase so that this no longer lights up like a christmas tree :)

The spirit of the rule is to use meaningful variable names. Anyone familiar with a loop will be familiar with i as an indexing variable, and I've never seen anyone confused by that, that would not be confused if it were named index.

yep, index works well even for cases like this:

[334, 464, 22].map((item, index) => { doSomething(item, index) })

Is there any conclusion from this discussion? Should I create PR? Otherwise I would close the issue.

I'd be fine updating "good" examples not to use i, but there shouldn't be many since looping is a bad example :-)

We're not currently planning to enable the id-length rule here, and will leave that for top-level apps to define.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stephenkingsley picture stephenkingsley  路  3Comments

golopot picture golopot  路  3Comments

zurfyx picture zurfyx  路  3Comments

surfaceowl picture surfaceowl  路  3Comments

progre picture progre  路  3Comments