Categories
JavaScript Best Practices

JavaScript Best Practices— Useless Catch, Concatenation, Escape, Return and More

Spread the love

JavaScript is a very forgiving language. It’s easy to write code that runs but has mistakes in it.

In this article, we’ll look at more useless code that we should remove, including useless catch clauses, concatenation, escape characters, useless comments, void operator and return statements.

No Useless catch Clauses

catch clauses that are useless just add extra bloat to our code and don’t do anything useful.

Therefore, we shouldn’t include them in our code. An example includes a catch clause that just throws the exception again:

try {
  foo();
} catch (e) {
  throw e;
}

Instead, we should do something in our catch clause to make it actually useful.

For instance, we may run something to handle the error before rethrowing the error as follows:

try {
  foo();
} catch (e) {
  handleError(e);
  throw e;
}

This way, the catch block actually does something rather than just rethrowing the error, which is redundant because we don’t need to wrap our code with try...catch just to rethrow an error.

The error will be thrown with or without it.

No Unnecessary Concatenation of Strings

It’s useless to concatenate 2 strings together as we do in the following code:

const foo = 'x' + 'y';

In the code above, both operands are strings. We only concatenate if we concatenate expressions like variables or anything else that aren’t constant strings.

We can simplify the example above to:

const foo = 'xy';

It’s much shorter and we don’t have a useless operator in between the 2 strings.

If we have some expression in between the strings or a string with an expression, then they’re also good use of the concatenation operator.

For instance, if we have:

const a = 1;
const foo = 'x' + a;

or:

const a = 1;
const foo = 'x' + a + 'y';

Then they’re good uses of the concatenation operator since we have a variable a within the chain of concatenations.

No Useless Escape Usage

Useless escape characters should be removed from our code.

Examples of this include strings or regex that only have escape characters in them like:

"'";
/!/;

Instead, we should either remove them or make them useful by including other characters in them:

"'foo";
/!foo/;

No Redundant return Statements

In a JavaScript function, a return; statement with nothing after it is useless.

For instance, if we have the following function:

const foo = () => {
  console.log('foo');
  return;
}

Then the return; statement is useless since the function would end with or without the return statement.

Instead, we should either remove it or make it do something like:

const foo = () => {
  return 'foo';
}

Don’t Use the void Operator

The void operator always return undefined . Therefore, it’s pretty useless in most cases in our JavaScript code.

In ES5, undefined values are mutable and the void operator lets us make sure that we get a real undefined value in our code. However, this use case is obsolete now.

Therefore, we shouldn’t use the void operator.

For instance, the following code is bad:

const foo = void 1;

We should just use undefined if we want to use it as follows:

const foo = undefined;

Remove Warning Comments

Warning comments shouldn’t be in production code. It indicates that the code is incomplete or has issues.

Therefore, the comments should be removed and the code should be fixed before going to production.

Examples of warning comments include:

// TODO: fix this
// FIXME: bad code

The comments above shouldn’t be in our code and the issues in the comments should be addressed.

Conclusion

Useless catch clauses are ones that just rethrow the error. We can do that without wrapping our code in try...catch , so it’s just useless code.

We should either remove the try...catch or put some code that handles the error in the catch block.

Redundant return statements are ones that don’t do anything at the end of the function.

They should either return something or be removed.

The void operator always returns undefined , so we should just use undefined and remove the void operator.

Finally, warning comments like todo and fix-me comments should be removed and the issues in the comments should be addressed.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *