Clean Code: The Power of a Consistent Style

When you work on a team, the code you write isn't just for you. It's for everyone. A consistent coding style across the entire project means that everyone can read and understand the code more easily. It reduces cognitive load because you don't have to switch mental models as you move from file to file.

Consistency is about agreeing on a set of rules and sticking to them. It doesn't matter so much if you prefer tabs or spaces, as long as everyone on the team is doing the same thing.

Naming Conventions

This is a big one. Choose a convention for your variables and functions and stick with it. camelCase is the standard for variables and functions in JavaScript and TypeScript.

// Bad: Inconsistent naming makes the code harder to read.
const userName = 'John';
const user_age = 25;
const UserEmail = 'john@example.com';
const user_address = '123 Main St';

// Good: Consistent camelCase is easy on the eyes.
const userName = 'John';
const userAge = 25;
const userEmail = 'john@example.com';
const userAddress = '123 Main St';

// Bad: Inconsistent function declaration styles.
function getUserData() { /* ... */ }
const fetchUserData = () => { /* ... */ };
const processUserData = function() { /* ... */ };

// Good: Pick one style and use it everywhere.
const getUserData = () => { /* ... */ };
const fetchUserData = () => { /* ... */ };
const processUserData = () => { /* ... */ };

Formatting

Consistent formatting of things like spacing, indentation, and line breaks makes the code visually predictable and easier to scan.

// Bad: Inconsistent spacing and indentation.
function calculateTotal(items:Item[],tax:number):number{
  let total=0;
  for(let i=0;i<items.length;i++){
    total+=items[i].price;
  }
  return total*tax;
}

// Good: Consistent formatting makes the structure clear.
function calculateTotal(items: Item[], tax: number): number {
  let total = 0;
  
  for (let i = 0; i < items.length; i++) {
    total += items[i].price;
  }
  
  return total * tax;
}

// Bad: A dense, hard-to-read object.
const user = {name:'John',age:25,email:'john@example.com'};

// Good: Each property gets its own line.
const user = {
  name: 'John',
  age: 25,
  email: 'john@example.com'
};

Automate It!

The best way to ensure a consistent style is to automate it. Tools like Prettier and ESLint are essential for any modern web project.

  • Prettier is an opinionated code formatter. It takes your code and reprints it according to its own set of rules, ensuring that all the code in your project has the same format.
  • ESLint is a linter that can find problems in your code. It can also be configured to enforce stylistic rules.

By setting these tools up to run automatically (for example, on every commit), you take the human element out of the equation. No more debates in pull requests about where a curly brace should go!

A consistent style is a hallmark of a professional, well-maintained codebase. It's a simple thing that can have a big impact on your team's productivity and happiness.