Development2024-03-23

Hi Back-End Boys and Girls: A Code Review Guide for Readable JavaScript

Readable code is elegant code. After 15 years of reviewing PRs, here's my definitive checklist for writing JavaScript that your team will actually thank you for.

#javascript#code-review#best-practices#readability#clean-code

Hi back-end boys and girls πŸ‘‹

Today I want to talk about something that's close to my heart: code readability. Because honestly? I've seen too many PRs that work perfectly but read like a crime scene.

Let me share my code review checklist. These are the exact things I look for when reviewing JavaScript. If your code passes this checklist, it's elegant. If not... we need to talk.

1. Variable Names Tell a Story

// Bad Smell 🀒
const d = new Date();
const t = d.getTime();
const r = items.filter(i => i.s === 'active');

// Elegant ✨
const currentDate = new Date();
const currentTimestamp = currentDate.getTime();
const activeItems = items.filter(item => item.status === 'active');

If I need to scroll up to understand what r means, your variable name has failed.

2. Functions Do One Thing

// Bad Smell 🀒
function processOrder(order) {
  // Validates, calculates tax, charges card, sends email, updates inventory...
  // 200 lines of mixed concerns
}

// Elegant ✨
function processOrder(order) {
  validateOrder(order);
  const total = calculateTotal(order);
  await chargeCard(order.paymentMethod, total);
  await sendConfirmationEmail(order.customerEmail, order);
  await updateInventory(order.items);
}

Each function should do one thing and do it well. If your function needs a table of contents, it's too long.

3. No Magic Numbers

// Bad Smell 🀒
if (response.status === 200 && data.length > 0) {
  setTimeout(() => retry(), 3000);
}

// Elegant ✨
const HTTP_OK = 200;
const RETRY_DELAY_MS = 3000;

if (response.status === HTTP_OK && data.length > 0) {
  setTimeout(() => retry(), RETRY_DELAY_MS);
}

Every unexplained number in your code is a "magic number." Name it or explain it.

4. Early Returns Over Deep Nesting

// Bad Smell 🀒
function getUser(userId) {
  if (userId) {
    const user = db.find(userId);
    if (user) {
      if (user.isActive) {
        return user;
      }
    }
  }
  return null;
}

// Elegant ✨
function getUser(userId) {
  if (!userId) return null;

  const user = db.find(userId);
  if (!user) return null;
  if (!user.isActive) return null;

  return user;
}

Guard clauses. Early returns. No nesting pyramids.

5. Use a JSON Formatter Before Debugging

When you're reviewing API responses or config files, don't squint at minified JSON. Use a JSON Formatter to beautify it first. Then validate with a JSON Schema Generator to ensure the structure matches your expectations.

6. Consistent Formatting

Use a formatter like Prettier. Enforce it in CI. Never argue about tabs vs spaces in a code review again.

// Use a [JS Beautifier](/tools/js-beautifier/) to fix inconsistent formatting
// before your code review even starts

The Bottom Line

Readable code isn't about being clever. It's about being kind β€” to your future self, to your teammates, and to the next developer who inherits your codebase.

Write code that reads like well-organized prose. That's elegance.

Beautify your JavaScript instantly with our free JS Beautifier β€” consistent formatting, configurable indentation, and zero setup.

πŸ› 

Try It Yourself

Put what you've learned into practice with our free online tools.

Explore More Developer Tools

Discover more tools and tutorials in this category

Browse Category β†’