Difference between revisions of "Naming Conventions"

From MiniScript Wiki
Jump to navigation Jump to search
(Created page with "The MiniScript intrinsic functions and Mini Micro API have established the following conventions with regard to naming variables. == Capitalizatio...")
 
m
Line 26: Line 26:
 
* <c>m</c>, <c>d</c>: a map (<c>d</c> is short for "dictionary")
 
* <c>m</c>, <c>d</c>: a map (<c>d</c> is short for "dictionary")
 
* <c>l</c>, <c>a</c>: a list (<c>a</a> is short for "array"; and note that <c>l</c> looks very similar to <c>1</c>, and so should probably be avoided)
 
* <c>l</c>, <c>a</c>: a list (<c>a</a> is short for "array"; and note that <c>l</c> looks very similar to <c>1</c>, and so should probably be avoided)
 +
 +
[[Category:Conventions]]

Revision as of 19:15, 21 February 2020

The MiniScript intrinsic functions and Mini Micro API have established the following conventions with regard to naming variables.

Capitalization

  • classes (maps intended to be used with new) use captialized CamelCase: Monster, RigidBody, InventoryItem
  • method names use camelCase, starting with lower-case, e.g.: print, calcTotal
  • variables follow the same convention: x, totalIncome

Nouns vs. Verbs

In general, methods that perform some operation should be active verbs, e.g. print, wait, applyDiscount. Clear abbreviations are acceptable when a name would otherwise be unwieldy, e.g. calcTotal instead of calculateTotal. Methods that merely return an attribute (whether stored or calculated) should be named for what they return, e.g. abs, average. (The line between these two cases can be fuzzy; favor a simple attribute name if the calculation is quick and has no side effects, but an active verb if the calculation may take some time or has any effects beyond returning the answer.)

Object references should be descriptive nouns, e.g. player, annualIncome.

Short Names

Very short variable names should be used only for fleeting purposes, such as loop counters in small loops, parameter names in small functions. They are also often used when experimenting in a REPL. The following names are generally understood:

  • i, j: loop counters (usually numbers)
  • c: character (e.g. while looping over a string)
  • x, y: 2D coordinates
  • dx, dy: 2D offset
  • vx, vy: 2D velocity
  • t: time
  • dt: time step or offset
  • s: a string, or any sequence
  • m, d: a map (d is short for "dictionary")
  • l, a: a list (a</a> is short for "array"; and note that <c>l looks very similar to 1, and so should probably be avoided)