javascript, linting, and ESLint

Andrew Nease
4 min readMay 3, 2017

--

Don’t call the world dirty because you forgot to clean your glasses.

Aaron Hill

Linting

At Flatiron School, a resounding mantra while developing code, an echoed sentiment of Kent Black, is “Make it work. Make it right. Make it fast.“ Within this context, linting could be considered an element of making it right.

In 1979, a program called Lint was widely used to check the source code of C programs for potential errors. Lint would warn you if you had divisions by zero, out-of-range values, or other system-crashing, fatal flaws in your code.

Lint was a lifesaver, and quite literally a lifesaver as well, which is why a conversation about linting should inevitably fall back upon the size of NASA’s code standards and the importance of consistency and reliability within your code. NASA rightfully takes coding standards more seriously than the rest of us.

(For further learning on how a rocket scientist conceptualizes these validation concerns, consult a presentation by Gerald Holzmann, senior research scientist at NASA’s Jet Propulsion Laboratory.)

In 2017, the definition of linting has become more generalized. Linting is the process of checking your code for not only system-crashing errors, but also stylistic inconsistencies as well.

Consistency is contrary to nature, contrary to life. The only completely consistent people are dead.

Aldous Huxley

It’s hard to find a positive quotation about consistency, but it does have its perks.

The field of cognitive neuroscience has long explored the relationship between pattern recognition and efficiency. The general idea is that the more often you see a thing’s pattern, the faster you can recognize what it is, as well as its components.

Development teams will often adopt a common set of stylistic standards for this reason. If two people share the same style, it will be much easier for them to read, understand, and modify each other’s code.

Never offend people with style when you can offend them with substance.

Sam Brown — Washington Post

ESLint

Choosing a linting tool for your own projects is a matter of personal preference. Each tool will have weaknesses and strengths. For this post, I’ll be writing about ESLint, an open-source javascript linter created in 2013.

ESLint has some core philosophies it abides by, such as:

  • All rules can be turned off or on (nothing can be deemed “too important to turn off”)
  • Rules are “agenda free” — ESLint does not promote any particular coding style
  • ESLint values documentation and clear communication

Like many linting tools, ESLint can be plugged directly into your preferred editor by installing a package. For example, in Atom:

Next, put an .eslintrc file in the root directory of your project. Where do you get the content for the file? Look around the internet for a template to use, write one from scratch, or ask a friend.

I was fairly intimidated the first time I saw an .eslintrc file, but it turns out to be rather simple once you’ve seen it a few times. Pattern recognition and efficiency, indeed.

The nice thing about standards is that there are so many of them to choose from.

Andrew S. Tanenbaum

The purpose of the .eslintrc file is to define the rules by which you want to code by. For example: "quotes": [2, "single", "avoid-escape"] will set some rules related to quotation marks. Because ESLintrespects documentation and clear communication, each rule is carefully explained at eslint.org/docs/rules/. For example:

Beyond stylistic preferences, ESLint can also be used to identify problems in your code before it is executed. Errors I’ve commonly found in my code include:

  • Missing semi-colons
  • Unused variables
  • Unexpected tokens

By installing the package in Atom, I’m able to see the errors in my code immediately:

Be regular and orderly in your life like a bourgeois, so that you may be violent and original in your work.
Gustave Flaubert

Whatever your coding style is, a linter can improve your aesthetic awareness and help you to teach yourself to adhere to a consistent set of rules — whichever ones you elect to follow.

Before:

After:

Originally published January 9, 2017

--

--