Shared: Naming
Summarizes naming conventions.
Class names
- Class names are written in UpperCamelCase.
- Use the singular form of a noun.
- Reason: if a plural noun is used, you cannot define a variable name for "an array holding instances of that class."
This is because there is no way to express "the plural of instances of a plural-named class."
- When no specific class name is dictated, name the class to describe the logic it encapsulates.
// NG
class Users {}
const users = [new Users(), new Users()] // Cannot express "plural of instances of a plural class"
// OK
class User {}
const users = [new User(), new User()]
Name variables and properties holding arrays as plural nouns
- A variable or property that holds an array (a value with multiple elements) is always named with "the plural form of the noun denoting its element."
- This is the counterpart of the "class names are singular" rule (see "Class names"). If the element's class is the singular
User, its array is the pluralusers. - Do not express plurality with a
Listsuffix (seelistin references/prohibited-words.md).
// NG: named singular despite holding an array / expressing plurality with a List suffix
const user = users.filter(it => it.enabled) // holds an array yet named singular
const paymentList = payments.filter(it => it.completed)
// OK: plural form of the noun denoting the element
const enabledUsers = users.filter(it => it.enabled)
const completedPayments = payments.filter(it => it.completed)
A variable holding constructors starts with a capital
- A variable or property that holds an array of constructors starts with a capital, as a class name does (
MiddlewareCtors): its elements are classes, andCtoris the word for a class held as a value. - A variable that holds a plain object starts lowercase, however it was imported. A capital initial tells the reader that the value is made of classes.
import MiddlewareCtors from './middleware-ctors.js' // an array of classes
import middlewareHash from './middleware-hash.js' // a plain object
The parameters of a higher-order function's callback
- How a callback's parameters are named —
it, and the accumulator ofreduce()— belongs to
/hoc-higher-order-functions.
Naming abstract classes and derived classes
- Prefix abstract classes with
Base~. - When extending
BaseSamplefor a specific application, addAppas inBaseAppSample. - The
Base~andApp~inBaseAppSampleare called "super prefixes." - When naming a derived class, remove the super prefix (
Base~/App~), keep the suffix, and replace it with a distinguishing prefix.
// Abstract class
class BaseSample {}
// Abstract class extended for the application
class BaseAppSample extends BaseSample {}
// Derived classes (super prefix removed and replaced with a distinguishing prefix; suffix "Sample" is kept)
class AlphaSample extends BaseSample {}
class BetaSample extends BaseAppSample {}
An interface name carries no distinguishing prefix
- Where a module accepts a set from its caller and also holds a set of its own, the name on the side the caller sees is the plain one (
MiddlewareCtors), and the distinguishing word goes to the module's own side (BundledMiddlewareCtors). - A word such as
extraon the caller's side leaks the module's internal split into every call site. The module is the one that knows it holds two sets, so the module is the one that names the difference.
Method names are "predicates," not "complete sentences"
- A method call
receiver.method()has a structure resembling a sentence: "subject (receiver) + predicate (method)." - Therefore, a method name should be named as a predicate with the receiver as the subject, not as a "complete sentence with both subject and predicate."
// NG: named as a complete sentence (object.isStatusValid() reads as "object is status valid?", which is unnatural)
isStatusValid () {
return this.status !== null
}
// OK: named as a predicate (object.isValidStatus() reads as "object is valid status")
isValidStatus () {
return this.status !== null
}
Property syntax and "messages to the receiver"
- In the property syntax
object.alpha, each token is called:object= receiver,.= dot operator,alpha= property. - A method is a property in the broad sense. It is a property whose value is a function, invoked with
();object.methodNameis property access, andobject.methodName()is the notation for executing that value "as a function." - Hence a method call is a message where "receiver = subject, method name = predicate," and follows a sentence-like structure such as
object.doSomething()(object does something) /object.isValid()(object is valid). #isStatusValid()is wrong because it treats the method name as "a complete sentence with subject and predicate combined (Is status valid?)." Calling it via a receiver reads as "object is status valid?", which is unnatural. Name it with only the predicate (#isValidStatus()→ "object is valid status").
Method names start with a verb
- Since a method name is a predicate, it is natural, from an English grammar standpoint, for it to "start with a verb" (e.g.
#hasEntity()/#findUserProfile()). - It may also start with an auxiliary verb (e.g.
#canFindEntity()/#shouldHaveFulfilledInput()).
Third-person singular verb → convention that the return value is boolean
- Starting a verb in the third-person singular form (or with an auxiliary verb) carries the convention that "the return value is boolean" (
is...()/has...()/can...()/should...(), etc.). - This originates from Java (Sun Microsystems) coding conventions from the late 1990s, which spread to later languages.
object.isValid() // boolean
object.containsInvalidTag() // boolean (third-person singular contains → boolean)
- This originates from Java conventions established by Sun Microsystems in the late 1990s (methods returning boolean start with a third-person singular verb or an auxiliary verb:
is...()/has...()/can...()/should...(), etc.), which spread as a standard to later languages. - Conversely, if a predicate is named correctly, the return type can be inferred even without JSDoc (e.g.
object.lovesWithoutConditions()→ readable as boolean because it's a third-person singular verb).
Criteria for using abbreviations
- The criteria and whitelist (shared between JS and CSS) for which abbreviations may be used are collected in references/abbreviations.md.
Do not use non-ASCII characters in names
- Do not use non-ASCII characters in variable names, class names, member names, etc.
// Never use
class 😂 {
取得 () {
return null
}
}
Avoid single-word class names wherever possible
- Avoid single-word class names wherever possible; use a compound name containing two or more words.
- If this is pointed out in a review, always change it to a compound word to save discussion time.
Spell out the full role
- A name states the role in full: what the thing handles and what it does to it. Do not shorten it by dropping words.
- A qualifier may be dropped only where no second thing could ever need it to be told apart.
PdfRenderingInvoiceAdaptermay becomeInvoiceAdapterwhere only one kind of invoice adapter can exist. - The same holds for method and function names.
Name a value for what it is, not for how it was made
- A variable is named for its role.
wrappedNextsays how the value came about, which is a description of the technique;receivedNextordefinedNextsay what the value is. - This is the rule behind the prohibition of
wrapper(see references/prohibited-words.md), applied to a variable: a name that explains the mechanism tells the reader nothing about the part the value plays.
Use American spelling
- The correspondence between American and British spellings is collected in references/spelling.md.
Prohibited words and redundant compound words
- Words that must not be used in naming (as prefixes/suffixes) and the redundant compound words that contain them are collected in references/prohibited-words.md.
Property names
- Property names are, in principle, nouns. They are acceptable if they are clear.
- Array properties should be pluralized (
#paymentscorrect /#paymentListincorrect). See "Name variables and properties holding arrays as plural nouns."
Datetime suffixes: `At` for an instant, `On` for a date
- A value that carries a time of day ends with
At(modifiedAt,trashedAt,expiredAt). - A value whose meaning stops at the calendar date ends with
On(billedOn,dueOn). - A range keeps the suffix and appends
From/To(modifiedAtFrom/modifiedAtTo,billedOnFrom/billedOnTo). - Reason: the suffix is the only place the granularity is stated. Dropping it (
modifiedFrom) forces every reader to open the column definition to learn whether a time component exists, and a date-only value silently compared against an instant is off by up to a day — a bug that only appears near midnight. From/Tomark the two ends of a range. A single value meaning "in effect from this moment" is not a range end; name it for the instant it holds (effectiveAt), noteffectiveFrom.
// NG
const modifiedFrom = ... // does the value carry a time of day? the name does not say
const billed = ... // a date or an instant?
const effectiveFrom = ... // a single instant named as if it were a range end
// OK
const modifiedAt = ...
const billedOn = ...
const effectiveAt = ...
const modifiedAtFrom = ... // range lower bound, suffix kept
const modifiedAtTo = ... // range upper bound
Method names
- Method/function names should basically start with the base (present tense) form of a verb (e.g.
findUsers()/deleteUsers()). - Starting with a particle (such as a preposition) is allowed, but should be avoided when used alone wherever possible (
fromOpenedAt()correct /from()incorrect). - Exception: inflator methods adopt preposition-like short names as their canonical vocabulary (
.from()/.of()/.by(), etc.). See the inflator-methods convention for details. - A single-word name for an instance method is basically prohibited (
#load()doesn't reveal what is being loaded; use#loadCardNumbers()). - For static methods, since the class name supplements the context, a single-word name is exceptionally allowed (
CardNumbersLoader.load()). - For a method name that serves as an entry point, interpret it as a transitive verb wherever possible and give it an object (e.g. for
ChunkBuilder, use#buildChunks()instead of#build()). - Reason: if the instance is held in a short-scoped variable or property, the context supplementation from the class name is lost. If held as in
const builder = ChunkBuilder.create(), thenbuilder.build()doesn't reveal what is being built. Including the object, as inbuilder.buildChunks(), conveys it to the reader. - The verb is the verb of the class's role. A class named for what it does speaks that verb at its entry point: a
Builderbuilds (#buildChunks()), aComposercomposes (#composeMiddleware()). An entry point that does not begin with the class's own verb leaves the reader wondering what else the class does.create~is not available for it: that verb is for instantiating (see references/verbs.md).
Super-prefixes state how a method behaves
- A method that handles many items at once, realizes a recursion, or calls a delegate
carries a super-prefix before its verb. Which prefixes there are, and what each one promises,
belong to /hoc-super-prefix-method-name.
- What it is read for here is that the prefix changes nothing behind it. A prefixed name is
still chosen by the rules on this page, so a method's name is settled here first and the
prefix is put in front of it afterwards.
Accessors (getter / setter)
- Getter names should in principle be nouns.
- Naming a getter with a "boolean method name" that starts with a third-person-singular verb or an auxiliary verb is also permitted.
- Since setters are prohibited, there is no naming rule for them (see the accessor-definition convention).
Method name verbs (choosing among highly abstract verbs)
- How to choose among highly abstract verbs (creation
create/generate/build, retrievalfind/fetch/extract, updatesave/send/set) is collected in references/verbs.md.
Contrasting terms
- The fixed pairs to use for variable/member names with opposite meanings are collected in references/antonyms.md.