Skip to content
Clean CodeChapter 5 / 57Beginner6 min read

Naming Things

How naming conventions work in clean code. Names for functions, booleans, hooks, files and exports that explain their job without any comments.

On this page

1. What is this practice?

When code has no comments, all the explaining falls on the names. A good name is half the documentation.

ThingConventionExample
FunctionOne short verb, the module gives the contextinvoices.create, dispatch, record
Booleanis, has, can, should prefixisExpired, hasQuotaLeft
Production checkensure or require, assert only in testsensureOwner(caller, doc)
Custom hookA named state object, not a tuple or JSX{ leads, isLoading, refresh }
Component filePascalCaseLeadTable.tsx
Hook or module filekebab-caseuse-lead-table.ts
Env variableSCREAMING_SNAKEDATABASE_URL
ExportNamed export, default only where the framework asksexport function price()

Do not mark something private with an underscore (_cache). Visibility comes from the language: do not export it from the module, or use private / #field in a class.

2. Why does it matter?

A bad name steals a little time from every reader. Across 100 files that adds up to hours, and a wrong name directly causes bugs.

Bad namesGood names
data, temp, obj, x everywhereinvoice, overdueInvoices, customer
You have to guess what a flag meansisArchived answers the question itself
A hook tuple, you must remember the orderNamed fields, no order problem
Default export, imported under a different name in every fileNamed export, one name everywhere, easy to search
PascalCase and kebab-case mixed in one folderThe import path is predictable instead of a guess

3. When to follow it

LevelHow importantWhy
Learning projectVery importantNaming is the cheapest habit with the biggest payoff
Production codeFollow the repo's convention fullyTwo styles in one repo make search and imports harder
Senior levelWrite conventions down and enforce them with lintA rule that is not written down becomes an argument in review

4. Bad vs good code

tsx
export default function useData(id: string) {
  const [d, setD] = useState<any>(null);
  const [l, setL] = useState(true);
  const _load = () => fetchLeadsForCampaignById(id).then(setD).finally(() => setL(false));

  useEffect(_load, [id]);

  return [d, l, _load] as const;
}

const [leads, loading] = useData(campaignId);
tsx
export function useLeads(campaignId: string) {
  const [leads, setLeads] = useState<Lead[]>([]);
  const [isLoading, setIsLoading] = useState(true);

  const refresh = useCallback(async () => {
    setLeads(await campaigns.leads(campaignId));
    setIsLoading(false);
  }, [campaignId]);

  useEffect(() => {
    refresh();
  }, [refresh]);

  return { leads, isLoading, refresh };
}

const { leads, isLoading } = useLeads(campaignId);
What is the difference?BadGood
Hook nameuseData, which data?useLeads is clear
State named, lleads, isLoading
Function namefetchLeadsForCampaignById, a whole sentencecampaigns.leads, the module gives the context
Private_loadNot exported at all, so no underscore needed
ReturnA tuple, a wrong order is a bugAn object, take values by name
ExportDefaultNamed

A one-word verb

The strict house style says a function name should be one short verb: create, cancel, record. The context comes from the module or class name, and invoices.cancel(id) reads like a sentence.

ts
export const invoices = {
  create,
  cancel,
  send,
};

5. How to apply it

Before choosing a name
What is this thing called in the domain?
If it is a function, what does it do or return?
If it is a boolean, add is, has, can or should
Does the module name already give the context? Then keep the name short
Does the repo already have a pattern for this kind of thing? Follow that

6. Exceptions and common mistakes

Loop counters i, j and coordinates x, y are fine, everyone knows them. Well-known domain abbreviations (id, url, api, html) are fine too.

React's own useState gives a tuple, and that is the framework's decision. The named object rule is for your custom hooks.

An honest note: "a one-word verb" is a strong opinion. Many teams prefer longer names like fetchUserProfile or calculateInvoiceTotal, especially when functions are imported from different files and the module context is not visible. The trade-off: a short name is good when the call site has context like invoices.create. Without context, a bare create is vague. Whichever you choose, keep it the same across the whole repo.

Mistake: Generic names like data, info, handle, manager

Why? These words fit everything, so they say nothing.

Better: Use the real domain word, like invoice, overdueInvoices or sendReminder.

Mistake: A boolean name that does not read like a question

Why? Looking at status or flag, you cannot tell what true means.

Better: A name like isActive, hasPaid or canEdit that is a yes or no question.

Mistake: Naming a production check assertX

Why? assert is a testing word, and the reader assumes it runs only in tests.

Better: Name production checks ensureX or requireX.

Mistake: Returning JSX from a hook

Why? A hook's job is to give state and logic, and returning JSX traps the UI and the logic in one place.

Better: The hook gives named state, and the component builds the JSX from it.

7. What to learn before and after

Final quick revision

CheckQuestion
DomainIs the name the real business word?
FunctionDoes the name say what it does or returns?
BooleanDoes it start with is, has, can or should?
HookDoes it return a named object, not a tuple or JSX?
PrivateWas it hidden by not exporting, instead of an underscore?
FilesIs there one naming style in the folder?
ExportIs it a named export?