Skip to content

Naming Conventions

Lukas Obermann edited this page Sep 10, 2019 · 1 revision

Functions

Functions are generally named camel case. If there are overlapping functions names, e.g. when there are two accessor functions accessing an attribute of the same name, but different type, a prefix separated by an underscore is allowed.

const { wikiEntry, effect } = Advantage.A // Attribute accessors for record Advantage

/**
 * `getAdvEffect :: Advantage -> String`
 */
export const getAdvEffect = pipe (wikiEntry, effect)

...

const { wikiEntry: spell_wikiEntry } = Spell.A

/**
 * `getSpellEffect :: Spell -> String`
 */
export const getSpellEffect = pipe (spell_wikiEntry, effect)

Accessors

  • Functions that return an attribute of a value or a part of it have the name of the attribute.
    Examples: id, value, description
  • Functions that access some kind of state or reference are named getX, where X is the name of the part being accessed.
    Examples: getCurrentHeroId, getSpellEffect

Modifiers

  • Functions returning a value with an attribute set to a new value are named setX, where X is the name of the attribute or part being set. Usually, this type of function does not contain any computation or deep state access.
    Examples: setId, setTotalAP
  • Functions that set some kind of state or reference are named putX, where X is the name of the part being accessed.
    Examples: putCurrentHeroId, putHeroEntry
  • Functions returning a container X modified by a function of type a -> a have the name updateX.
    Example: updateTotalAP :: (Int -> Int) -> Hero -> Hero
  • Functions modifying a container X by a function of type a -> a have the name modifyX.
    Example: modifyHero :: (Hero -> Hero) -> Hero -> Hero, although a special function might not make sense in that specific case ... imagine one or two additional params.

Predicates

  • Predicates testing a property X have the name isX.

Conversions

  • Functions converting a value of type X to a value of type Y have the name xToY with all leading uppercase characters of X converted to lower case.
    Example: maybeToList
  • Overloaded functions have the name toX or fromX, depending on if they convert to or from a specific type, respectively.
    Examples: toInt, fromJSON

Suffixes

  • g_: A variant of g with only tiny changes. Haskell: g'
  • gF: Flipped version (first two arguments) of g.
  • gS: Safe version of g.
  • gL: Lens version of g.
    Example: Accessor value and corresponding lens valueL, if both are present. The lens would need to be renamed manually though, as its default name would be value, too.

Miscellaneous

  • Functions and actions which are potentially dangerous to use and leave some kind of proof obligation to the programmer have the name unsafeX.
    Example: unsafeIndex :: Int -> [a] -> a: Get the element at the specified index in the list. Throws if list not that long.

Variables

Variables can be named camel case or snake case, but, to prevent shadowing functions, snake case is prefered.

const mwiki_entry = lookup ("SPELL_12") (spells) // Wrapped in Maybe

const ewiki_entry = maybeToEither ("Not found.") (mwiki_entry) // Wrapped in Either

... some logic ...

const wiki_entry = fromRight (ewiki_entry) // Unwrapped value

Prefixes

  • mx: a Maybe of x.
  • ex: an Either of x.
Clone this wiki locally