When you first look at this code, what do you think?
const x = fetch('/getData', { id: d });
Questions flood your mind:
Whatβs
x
? An array? An object?Is
d
a user ID, a timestamp, or something else?Why
getData
instead ofuser-profiles
?
And indeed. What did the author want to write?
In a series of publications about naming in JavaScript, I will try to propose what I believe to be the correct approach to naming language entities.
Bad naming costs time. A study found developers spend 60% of their time reading code. Poor names make this even harder.
In this guide, weβll focus on naming conventions for:
Classes & Components (
UserService
vsuserService
)Events (
onClick
vshandleClick
)API Endpoints (
/orders
vs/getOrders
)Tests
(describe('User')
vsdescribe('Test User'))
// π Clear
class PaymentGateway {
processTransaction() { ... }
}
// π Confusing
class pg {
pt() { ... } // "pt" = ?
}
This isnβt just about style. Itβs about:
Onboarding (new devs understand your code faster).
Maintainability (youβll thank yourself later).
Communication (names should "document" the code).
In a series of publications, we will talk about:
- "Naming Conventions in JavaScript (Classes, Components, Events, APIs)"
- "JavaScript Variable Naming: Rules You Canβt Ignore"
- "How to Name Functions in JavaScript: A Practical Guide"
Ready to name like a pro? Letβs dive in!
π 1. Class Names (PascalCase)
Classes should be nouns representing entities (objects, services, models) and always start with a capital letter and use PascalCase:
// π Good
class User { ... }
class PaymentService { ... }
class HttpClient { ... }
// π Bad
class user { ... } // Lowercase
class makePayment { ... } // Verb (a class is not a function)
π 2. Specificity instead of general terms in classes
Avoid names like Manager
, Helper
, Processor
:
/* π Good */
class UserRepository {} // Clear: works with user data
class EmailValidator {} // Specific goal
/* π Bad */
class DataManager {} // What exactly is "managing"?
class ApiHelper {} // Too vague
π 3. Components (React/Vue/Svelte)
Component names use PascalCase, like classes:
// π Good
function UserProfile() { ... }
class CartItem extends React.Component { ... }
// π Bad
function userProfile() { ... } // camelCase for components is confusing
π 4. Events (Event Naming)
Handlers:
on{Event}
(e.g.,onClick
).Custom events:
kebab-case
(e.g.,user-registered
).
// π Good
button.addEventListener('click', onClick);
emitter.emit('user-registered', data);
// π Bad
button.addEventListener('click', handleClick); // Non-standard prefix
emitter.emit('userRegistered', data); // camelCase for events is non-standard
π 5. API Endpoints (kebab-case or snake_case)
// π Good
fetch('/api/user-profiles/123');
fetch('/api/orders?status=completed');
// π Bad
fetch('/api/getUserProfile/123'); // Redundant verbs
fetch('/api/userProfiles'); // camelCase in URLs
π 6. Tests (describe/it Style)
Test names should describe what is being tested:
// π Good
describe('UserService', () => {
it('should create a new user', () => { ... });
});
// π Bad
describe('Test User', () => { // Vague
it('test #1', () => { ... }); // Meaningless
});
π 7. Imports (Readability & Context)
Module names should reflect their purpose:
// π Good
import UserService from './services/UserService'; // Service
import { formatDate } from './utils/date'; // Utility
// π Bad
import Service from './Service'; // Too generic
import foo from './helpers/foo'; // Unclear
π 8. Configuration Files (env, config)
// π Good (clear prefixes)
const DB_HOST = 'localhost';
const MAX_UPLOAD_SIZE_MB = 10;
// π Bad
const host = 'localhost'; // No context
const maxSize = 10; // Units unclear
π 9. Types (TypeScript)
Interfaces:
PascalCase
+I
(if project convention).Union types: Descriptive names.
// π Good
interface IUser { ... }
type PaymentStatus = 'pending' | 'completed';
// π Bad
interface user { ... }
type status = 'pending' | 'completed';
π 10. Async Operations (Prefixes: fetch
, load
, sync
)
// π Good
async function fetchPosts() { ... }
const isLoading = false;
// π Bad
async function getData() { ... } // Unclear if HTTP request
const loading = false; // Could be component state
Key Takeaways: Where Else Is Naming Important?
State management (Redux, Pinia):
userSlice
,useCartStore
.Error classes:
ValidationError
,NetworkError
.Project files:
user.controller.js
,date.utils.js
.Git commits:
feat: add user login
,fix: button click handler
.
π° Golden Rule:
A name should reflect what it is and where itβs used.
Read more:
"JavaScript Variable Naming: Rules You Canβt Ignore" (second part)
"How to Name Functions in JavaScript: A Practical Guide" (third part)
Top comments (0)