Properly formatted code is a widely agreed-upon virtue in the craft of programming. There are sometimes differences of opinions on exactly what the right format is, but generally it is formatting that contributes to the readability of code and understanding of the intent of the code.
Perhaps the most common rules list for ESLint is the AirBnB style guide. As of mid-2021, it gets over 2.5 million downloads per week from NPM.
However, before adding something to our project, we should know what it is and what value it provides.
What is AirBnB’s ESLint Config Package?
A brief technical perspective:
AirBnB released a free downloadable package (eslint-config-airbnb) that contains an .eslintrc file with AirBnB’s recommended ESLint rules. This can easily be plugged into your project and AirBnB style rules are applied.
A Code Style perspective:
To summarize, the package translates clean code and code formatting standards developed by AirBnB into rules enforced by ESLint.
Why use AirBnB’s ESLint Config and Style Guide?
- The AirBnB rules list is an excellent platform for starting out and can save you many hours
- You can modify and overwrite rules as you create your own standards and opinions
- It’s especially helpful for new devs who otherwise might be inexperienced or unaware of the possibilities ESLint offers
- Did I mention it saves time?
- AirBnB is hiring some of the smartest devs in the world. I believe it is wise to learn from them.
- I’m not saying you should surrender your curiosity or blindly follow the ‘experts’.
- However, it is wise to learn from others and not reinvent the wheel.
- If coding is a craft, there is art and beauty in perfection.
- Reading the AirBnB style guide is an excellent reinforcement for all levels of devs
- It’s like a concise version of Bob Martin’s book “Clean Code”.
The 21 Best Rules in AirBnB’s ESLint Rules List and Style Guide
These are style guide rules that caught my eye. Are they really the 21 best? Read the style guide and decide for yourself.
All of these rules are found in the style guide; most of them have a related .eslintrc rule in eslint-airbnb-config that enforces the recommended behavior. The style guide rules that don’t have related eslint rules are simply not programmatically enforceable.
- Use less boilerplate code (rules 3.3 and 3.4). Drop reserved words where possible.
- Eliminate quotes and improve optimization (3.6). This can actually improve performance.
- Use prototype instead of calling object methods directly (3.7). I need some self-scrutiny here.
- Usually we should use spread operators, but not when mapping over iterables (4.5). This avoids creating an unneeded extra array.
- Please format arrays of objects nicely with new lines for each brace (4.7).
- Avoid creating unneeded references when you can use object destructuring (5.1).
- Don’t break strings into multiple lines (6.2)? I’m not sure I agree with this one…yes, it makes the code less searchable, but I think it’s harder to work with a one-line giant string.
- Use well-named function expressions (7.1). Bob Martin would be proud.
- Instead of pulling values from the
argumentsparameter, use rest syntax (7.5 and 7.6)
- Always put default params last (7.9). A function is far more readable this way.
- Don’t mutate parameters passed into a function (7.12). This can have unintended results.
- Use higher-order functions instead of old-school loops (11.1).
- Don’t use bracket notation to access properties (12.1).
- Don’t declare multiple variables using const and commas (13.2). This is an important one: declaring one const or let for each variable lets you step through each line with the debugger.
- This one is interesting: don’t use ++ (13.6). It can cause bugs due to automatic semicolon insertion. Who knew?
- Use triple equals, not double equals (15.1). This is obvious…to most people. But then one day you find yourself transferred to a project with 30,000 lines of code using ==.
- Put the
elseon the same line as the closing curly brace of the
if(16.2). This one seems nit-picky…but it’s good to have everyone on a team formatting code the same way.
- Don’t rely on the second half of an
&&statement to execute your code (17.2). This gives me something to consider…I like assigning variables this way.
- Cache any lookups (26.2). The style guide refers to jQuery, which I hope you don’t care about anymore. However, the concept is good.
- Use /**/ for multi-line comments (18.1). When you decide a comment is necessary, make sure it’s easy to read.
Is TypeScript Compatible with eslint-config-airbnb ?
Yes, with eslint-config-airbnb-typescript. It doesn’t have TypeScript-specific rules, it is simply compatible.
Since you also like TypeScript, check out my list of 50 difficult TS questions.
Read this to learn how to use ESLint –fix in your project.
Now that you’ve learned about setting ESLint rules in your project, here’s how to disable those same rules.
Installing the AirBnB style guide also installs an accessibility plugin called jsx-a11y. Here’s how to use eslint-plugins-jsx-a11y to make your site more accessible.