πŸ’…πŸ»If you're beautiful, follow this JS Code Style

Pulkit Singh - Jan 1 '23 - - Dev Community

Image description

Summary πŸ“‘

Soft tabs with two spaces

Eslint: indent

  • Wrong:
if (true) {
    console.log('True')
}
Enter fullscreen mode Exit fullscreen mode
  • Correct:
if (true) {
  console.log('True')
}
Enter fullscreen mode Exit fullscreen mode

Never use semicolons

Eslint: semi

  • Wrong:
if (true) {
  console.log('True');
}
Enter fullscreen mode Exit fullscreen mode
  • Correct:
if (true) {
  console.log('True')
}
Enter fullscreen mode Exit fullscreen mode

Always use single quotes

Eslint: quotes

  • Wrong:
if (true) {
  console.log("True")
}
Enter fullscreen mode Exit fullscreen mode
  • Correct:
if (true) {
  console.log('True')
}
Enter fullscreen mode Exit fullscreen mode

Keep else in the same line of closure of the if

Eslint: brace-style

  • Wrong:
if (true) {
  console.log('True')
}
else {
  console.log('False')
}
Enter fullscreen mode Exit fullscreen mode
  • Correct:
if (true) {
  console.log('True')
} else {
  console.log('False')
}
Enter fullscreen mode Exit fullscreen mode

Add spaces between operators

Eslint: space-infix-ops

  • Wrong:
for (i=0;i<10;i++) {
  ...
}
Enter fullscreen mode Exit fullscreen mode
  • Correct:
for (i = 0; i < 10; i++) {
  ...
}
Enter fullscreen mode Exit fullscreen mode

Avoid single letter names

Be descriptive with your naming. Eslint: id-length

  • Wrong:
function q() {
  ...
}
Enter fullscreen mode Exit fullscreen mode
  • Correct:
function query() {
  ...
}
Enter fullscreen mode Exit fullscreen mode

Use lowerCamelCase

Eslint: camelcase

  • Wrong:
let is_error
Enter fullscreen mode Exit fullscreen mode
  • Correct:
let isError
Enter fullscreen mode Exit fullscreen mode

Use the === operator

Eslint: eqeqeq

  • Wrong:
const example = 'Is a example'

if (example == 15) {
  ...
}
Enter fullscreen mode Exit fullscreen mode
  • Correct:
const example = 'Is a example'

if (example === 15) {
  ...
}
Enter fullscreen mode Exit fullscreen mode

Add spaces outside parentheses () but avoid it inside

Eslint: keyword-spacing

  • Wrong:
if(condition){
  ...
}
Enter fullscreen mode Exit fullscreen mode
  • Correct:
if (condition) {
  ...
}
Enter fullscreen mode Exit fullscreen mode

Use function expressions instead of function declarations

Eslint: func-style

  • Wrong:
function foo() {
}
Enter fullscreen mode Exit fullscreen mode
  • Correct:
const foo = function () { }
Enter fullscreen mode Exit fullscreen mode

When you must use function expressions (as when passing an anonymous function), use arrow function notation

Eslint: prefer-arrow-callback

  • Wrong:
[1, 2, 3].map(function (x) {
  const y = x + 1
  return x * y
})
Enter fullscreen mode Exit fullscreen mode
  • Correct:
[1, 2, 3].map((x) => {
  const y = x + 1
  return x * y
})
Enter fullscreen mode Exit fullscreen mode

Ternary operator single line only

The ternary operator should be used on a single line.

  • Wrong:
const foo = (condition)
  ? 1
  : 2
Enter fullscreen mode Exit fullscreen mode
  • Correct:
const foo = (condition) ? 1 : 2
Enter fullscreen mode Exit fullscreen mode

Don't be dumb with ternary operator

Disallow ternary operators when simpler alternatives exist. Eslint: no-unneeded-ternary

  • Wrong:
const isYes = answer === 1 ? true : false;
Enter fullscreen mode Exit fullscreen mode
  • Correct:
const isYes = answer === 1;
Enter fullscreen mode Exit fullscreen mode

Use const for all of your references, avoid using var

Eslint: prefer-const

  • Wrong:
var foo = 'bar'
Enter fullscreen mode Exit fullscreen mode
  • Correct:
const foo = 'bar'
Enter fullscreen mode Exit fullscreen mode

If you must reassign references, use let instead of var

Eslint: no-var

  • Wrong:
var count = 1

if (condition) {
  count += 1
}
Enter fullscreen mode Exit fullscreen mode
  • Correct:
let count = 'bar'

if (condition) {
  count += 1
}
Enter fullscreen mode Exit fullscreen mode

Declare one const or let per declaration statement

Eslint: one-var

  • Wrong:
const foo = require('./bar'),
      foo = require('./foo')
Enter fullscreen mode Exit fullscreen mode
  • Correct:
const foo = require('./bar')
const foo = require('./foo')
Enter fullscreen mode Exit fullscreen mode

Use the literal syntax for object creation

Eslint: no-new-object

  • Wrong:
const foo = new Object()
Enter fullscreen mode Exit fullscreen mode
  • Correct:
const foo = {}
Enter fullscreen mode Exit fullscreen mode

Use the literal syntax for array creation

Eslint: no-array-constructuor

  • Wrong:
const foo = new Array()
Enter fullscreen mode Exit fullscreen mode
  • Correct:
const foo = []
Enter fullscreen mode Exit fullscreen mode

Declare array items in list

>= 4 arguments: indent. Eslint: array-element-newline and array-bracket-newline

  • Wrong:
const foo = [
  'hello', 'world'
]
Enter fullscreen mode Exit fullscreen mode
const foo = ['hello', 'world', 'lore', 'impsum']
Enter fullscreen mode Exit fullscreen mode
  • Correct:
const foo = ['hello', 'world']
Enter fullscreen mode Exit fullscreen mode
const foo = [
  'hello',
  'word',
  'lore', 
  'impsum'
]
Enter fullscreen mode Exit fullscreen mode

Function arguments indentation

>= 4 arguments: indent (excerpt callbacks brackets). Eslint: function-paren-newline

  • Wrong:
const foo = (
  'lore', 
  'impsum'
) => {}
Enter fullscreen mode Exit fullscreen mode
const foo = ('carls', 'farls', 'karen', 'logan') => {}
Enter fullscreen mode Exit fullscreen mode
foo.bar(
  'string',
  () => {
    statements
  }
)
Enter fullscreen mode Exit fullscreen mode
  • Correct:
const foo = ('lore', 'impsum') => {}
Enter fullscreen mode Exit fullscreen mode
const foo = (
  'carls', 
  'farls', 
  'karen',
  'logan'
) => {}
Enter fullscreen mode Exit fullscreen mode
foo.bar('string', () => {
  statements
})
Enter fullscreen mode Exit fullscreen mode

Method chaining

Eslint: newline-per-chained-call

  • Wrong:
user
.findOne({ name: 'foo' })
.populate('bar')
.exec(function(err, user) {
  return true
})
Enter fullscreen mode Exit fullscreen mode
user.findOne({ name: 'foo' })
  .populate('bar')
  .exec(function(err, user) {
    return true
  })
Enter fullscreen mode Exit fullscreen mode
  • Correct:
user
  .findOne({ name: 'foo' })
  .populate('bar')
  .exec(function(err, user) {
    return true
  })
Enter fullscreen mode Exit fullscreen mode

Any non-trivial conditions must be assigned to a variable or function with a descriptive name

  • Wrong:
if (password.length >= 4 && /^(?=.*\d).{4,}$/.test(password)) {
  ...
}
Enter fullscreen mode Exit fullscreen mode
  • Correct:
const isValidPassword = password.length >= 4 && /^(?=.*\d).{4,}$/.test(password)

if (isValidPassword) {
  ...
}
Enter fullscreen mode Exit fullscreen mode

Try to write comments that explain higher level mechanisms or clarify difficult segments of your code

  • Wrong:
const foo = "var(--bar)"

// Regexp
if (foo.replace(/var\(/, "").replace(/\)/, "") === "--bar") {
  ...
}
Enter fullscreen mode Exit fullscreen mode
  • Correct:
let foo = 'var(--bar)'

let value = foo
            .replace(/var\(/, '') // Remove the 'var('
            .replace(/\)/, '') // Remove the ')'

if (foo === '--bar') {
  ...
}
Enter fullscreen mode Exit fullscreen mode

Don't use comments to trivial things

  • Wrong:

// Create the passwors
const password = 'carls1234'
Enter fullscreen mode Exit fullscreen mode
  • Correct:
const password = 'carls1234'
Enter fullscreen mode Exit fullscreen mode

References

Project inspired by valle-style-guide

. . . . . . . . . . . . . . . .
Terabox Video Player