>_ DevTrendsen

Language

Home

Languages

Sections

Frontend Backend Mobile DevOps AI / ML GameDev Security
JavaScript

Clean Code JavaScript - How to Write Code You're Not Embarrassed to Show Colleagues

94,452 stars

Do you know that feeling when you open old code and can't understand what you meant? Or when a colleague asks for help with their script, and you spend hours trying to decipher it? The Clean Code JavaScript repository is a collection of practical rules that will help make your code understandable, maintainable, and professional.

Why does clean code matter so much?

Over 15 years of working with JavaScript, I've seen hundreds of projects. And the main problem with most of them isn't algorithmic complexity, but code unreadability. We spend 10 times more time reading code than writing it. Clean Code JavaScript is an adaptation of Robert Martin's famous book "Clean Code" for the JavaScript world.

As the repository author Ryan McDermott says:

"Every piece of code starts as a rough draft, like wet clay taking its final form. We carve out imperfections when we review it with colleagues."

5 key principles from the repository

1. Meaningful variable names

The first step to clean code is proper naming. They should be:

  • Pronounceable
  • Searchable (so you can find them via Ctrl+F)
  • Not requiring mental translation

Bad:

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

Good:

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

2. Functions should do one thing

This error appears in 90% of projects. A function is not a novel, but a short poem.

Bad:

function emailClients(clients) {
  clients.forEach(client => {
    const record = database.lookup(client);
    if (record.isActive()) email(client);
  });
}

Good:

function emailActiveClients(clients) {
  clients.filter(isActiveClient).forEach(email);
}

function isActiveClient(client) {
  const record = database.lookup(client);
  return record.isActive();
}

3. Avoid side effects

A function should not modify anything outside its area of responsibility. This is especially important when working with arrays and objects.

Bad:

const addItemToCart = (cart, item) => {
  cart.push({ item, date: Date.now() });
};

Good:

const addItemToCart = (cart, item) => {
  return [...cart, { item, date: Date.now() }];
};

4. SOLID for JavaScript

SOLID principles work not only in Java and C#. Here's what Dependency Inversion looks like in JS:

Bad:

class InventoryTracker {
  constructor(items) {
    this.items = items;
    this.requester = new InventoryRequester();
  }
}

Good:

class InventoryTracker {
  constructor(items, requester) {
    this.items = items;
    this.requester = requester;
  }
}

5. Testability

Clean code is testable code. Each function should have a single responsibility and predictable result.

Bad:

describe('MomentJS', () => {
  it('handles all cases', () => {
    // 20 строк теста разных сценариев
  });
});

Good:

describe('MomentJS', () => {
  it('handles 30-day months', () => {
    // один четкий сценарий
  });
  
  it('handles leap year', () => {
    // другой сценарий
  });
});

Who will benefit from this repository?

  • Junior developers: You'll understand the difference between working code and good code
  • Team leads: You can use these principles for code review
  • Instructors: You'll get excellent examples for JavaScript courses
  • Freelancers: Clients value clean and maintainable code

Personal implementation experience

In my practice, there was a project with 50 files of 500+ lines of code each. After implementing Clean Code principles:

  • Time to add new features decreased by 40%
  • Number of production bugs dropped by 3 times
  • New developers onboarded 2 times faster

How to start using?

  1. Bookmark the repository
  2. Choose 2-3 rules that you violate most often
  3. Start applying them in new code
  4. Gradually refactor old sections

As the author says: "These rules are not dogma, but a guide. Our profession is young, and we're still learning."

Links:

Related projects