Lua is a lightweight, high-level, multi-paradigm programming language designed primarily for embedded use in applications. It is cross-platform, as the interpreter of compiled bytecode is written in ANSI C.
Lua Anti-Patterns Overview
Global Variables by Default
local
keyword when declaring variables. In Lua, variables are global by default, which can lead to unexpected behavior and name collisions.Not Using Modules
Using Tables Inefficiently
table.insert()
when you know the index in advance. This is more efficient, especially for large tables.String Concatenation in Loops
..
operator. Instead, collect strings in a table and use table.concat()
at the end, which is much more efficient.Not Using Proper Error Handling
nil
plus an error message on failure.Using Numeric For-Loops Incorrectly
Not Using Proper Scope
Inefficient Table Clearing
nil
to actually clear a table.Not Using Metatables Appropriately
Using Inefficient Patterns for OOP
Not Using Proper Closures
Not Using Proper Module Structure
Using Unnecessary Upvalues
Not Using Proper Error Propagation
Using Inefficient Table Traversal
pairs()
for traversing hash tables and ipairs()
for traversing array-like tables. Don’t use the length operator (#
) for non-sequential tables.Not Using Proper Documentation
Not Using Proper Testing
Using Inefficient Algorithms
Not Using Proper Resource Management
Not Using Proper Configuration Management