Formatting

Formatting is subjective. Like many rules herein, there is no hard and fast
rule that you must follow. The main point is DO NOT ARGUE over formatting.
There are tons of tools to automate this.
Use one! It’s a waste of time and money for engineers to argue over formatting.

For things that don’t fall under the purview of automatic formatting
(indentation, tabs vs. spaces, double vs. single quotes, etc.) look here
for some guidance.

Use consistent capitalization

JavaScript is untyped, so capitalization tells you a lot about your variables,
functions, etc. These rules are subjective, so your team can choose whatever
they want. The point is, no matter what you all choose, just be consistent.

Bad:

  1. const DAYS_IN_WEEK = 7;
  2. const daysInMonth = 30;
  3. const songs = ['Back In Black', 'Stairway to Heaven', 'Hey Jude'];
  4. const Artists = ['ACDC', 'Led Zeppelin', 'The Beatles'];
  5. function eraseDatabase() {}
  6. function restore_database() {}
  7. class animal {}
  8. class Alpaca {}

Good:

  1. const DAYS_IN_WEEK = 7;
  2. const DAYS_IN_MONTH = 30;
  3. const SONGS = ['Back In Black', 'Stairway to Heaven', 'Hey Jude'];
  4. const ARTISTS = ['ACDC', 'Led Zeppelin', 'The Beatles'];
  5. function eraseDatabase() {}
  6. function restoreDatabase() {}
  7. class Animal {}
  8. class Alpaca {}

Function callers and callees should be close

If a function calls another, keep those functions vertically close in the source
file. Ideally, keep the caller right above the callee. We tend to read code from
top-to-bottom, like a newspaper. Because of this, make your code read that way.

Bad:

  1. class PerformanceReview {
  2. constructor(employee) {
  3. this.employee = employee;
  4. }
  5. lookupPeers() {
  6. return db.lookup(this.employee, 'peers');
  7. }
  8. lookupManager() {
  9. return db.lookup(this.employee, 'manager');
  10. }
  11. getPeerReviews() {
  12. const peers = this.lookupPeers();
  13. // ...
  14. }
  15. perfReview() {
  16. this.getPeerReviews();
  17. this.getManagerReview();
  18. this.getSelfReview();
  19. }
  20. getManagerReview() {
  21. const manager = this.lookupManager();
  22. }
  23. getSelfReview() {
  24. // ...
  25. }
  26. }
  27. const review = new PerformanceReview(employee);
  28. review.perfReview();

Good:

  1. class PerformanceReview {
  2. constructor(employee) {
  3. this.employee = employee;
  4. }
  5. perfReview() {
  6. this.getPeerReviews();
  7. this.getManagerReview();
  8. this.getSelfReview();
  9. }
  10. getPeerReviews() {
  11. const peers = this.lookupPeers();
  12. // ...
  13. }
  14. lookupPeers() {
  15. return db.lookup(this.employee, 'peers');
  16. }
  17. getManagerReview() {
  18. const manager = this.lookupManager();
  19. }
  20. lookupManager() {
  21. return db.lookup(this.employee, 'manager');
  22. }
  23. getSelfReview() {
  24. // ...
  25. }
  26. }
  27. const review = new PerformanceReview(employee);
  28. review.perfReview();