How to write Good Automated Tests

Writing good tests requires skill and consistency to effectively catch bugs and errors before they make their way into production. Writing tests is one thing, but writing good tests is another. Good…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Opinionated JavaScript

As you write more and more code, you start realizing that there is more than one way to solve the problem. While everyone is looking for the “silver bullet” that will solve all the problems, there is no perfect way to code everything.

That said, it is healthy to form opinions on how to write code. Being “opinionated” is not a bad thing. In fact, it is very healthy to form opinions. That said, it is also just as important to understand why you have those opinions.

This post will highlight some of the stronger opinions I have developed for how I write JavaScript now and why I have those opinions.

A common check we look for in JavaScript is if we have a value or not. A clever trick that is often taken advantage of in JavaScript is that null and undefined are considered “falsy,” which lets us write code like this:

As simple as this seems now, it has a few issues. First of all, the 0 and empty strings are also falsy values, which means the above code will run for situations other than just undefined or null.

Beyond that, the longer you separate yourself from when you wrote this code, the harder this code is to read. The above code isn’t clear if myVariable is a boolean or an object or what.

You can avoid both these problems if you explicitly check for the value you want:

On a side note, if your variable potentially could be undefined, I highly recommend that you attach maybe to the front of the variable name to show that it might be undefined. For example, maybeUser would…

Add a comment

Related posts:

What If We Were Serious about Addressing the Climate?

Let us imagine threat, see climate solution, transform money, ration environment, build morale, cycles of regeneration, repairability, rethink packaging…

Get Rid of Stubborn Arm Fat With This Amazing Exercises at Home

Eliminating stubborn body fat can be a challenge, especially when you target specific parts of your body. Arms are usually regarded as a problem area, which is why many people want to get rid of arm…

Manage media and content distribution across multiple sites and apps

In our previous blog we talked about how Drupal 8 can be leveraged to solve the business challenges of the media vertical. We discussed about the challenges faced by the media vertical of managing…