Remember when JavaScript was a language used to make elements change on the page when the cursor was over them? These days are over, every language evolves over time, and so does the way we use them. Look at the code you wrote one or two years ago: do you feel ashamed? If yes, this post is for you 🙂
I'll try here to list some good practices to make your JavaScript code easier to write, read and maintain.
The first advice I'm giving you is to use a linter that will check you respect some rules that make your code consistent from a file to another, especially if you're several developer to work on the same project: indentation, spaces in parenthesis, replace ==
by ===
…
But more important, make your linter fix automatically your code for you when possible. ESLint does that very well (with the --fix
option), and it's well-integrated with all major IDEs to auto-fix files on save.
You can also use Prettier which is more focused on formatting than linting, but the result is basically the same 😉
Next point will help you chose what rules to use with your linter:
If you wonder what rules to want for your code, here is a hint: StandardJS. It's a very strict linter that won't give you any choice in the rules, but each of them is more and more admitted by the community. Here are some examples:
if
) and in curly braces, not inside parenthesisStandardJS is a standalone Node module that can lint and fix your code, but if you want to use it in a big existing project and deactivate some rules (because some would need a lot of modifications), you can also use the ESLint predefined config. For instance I deactivated the rules no-mixed-operators and import/no-webpack-loader-syntax.
If you develop with JavaScript there's no way you haven't heard of ES2015+ (or ES6, ES7…) features. Here are the ones I couldn't live without anymore:
x => x * 2
is very useful with functional programming (see next point)function doSomething() {
const a = doSomethingElse()
const b = doSomethingWithA(a)
const otherResults = { c: '😺', d: '🐶' }
return { a, b, ...otherResults } // equivalent to { a: a, b: b }
}
const { a, c, ...rest } = doSomething() // Also works with arrays!
// `rest` looks like { b: ..., d: '🐶' }
async/await
:// Please try to write the same code with classic promises ;)
async function doSomething() {
const a = await getValueForA()
const b = await getValueForBFromA(a)
const [c, d] = await Promise.all([
// parallel execution
getValueForC(), getValueForDFromB(b)
])
const total = await calculateTotal(a, b, c, d)
return total / 1000
}
Wonder how to use these fantastic features? One of my articles gives you some advice! (By the way, with latest version of Node.js you probably won't need Babel anymore to use the greatest new features 😀)
Very hype right now, functional programming is gaining a lot of success recently, not only in JavaScript. The reason? It makes the code more predictible, safer, deterministic, and a lot easier to maintain when you're used to it. Here are some simple advices:
First stop using for loops. In most (every?) case you don't need them. For instance:
const arr = [{ name: 'first', value: 13 }, { name: 'second', value: 7 }]
// Instead of:
const res = {}
for (let i = 0; i < arr.length; i++) {
const calculatedValue = arr[i].value * 10
if (calculatedValue > 100) {
res[arr[i].name] = calculatedValue
}
}
// Prefer:
const res = arr
.map(elem => ({ name: elem.name, calculatedValue: elem.value * 10 }))
.filter(elem => elem.calculatedValue > 100)
.reduce((acc, elem) => ({
[elem.name]: calculatedValue,
...acc
}), {})
Okay I admit this is a very extreme example, and if you're not used to functional programming it may look more complicated. Maybe we can simplify it:
const enrichElementWithCalculatedValue =
elem => ({ name: elem.name, calculatedValue: elem.value * 10 })
const filterElementsByValue = value =>
elem => elem.calculatedValue > value
const aggregateElementInObject = (acc, elem) => ({
[elem.name]: calculatedValue,
...acc
})
const res = arr
.map(enrichElementWithCalculatedValue)
.filter(filterElementsByValue(100))
.reduce(aggregateElementInObject, {})
Here we defined three functions that basically exactly what their names say. Second advice: create local functions (even in existing functions) to document your code without comments!
Note that the three local functions don't modify context they're executed in. No external variable is modified, no other service is called… In functional programming they're called pure functions. They have some great advantages:
So my third advice: use pure functions a lot!
arr.indexOf(elem) !== -1
in favor of arr.includes(elem)
.Oh and to conclude, the greatest advice I can give you: always refactor your code! Making improvements to a module you wrote one year ago? Take the opportunity to replace var
with const
, to use arrow functions or async/await
to simplify the code… It's always nicer to work on code you like 😉
This article was originally posted on my blog.
Great Post! Love the JS code without the semi-colon. I'm definitely gonna install StandardJS
A nice little article but your first code example for functional programming is misleading.
The array declaration used for both // Instead of: and // Prefer: is shown visually to be a part of the // Instead of: block.
This might suggest to the user at a glance "oh, the new way is prettier". But it's a misrepresentation of what's going on.
Sorry to be a neg, but you're going to sell an idea, you gotta sell the truth.
You're absolutely right, my bad. It's fixed 😉
Prettier lints like crazy. It enforces an absolute coding standard. It's main virtue is that it only has two configuration options, tabs vs spaces and quotes vs apostrophes. Otherwise, everyone who uses it gets standard, well-formatted code. I am a huge fan.
While I love new language features and usually feel a need to upgrade "old" code to newer standards, I think its also important to put things into perspective. Sometimes the old code is perfectly fine and refactoring will have a higher cost than benefit. If the only benefit is that it makes yourself feel better about the code, maybe being more tolerant towards "old" code has a higher reward.
With that being said, I absolutely advocate coding standards and usage of new language features!
If you map, then filter, then reduce an array in code that I manage, you're not getting your code in.
whilst array.map and array.forEach are unarguably more readable than for loops, aren't for loops still considerably faster in most browsers?
"The reason? It makes the code more predictible, safer, deterministic, and a lot easier to maintain when you're used to it."
Can you elaborate why? I have never programmed in a functional way in a serious project so I would like to know the reason behind those affirmations.
Thanks.
Fantastic article!
Great post! I want to take JS code a new level, I guess you have given me something keep in mind a lot.