Syntax
- Use
let
andconst
overvar
. There's a lot of discussion on all of this, but we've chosen to stick withconst
andlet
. - Prefer
const
for variables that do not or should not be reassigned. For all else, uselet
. - Prefer template literal backticks over single or double quotes for reasons Wes Bos explained.
- Use consistent capitalization. Capitalization can tell you a lot about your variables, functions, etc. These rules are subjective, so can choose whatever makes sense, but the point is, no matter what is choosen, be consistent.
- Prefer
async/await
over Promises and over callbacks. Avoidtry/catch
blocks as well. - camelCase variables and function names wherever possible. In the case of constants, you may choose to upper snake case, e.g
const A_LONG_CONSTANT_VARIABLE = 'foo';
Variables
- Use meaningful and pronounceable variable names.
- Use the same vocabulary for the same type of variable.
- Use searchable names. You will read more code than you write so it's important that the code written is readable and searchable. By not naming variables that end up being meaningful for understanding our program, we hurt our readers. Make your names searchable.
- Use explanatory variables.
- Avoid mental mapping. Explicit is better than implicit.
- Don't add unneeded context. If your class or object name tells you something, don't repeat that in your variable name.
- Follow the DOMs API by prefixing getters with
get
and setters withset
.
Functions
- Limiting the amount of function parameters is incredibly important because it makes testing your function easier. Having more than three leads to a combinatorial explosion where you have to test tons of different cases with each separate argument.
- Use destructuring syntax to make it obvious what properties the function expects. This clones the specified primitive values of the argument object passed into the function and helps prevent side effects. Note: objects and arrays that are destructured from the argument object are NOT cloned. Linters can warn you about unused properties, which would be impossible without destructuring.
- This is by far the most important rule in software engineering. When functions do more than one thing, they are harder to compose, test, and reason about. When you can isolate a function to just one action, it can be refactored easily and your code will read much cleaner. If you take nothing else away from this guide other than this, you'll be ahead of many developers.
- Naming things can be hard, but the best advice is to name things for what they do or return
- Functions should only be one level of abstraction. When you have more than one level of abstraction your function is usually doing too much. Splitting up functions leads to reusability and easier testing.
Classes
Prefer ES2015/ES6 classes over ES5 plain functions
It's very difficult to get readable class inheritance, construction, and method definitions for classical ES5 classes. If you need inheritance (and be aware that you might not), then prefer ES2015/ES6 classes. However, prefer small functions over classes until you find yourself needing larger and more complex objects.
Use method chaining
This pattern is very useful in JavaScript and you see it in many libraries such as jQuery and Lodash. It allows your code to be expressive, and less verbose. For that reason, use method chaining and take a look at how clean your code will be. In your class functions, simply return this at the end of every function, and you can chain further class methods onto it.
Error Handling
Thrown errors are a good thing! They mean the runtime has successfully identified when something in your program has gone wrong and it's letting you know by stopping function execution on the current stack, killing the process (in Node), and notifying you in the console with a stack trace.
Do not ignore caught errors. Doing nothing with a caught error doesn't give you the ability to ever fix or react to said error. Logging the error to the console (console.log) isn't much better as often times it can get lost in a sea of things printed to the console. If you wrap any bit of code in a try/catch it means you think an error may occur there and therefore you should have a plan, or create a code path, for when it occurs.
For the same reason you shouldn't ignore caught errors from try/catch
.
Comments
Code is written and maintained by people. Ensure your code is descriptive, well commented, and approachable by others. Great code comments convey context or purpose. Do not simply reiterate a component or class name.
Be sure to write in complete sentences for larger comments and succinct phrases for general notes. And avoid journal style comments in favor of better commit messages and git log
for history.
Comments are not meant to replace version control, so don't comment out code or mark to do later; remove it and create a commit in history.
Organization
- We tend to read code from top-to-bottom, like a newspaper. Because of this, make your code read that way. e.g. if a function calls another, keep those functions vertically close in the source file. Ideally, keep the caller right above the callee.
- Organize sections of code by component.
- Develop a consistent commenting hierarchy.
- Use consistent white space to your advantage when separating sections of code for scanning larger documents.