Clean Code Javascript

· Code

Ryan Mcdermott:

Software engineering principles, from Robert C. Martin's book Clean Code, adapted for JavaScript. This is not a style guide. It's a guide to producing readable, reusable, and refactorable software in JavaScript.

Not every principle herein has to be strictly followed, and even fewer will be universally agreed upon. These are guidelines and nothing more, but they are ones codified over many years of collective experience by the authors of Clean Code.

Our craft of software engineering is just a bit over 50 years old, and we are still learning a lot. When software architecture is as old as architecture itself, maybe then we will have harder rules to follow. For now, let these guidelines serve as a touchstone by which to assess the quality of the JavaScript code that you and your team produce.

One more thing: knowing these won't immediately make you a better software developer, and working with them for many years doesn't mean you won't make mistakes. Every piece of code starts as a first draft, like wet clay getting shaped into its final form. Finally, we chisel away the imperfections when we review it with our peers. Don't beat yourself up for first drafts that need improvement. Beat up the code instead!

You need to go and read Ryan's manifesto on Clean Code for Javascript right now.

I've followed Clean Code practices for years, and it's nice to see people taking it and making use of it.

Here's an example:

Variables

Use meaningful and pronounceable variable names

Bad:

const yyyymmdstr = moment().format('YYYY/MM/DD');  

Good:

const currentDate = moment().format('YYYY/MM/DD');  

Use the same vocabulary for the same type of variable

Bad:

getUserInfo();  
getClientData();  
getCustomerRecord();  

Good:

getUser();  

You can get the rest from his post.

Source: Clean Code for Javascript

About the Author: Roger Stringer

It's all about finding that Work-Life Balance.

I spend most of my time solving problems for people, and otherwise occupying myself with being a dad, cooking, coding, speaking, learning, writing, reading, and the overall pursuit of life. I live in Penticton, BC.

Learn More
Roger Stringer
  • LinkedIn
  • Reddit
  • Google+
  • Pinterest
  • Pocket
Comments powered by Disqus