Difference between revisions of "Naming Conventions"

From MiniScript Wiki
Jump to navigation Jump to search
m
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
The MiniScript [[Intrinsic Functions|intrinsic functions]] and [[Mini Micro]] API have established the following conventions with regard to naming variables.
+
The MiniScript [[:Category:Intrinsic Functions|intrinsic functions]] and [[Mini Micro]] API have established the following conventions with regard to naming variables.
  
 
== Capitalization ==
 
== Capitalization ==
Line 5: Line 5:
 
* method names use camelCase, starting with lower-case, e.g.: <c>[[print]]</c>, <c>calcTotal</c>
 
* method names use camelCase, starting with lower-case, e.g.: <c>[[print]]</c>, <c>calcTotal</c>
 
* variables follow the same convention: <c>x</c>, <c>totalIncome</c>
 
* variables follow the same convention: <c>x</c>, <c>totalIncome</c>
 +
 +
UPDATE: Recent discussion has led to also capitalizing "static" class members, i.e., methods and properties which are intended to be called directly on a class rather than on some instance of the class.  For example, <c>Monster.Instances</c> might be a list on the <c>Monster</c> class that keeps track of all the instances of that class.  This convention is not yet consistently used, but it is recommended and catching on.
  
 
== Nouns vs. Verbs ==
 
== Nouns vs. Verbs ==
Line 11: Line 13:
  
 
Object references should be descriptive nouns, e.g. <c>player</c>, <c>annualIncome</c>.
 
Object references should be descriptive nouns, e.g. <c>player</c>, <c>annualIncome</c>.
 +
 +
== Avoid Starting With an Underscore ==
 +
 +
Per the MiniScript manual, in most cases you should avoid starting any global variables or function names with an underscore. Identifiers starting with an underscore are often used by the host environment for special “under the hood” code, and name collisions there could cause problems.
  
 
== Short Names ==
 
== 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:
+
Very short variable names should be used only for fleeting purposes, such as loop counters in small loops, or parameter names in small functions.  They are also often used when experimenting in a [[REPL]].  The following names are generally understood:
  
 
* <c>i</c>, <c>j</c>: loop counters (usually numbers)
 
* <c>i</c>, <c>j</c>: loop counters (usually numbers)
Line 25: Line 31:
 
* <c>s</c>: a string, or any sequence
 
* <c>s</c>: a string, or any sequence
 
* <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</c> 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]]
 
[[Category:Conventions]]

Latest revision as of 23:26, 15 February 2025

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

UPDATE: Recent discussion has led to also capitalizing "static" class members, i.e., methods and properties which are intended to be called directly on a class rather than on some instance of the class. For example, Monster.Instances might be a list on the Monster class that keeps track of all the instances of that class. This convention is not yet consistently used, but it is recommended and catching on.

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.

Avoid Starting With an Underscore

Per the MiniScript manual, in most cases you should avoid starting any global variables or function names with an underscore. Identifiers starting with an underscore are often used by the host environment for special “under the hood” code, and name collisions there could cause problems.

Short Names

Very short variable names should be used only for fleeting purposes, such as loop counters in small loops, or 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 is short for "array"; and note that l looks very similar to 1, and so should probably be avoided)