Airbnb Javascript Style Guide

creativepartners 857 views 17 slides May 11, 2017
Slide 1
Slide 1 of 17
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17

About This Presentation

Given at the CCD session at RocketInternet - 10-05-2017


Slide Content

Airbnb JavaScript Style Guide() {

Code is first for human… not computer “

Why Style Guide? Best practices. More readable code Easy maintenance

Single standard? No, like a lot • Airbnb • Google • JQuery • W3Schools • Node • StandardJS We focus on Airbnb

References Use const or let for all of your references - avoid using var const and let only exist in the blocks they are defined in var is a function-scoped

Use one const or let declaration per variable Don't chain variable assignments Avoid using ++ , -- Variables

Literal syntax for object creation Don't use reserved words as keys Use object method shorthand Use property value shorthand Group your shorthand properties at the beginning of your object declaration. Objects

Use the literal syntax for array creation Use Array#push instead of direct assignment to add items to an array Use spread operator ... to copy arrays Arrays

Use single quotes '' for strings. Strings that cause the line to go over 100 characters should not be wrapped by concatenation When building up strings, use template instead of concatenation Do not unnecessarily escape characters in strings as backslashes harm readability Strings

Almost there ...

Use named function expressions instead of function declarations Function declarations are hoisted -- easy to use before it is defined in the file -- harms readability and maintainability Name the expression Wrap immediately invoked function expressions in parentheses Use default parameter syntax rather than mutating function arguments Functions

Put default parameters last Spacing in a function signature Never mutate parameters Never reassign parameters Multiline signatures should : each item on a line by itself trailing comma on the last item Functions

Use === and !== over == and != Use shortcuts for booleans, but explicit comparisons for strings and numbers Ternaries should not be nested and generally be single line expressions Avoid unneeded ternary statements Comparison & Equality

Don't use iterators Prefer higher-order functions map() / every() / filter() / … and Object.keys() / Object.values() / ... over loops like for-in , as this enforces immutability Iterators

Always comma-dangle (cleaner diff), even in function definition /** ... */ multi-line comments // for single line comments // FIXME: to annotate problems // TODO: to annotate solutions Commas, Comments

Guidelines use of EcmaScript 2015 features Modules import/export Class & extends String template notation Arrow functions Furthermore... Linters ESlint - Airbnb Style .eslintrc JSHint - Airbnb Style .jshintrc JSCS - Airbnb Style Preset CSS Airbnb CSS / Sass Styleguide

References https://github.com/airbnb/javascript