Course progress0%
Course content

Module 1

Module 2

Module 3

Module 4

Module 5

Module 6

Module 7

Module 8

Module 9

Module 10

Module 11

Module 12

Module 13

Module 14

Module 15

Module 16

Module 17

Module 18

25 min

Names That Explain Decisions

Choose names that communicate role and units

By the end of this lesson

  • Choose names that communicate role and units
  • Use context instead of mechanical naming rules

What does the number mean?

const x = 500 tells a reviewer little. Is it a quantity, a price or a duration? Naming is part of making the decision understandable.

const unitPriceCents = 500;
const quantity = 3;
const subtotalCents = unitPriceCents * quantity;
const hasFreeDelivery = subtotalCents >= 10000;
console.assert(subtotalCents === 1500);
console.assert(hasFreeDelivery === false);

Units in a name prevent confusing cents with whole currency units. Boolean names should read like a question or statement: hasFreeDelivery, isEligible, canCollect. Functions usually name an action or question; arrays usually use plural names.

Guided review

Names such as data2 or calc conceal purpose. But short names are not automatically bad: a conventional index i in a very small loop may be clear. Choose enough context to disambiguate the value, not the longest possible name.

Do not name a subtotal totalWithTax if tax has not been added. Misleading precision is worse than a name that honestly describes the current value.

Independent exercise

Rename amount, result and flag in a routine that computes a discount from a subtotal and checks eligibility. State the units and whether result means the discount amount or the discounted subtotal.

Correction

Possible names include subtotalCents, discountCents and isEligibleForDiscount. If the result is after the deduction, discountedSubtotalCents is more accurate. There is no single required spelling; another reader should infer the value's role correctly.

Review

Read your function aloud using its names. If you must repeatedly explain "this is actually...", revise the vocabulary or the model. Names should reflect confirmed business concepts rather than obscure them behind technical abbreviations.

Lesson complete?

Your progress is saved on this device.