Removing Useless Expressions - JavaScript Best Practices

John Au-Yeung - Jan 26 '21 - - Dev Community

Check out my books on Amazon at https://www.amazon.com/John-Au-Yeung/e/B08FT5NT62

Subscribe to my email list now at http://jauyeung.net/subscribe/

To make code easy to read and maintain, we should follow some best practices.

In this article, we'll look at some best practices we should follow to make everyone's lives easier.

No Constant Expressions in Conditions

We shouldn't have constant expressions in conditions.

They either always run or never run if they're present.

For instance, don't write:

if (false) {
   foo();
}
Enter fullscreen mode Exit fullscreen mode

Instead, write:

if (x === 1) {
   foo();
}
Enter fullscreen mode Exit fullscreen mode

No Control Characters in Regex

We shouldn't have control characters in a regex.

They are rarely used and it's probably a mistake to check for them.

Instead of writing:

const pattern = /\x1f/;
Enter fullscreen mode Exit fullscreen mode

or:

const pattern = new RegExp("\x1f");
Enter fullscreen mode Exit fullscreen mode

We write:

const pattern1 = /\x20/;
const pattern2 = new RegExp("\x20");
Enter fullscreen mode Exit fullscreen mode

instead.

Don't Use debugger

debugger sets a breakpoint in our JavaScript code so we can inspect the variables.

This shouldn't be in our production code.

So instead of writing:

const isTruthy = (x) => {
  debugger;
  return Boolean(x);
}
Enter fullscreen mode Exit fullscreen mode

We write:

const isTruthy = (x) => {
  return Boolean(x);
}
Enter fullscreen mode Exit fullscreen mode

No Duplicate Arguments in Function Definitions

Duplicate argument names isn't valid syntax.

It'll throw an error if strict mode is on.

Instead of writing:

function foo(a, b, a) {
  console.log(a);
}
Enter fullscreen mode Exit fullscreen mode

We write:

function foo(a, b) {
  console.log(a);
}
Enter fullscreen mode Exit fullscreen mode

No Duplicate if-else-if Conditions

We shouldn't have duplicate conditions in our if statements.

The duplicate is useless and causes confusion.

So instead of writing:

if (isSomething(x)) {
  foo();
} else if (isSomething(x)) {
  bar();
}
Enter fullscreen mode Exit fullscreen mode

We write:

if (isSomething(x)) {
  foo();
}
Enter fullscreen mode Exit fullscreen mode

No Duplicate Keys in Object Literals

Duplicate keys are confusing and useless.

So we shouldn't write:

const foo = {
   bar: "baz",
   bar: "qux"
};
Enter fullscreen mode Exit fullscreen mode

Instead, we write:

const foo = {
   bar: "qux"
};
Enter fullscreen mode Exit fullscreen mode

No Duplicate case Label

switch statements shouldn't have more than one case label.

As with other duplicates, they cause confusion and are redundant.

So instead of writing:

switch (a) {
  case 1:
    break;
  case 2:
    break;
  case 1:
    break;
  default:
    break;
}
Enter fullscreen mode Exit fullscreen mode

We write:

switch (a) {
  case 1:
    break;
  case 2:
    break;
  default:
    break;
}
Enter fullscreen mode Exit fullscreen mode

No Empty Block Statements

Empty blocks are useless. So we should remove them.

Instead of writing:

if (foo) {
}
Enter fullscreen mode Exit fullscreen mode

or

while (foo) {
}
Enter fullscreen mode Exit fullscreen mode

or

switch(foo) {
}
Enter fullscreen mode Exit fullscreen mode

We write:

if (foo) {
  bar();
}
Enter fullscreen mode Exit fullscreen mode

or

while (foo) {
  bar();
}
Enter fullscreen mode Exit fullscreen mode

or:

switch(foo) {
  case 1:
    //...
    break;
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

We shouldn't have duplicates of most things.

Also, we should remove control characters from regexes and debugger.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .