WIP - JavaScript style guide #12
sindresorhus
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
At some point I want to make a JS style guide. For now, this is just a braindump.
Prioritize readable code
That means using readable variable and function names. No abbreviations.
For example, use
const error
instead ofconst err
.More here: https://github.com/sindresorhus/eslint-plugin-unicorn/blob/master/docs/rules/prevent-abbreviations.md
Make boolean variables obvious
Boolean variables should use
is
,has
,can
,will
, ordid
prefix.For example:
.visible
→.isVisible
.rainbow
→.hasRainbow
Hopefully, there can be a lint rule for this: typescript-eslint/typescript-eslint#515
Only accept an
Iterable
if it's used directlyFunctions should only accept an
Iterable
if it can actually take advantage of it. If it only does[...iterable]
internally, it should just accept an array. Users can always use the spread syntax at callsite if they have anIterable
. The problem with accepting anIterable
when you're not actually not taking advantage of the iterator interface is:Iterable
, which can cause weird issues where a user accidentally passes in a string and your function uses each character in the string.More info
Beta Was this translation helpful? Give feedback.
All reactions