Clean Code: The Art of Naming Things

Choosing good names is like writing a good headline; it gives you the gist of the story and makes you want to read more. In programming, a good name can make the difference between code that's a puzzle to be solved and code that's a story to be read. Let's look at how we can improve our naming.

Variables

Variable names should be nouns that clearly describe the information they hold. Avoid single-letter names or abbreviations that might be cryptic to others (or to your future self!).

// Bad
const d = new Date(); // 'd' for... date? day?
const t = 1000; // 't' for... time? timeout?
const userList = ['John', 'Jane']; // It's a list, but what kind of list? An array, right?

// Good
const currentDate = new Date();
const timeoutInMilliseconds = 1000;
const users = ['John', 'Jane'];

Booleans

For boolean variables, it's a good practice to use prefixes like is, has, or can. This makes conditions read like plain English.

// Bad
const valid = true;
const write = true;

// Good
const isValid = true;
const canWrite = true;
const hasSufficientPermissions = true;

Functions

Function names should be verbs that describe what the function does. If a function returns a value, the name should ideally reflect that.

// Bad
function proc(data) { // 'proc' is too generic. What does it do?
  // ...
}

function i(user) { // 'i' is meaningless.
    // ...
}

// Good
function processUserData(data) {
  // ...
}

function isUserAdmin(user) {
    return user.isAdmin;
}

Classes

Classes are the blueprints for your objects, so their names should be nouns or noun phrases. Think of them as the title of a chapter in your application's story.

// Bad
class Manager { // Manages what? Too vague.
    // ...
}

class Data { // What kind of data?
    // ...
}

// Good
class UserManager {
    // ...
}

class DatabaseConnection {
    // ...
}

Naming is a skill that improves with practice. The goal is to write code that's so clear you barely need comments to explain it. So, take an extra moment to think about your names. It's a gift to your future self and anyone else who reads your code.