NAMING
How does a name in the code state what it names, in every language?
Binds: code, code::swift, code::python
This page states the naming rules that Swift, Python and TypeScript share.
PYTHON.md and SWIFT.md state the case of a name in each language.
PYTHON_TESTING.md and SWIFT_TESTING.md state the form of a test name.
A name states its intent
Chapter 2 of Clean Code by Robert C. Martin sets these rules, and section 3.16 of the Google Python Style Guide agrees with them, at https://google.github.io/styleguide/pyguide.html#s3.16-naming:
- A name states its intent. A reader needs no comment to know what it holds.
- A name carries no abbreviation that a reader outside this repository must guess. A name deletes no letters inside a word.
- A name carries no encoding of its type, such as
id_to_name_dict. - One concept takes one word across the codebase.
- A longer name that leaves no doubt wins over a short one.
A name states what a thing is, or the interface that it offers
A name names no caller, no use and no role. A new caller then renames nothing. A package that holds an MCP server is a server, whichever client starts it. A word for an activity of one caller fails this rule, and so does a word for a role.
A language page can define a suffix for the role of a type in its layer. Such a
role is part of what the type is. SWIFT_LAYERS.md defines the suffixes of
Swift.
Each name passes or fails on its own
A reviewer reads one name at a time. So each rule below names a form that one name can fail.
-
A function name says what the function does. A noun phrase names a function that hands back a thing that already exists, as it is or after a trivial conversion. A function that works its answer out names that work with a verb, such as
relay_review_output. A bare participle or adjective names no work.relayed,refused,stagedandfreshfail. -
A function that guards against a fault names the fault that it detects.
detect_several_changespasses. A name of the verdict or of the message that the function hands back fails, such asrefusal_reason. -
A name does not repeat the module or the type that holds it. The qualified name reads as one phrase, such as
pre_push_gate.detect_several_changes.push.detect_several_changes_in_pushfails. -
A function that loops until an event names each event that ends the loop. The reader of
relay_review_output_until_exitknows that the call blocks until the review exits.relay_agy_events_until_result_or_closenames both of its ends.relay_review_outputfails for a function that loops. -
A context manager names the state that holds inside its
with. It reads afterwith, so a participle fits there:with stop_signal_handling_deferred():. A context manager that yields a resource names that resource, such asunlinked_temporary_file. -
A name shows what a call costs. A function that reaches the network, a model provider or a long-running process says so with its verb, such as
fetch_orrun_. A localgitread is no such cost. A function that reads local state alone carries no such verb, and a loop can call it.fetch_git_remotepasses, and so doesfind_newest_ancestor_on_git_remote. Afind_name that also fetches fails. -
A name states its domain where the module does not. In a review tool, a remote can be a reviewer or a model provider.
git_remotepasses, and a bareremotefails in a module that does not state Git. -
A constant names what it holds. A compiled pattern names what it matches, such as
HUNK_HEADER. A message names its text, such asBUSY_MESSAGE. A number names its unit or its limit, such asWAIT_SECONDSorREPLY_BYTE_LIMIT.HUNK,BUSYandWAITfail. -
A name of one word is a noun that names one kind of thing.
Receiptpasses. A one-word name that is a verb, a participle or an adjective fails, because it names no thing. -
A boolean name reads as a question that
trueorfalseanswers.branch_contains_baseandis_ancestorpass.baseandcheckfail. The rule names a value or a check with no effect. A function that acts and answers whether the act happened takes the verb of the act, such asthreading.Lock.acquireof the Python standard library. -
A name that a docstring explains is too short. A reader of the call site sees the name, and not the docstring.
-
A type that keeps the method names of a standard type, because it stands in for that type, names that type in its class name. A class that a
dictcan be swapped with, and so keepsupdate, isAttemptsDict. A class whose name says no such type takes method names by the rules above.Attempt.updatewrote a file, so it becameAttempt.save_fields. -
A test double names what it does.
recording_stage_runnerrecords each call that it gets. A name withfake,mockorstubsays only that the thing stands in for another, and it hides a use beyond the test, such as a log capture.
A class name is a noun for one kind of thing
PEP 8, chapter 2 of Clean Code, the Swift API Design Guidelines and the .NET Framework Design Guidelines agree on these rules.
- A class name is a noun or a noun phrase, and never a verb.
ReceiptandAddressParserpass.Cutreads as a verb,Unreadas an adjective andEndedas a participle, so each fails. - A class name carries no empty word.
Manager,Processor,Data,Info,HelperandHandlersay nothing that the rest of the name does not. - A variant names its family. An error ends with
Error, such asPassDeadlineError. A kind of answer or of event ends with that word where the short name is unclear alone, such asCutAnswerorTerminalEvent. - A protocol names what a thing is, or what it can do. A protocol for what a
thing is reads as a noun, such as
Session. A protocol for a capability ends inable,ibleoring, such asEquatable. - A value class names the value, and not its storage.
Receiptpasses, andReceiptDatafails. A type that stands in for a standard type follows the rule about method names above.
A test name is a sentence
A test name states the behaviour that the test proves, as one sentence.