Naming things
2023-05-12
The names you give to classes, functions, and variables while programming is vital to a smooth experience, especially when collaborating with others.
Common practices differ among languages, but some general principles to consider when naming things in your code are as follows:
- Reveal your intent
- If you have to read the code to understand a name, you have failed to reveal your intent
- Parts of speech
- While exceptions can be made, following these rules generally results in easy to read code
- Class -> Noun
- Variable -> Noun
- Methods
- Returns boolean -> Predicate (e.g.
isEven
) - Returns non-boolean -> Verb
- Returns boolean -> Predicate (e.g.
- Enum -> Often Adjectives
- The Scope Length Rule
- Variables
- The longer the scope of the variable, the longer the name of the variable
- Variables in short scope should have short names
- Classes/Functions
- The longer the scope, the shorter the name
- The shorter the scope, the longer the name
- Variables
Note that in Clojure, functions that return booleans are usually named with
a question mark at the end like even?