Skip to content
All essays
CraftMarch 27, 20258 min

Naming Conventions: Write Self-Documenting Code

Master naming conventions. Write code that reads like prose and requires fewer comments.

Ü
Ümit Uz
Mobile & Full Stack Developer

The Power of Good Names

Good names make code self-documenting. Bad names confuse and mislead. Naming is hard but essential for clean code.

typescript
// Bad
const d = new Date();  // What does d mean?

// Good
const currentDate = new Date();

Principles

Use Intention-Revealing Names

typescript
// Bad
const d = days;  // Time since when?
const t = time;  // What time?

// Good
const daysSinceCreation = days;
const elapsedTimeInSeconds = time;

Avoid Disinformation

typescript
// Bad - misleading
const userList = [];  // But it contains User objects
const userNames = [];  // Contains full user objects

// Good
const users = [];
const names = [];

Make Meaningful Distinctions

typescript
// Bad - noise words
const userData = {};
const userInfo = {};  // What's the difference?

// Good
const userRecord = {};
const userPreferences = {};

Naming Patterns

Variables

typescript
// Booleans - prefixes
const isActive = true;
const hasPermission = false;
const canEdit = true;
const shouldUpdate = true;

// Collections - plural
const users = [];
const userMap = new Map();
const userSet = new Set();

Functions

typescript
// Verbs for actions
function getUser(id: string) { }
function createUser(data: User) { }
function updateUser(id: string, data: User) { }
function deleteUser(id: string) { }

// Return booleans - is/has/can
function isValid() { }
function hasPermission(user: User) { }
function canEdit(user: User) { }

Classes

typescript
// Nouns
class UserService { }
class OrderProcessor { }
class EmailValidator { }

Constants

typescript
// UPPER_SNAKE_CASE
const MAX_RETRIES = 3;
const DEFAULT_TIMEOUT = 5000;
const API_BASE_URL = 'https://api.example.com';

Context Matters

Small Scope

typescript
// i, j fine for loops
for (let i = 0; i < 10; i++) {
  for (let j = 0; j < 10; j++) {
    // Short scope, short names OK
  }
}

Large Scope

typescript
// Full words for larger scope
let retryCount = 0;
let maxRetries = 3;

// Not
let r = 0;
let m = 3;

Domain Language

typescript
// Use business terminology
class ShoppingCart { }  // Not Cart or Basket
class OrderProcessor { }  // Not Processor
class PriceCalculator { }  // Not Calculator

Refactoring Poor Names

Step 1: Understand

typescript
// What does this do?
const p = u.permissions.includes('edit');

Step 2: Rename

typescript
const hasEditPermission = user.permissions.includes('edit');

Step 3: Extract

typescript
class User {
  canEdit() {
    return this.permissions.includes('edit');
  }
}

if (user.canEdit()) { }

Best Practices

  1. 1Use pronounceable names
  2. 2Avoid single-letter variables (except loops)
  3. 3Use consistent terminology
  4. 4Searchable names (constants vs literals)
  5. 5Avoid encoding types in names (Hungarian notation)

Conclusion

Good names reveal intent. Take time to choose names that tell a story. Code is read far more than written.

Write code that reads like prose!

Enjoyed this essay? Share it.

Related essays

Next essay
Monorepos: Managing Multiple Projects in One Repository