DEV Community

15 Rules For Writing Clean JavaScript

Mohammad Faisal on May 09, 2023

To read more articles like this, visit my blog So you are a React developer or Node.js developer. You can write code that works. But can you write...
Collapse
 
axel_nieminen_072275fab50 profile image
niemax

Great article! From my perspective the usage of if statements on one line is very clear if the condition and the function have declarative names. I often omit the braces if I'm sure it's easy to read. :)

Collapse
 
dvalin99 profile image
Domenico Tenace

Nice article!
I have one more point, in my opinion, that would help to write clean code in JavaScript:

Use a maximum of two input parameters for functions.

For me, this is a good helper to write clean code and visually helps to understand the code better.
What do you think?

Collapse
 
mohammadfaisal profile image
Mohammad Faisal

Totally agree. Although I would go upto 3 parameters.

Collapse
 
itsahsanmangal profile image
Ahsan Mangal πŸ‘¨πŸ»β€πŸ’»

This rticle is well-organized and straightforward, presenting 15 distinct rules, each with its own "bad" and "good" examples.

Collapse
 
himanshudevgupta profile image
Himanshu Gupta

Such a good article @mohammadfaisal thank you for sharing

Collapse
 
mohammadfaisal profile image
Mohammad Faisal

Thank you!

Collapse
 
matborowiak profile image
Mat Borowiak

What do you think about one-liner guards inside functions? These also stack nicely. Eg:

const myFunction = (name) => {
   if (shouldStop) return;
   if (alsoShouldStop) return;

   const changedName = changeName(name);
   return changedName;
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jackmellis profile image
Jack

Very few of these rules come with any actual explanation.
Some of them do have an important purpose (consistent variable casing means a developer from any language/codebase can quickly identify classes/variables/constants, for example), but lots of these rules are purely personal preference.
I think you'd find a lot of these difficult to justify other than "that's how I do it", "it said so in a book", or "because prettier"

Collapse
 
mohammadfaisal profile image
Mohammad Faisal

Maybe. But is there any universal styleguide for javascript? I guess not.
So every style guideline will ultimately fall into the "personal pereferance" bucket

Collapse
 
jackmellis profile image
Jack

There are a few "standard" rulesets for eslint, and there's prettier, but ultimately there are a lot of personal preferences - which are absolutely fine to have. However, there's a big difference between personal stylistic preferences, and practices that are intended to reduce bugs/errors.

Collapse
 
balazssoltesz profile image
Balazs Soltesz

Or in one sentence: use eslint

Collapse
 
mohammadfaisal profile image
Mohammad Faisal

Eslint is a tool that helps to apply the knowledge with ease.

Eslint is not the knowledge.

Collapse
 
edmundasramanauskas profile image
Edmundas Ramanauskas • Edited

sorry if this sounds offensive (I'm not trying to be) but neither is your post.
you wrote 15 rules but didn't explain why they're good.
also, I would like to know you're reasoning regarding points 11 and 14.
I've never seen 11 in action unless you have 2 loops in a row and want to reuse variable.
regarding 14, I've seen people using both and never had any problem myself with either. and personally I prefer using single quotes because if you need to put html or json inside the string you don't need to escape it.
point 15 is just comparing apples to potatoes. it's just a mistake and no linter will help you here. so it shouldn't be in the list because you're just saying don't make this particular mistake and it has nothing to do with coding style or rules. unless you made a mistake yourself and actually meant variable === undefined instead in which case it warrants an explanation.

Thread Thread
 
mohammadfaisal profile image
Mohammad Faisal

I understand your point. These rules are taken from the book β€œMaintainable Javascript” written by β€œNicholas C. Zakas”.

So obviously we will not agree on all the points. Because style is subjective.

Collapse
 
poljeff profile image
Pol Jeff

I disagree with rules 11 and 14