Variables¶
Naming¶
Name constants using ALL_CAPS¶
camelCase non-constant local variable names¶
PascalCase non-constant global variable names¶
Use underscore "_" as the name of a variable that cannot be deleted but is unused.¶
Enums Vs Booleans¶
Enums should be used to reflect the state of something when more than two options exist. A common anti-pattern is using multiple booleans to reflect the state. This is confusing and problematic, because the code then needs to defend against impossible states, as the combination fo booleans is able to represent more states than is desired. It also makes the code more opaque and harder to reason about. What does it mean if isWalking and isRunning are both false? That we don't know? Is the state is idle? Or maybe swimming?
local MOVEMENT = {
UNKNOWN = 1,
WALKING = 2,
RUNNING = 3
}
local movementState = MOVEMENT.UNKNOWN
Location¶
Local variables within a function should be declared as close as possible to the place where they are used. This limits what the developer must keep in their head while reading the code. Local variables declared outside of a function should be declared at the top of the file, grouped together. Global variables should be declared at the top of the file grouped together. client/server global variables should only be declared within a single client/server file. This helps keep things organized instead of spreading random globals around the resource.