Back to Rules
Web Development

Lua Best Practices: Do's and Don'ts for Clean, Performant Code in Game Dev & Scripting

Claude Directory November 29, 2025
0 copies 0 downloads

Unlock expert Lua coding standards with this rewritten guide using Do/Don't format. Optimize performance, ensure security, and write modular code for games, embedded systems, and more.

Rule Content
## Naming Conventions
**Do:** Adopt snake_case for variables and functions, PascalCase for modules, UPPERCASE for constants, and prefix private items with underscore. Use descriptive names.
Example:
```lua
local user_name = "player1"  -- Good
local MAX_LIVES = 3          -- Good
local _private_helper = function() end  -- Good
```
**Don't:** Mix cases or use vague names like 'x' or 'tmp'.
Example:
```lua
local UserName = "player1"  -- Bad, inconsistent
local x = 10                -- Bad, not descriptive
```

## Code Organization
**Do:** Group related functions into modules, use local functions for privacy, section code with comments, and limit file size. Rely on require() for dependencies.
Example:
```lua
-- player_module.lua
local Player = {}
function Player.new(name)
  local self = {name = name}
  return self
end
return Player
```
**Don't:** Pollute global namespace or cram everything into one file.
Example:
```lua
-- Bad: globals everywhere
globalPlayer = {}  -- Avoid
```

## Error Handling
**Do:** Wrap risky calls in pcall/xpcall, use assert() for checks, handle nils explicitly, and log errors.
Example:
```lua
local ok, result = pcall(function() return riskyFunc() end)
if not ok then
  print("Error: " .. tostring(result))
end
assert(type(input) == "string", "Input must be string")
```
**Don't:** Ignore errors or use bare calls that crash.
Example:
```lua
riskyFunc()  -- Bad, unhandled
```

## Performance Optimization
**Do:** Prefer locals, pre-allocate tables, use table.concat() for strings, avoid globals and loop table creation.
Example:
```lua
local t = {} for i=1,100 do t[i] = i end  -- Pre-allocate
local str = table.concat(parts, " ")     -- Efficient concat
```
**Don't:** Rely on globals or inefficient string ops.
Example:
```lua
str = str .. part  -- Bad in loops
```

## Memory Management
**Do:** Clean up resources, use weak tables, avoid cycles, clear unused refs, monitor in long apps.
Example:
```lua
local weak = setmetatable({}, {__mode = "v"})  -- Weak values
obj.ref = nil  -- Clear ref
```
**Don't:** Create circular refs or leak memory.
Example:
```lua
a.obj = b; b.obj = a  -- Bad cycle
```

## Testing & Documentation
**Do:** Test units, edges, integrations; comment params/returns, explain logic, add examples.
Example:
```lua
-- function add(a: number, b: number): number
-- Adds two numbers.
-- @param a First number
-- @param b Second number
-- @return Sum
function add(a, b) return a + b end
```
**Don't:** Skip tests or leave undocumented code.
Example:
```lua
function add(a,b) return a+b end  -- Bad, no docs
```

## Security & Lua-Specific
**Do:** Validate inputs, avoid loadstring, use metatables wisely, stick to 1-based indexing, leverage tables.
Example:
```lua
if type(input) ~= "string" then error("Invalid input") end
local mt = {__index = default}
```
**Don't:** Trust user input or misuse globals/metatables.
Example:
```lua
loadstring(user_code)()  -- Dangerous
```

## Game Dev Patterns
**Do:** Structure game loops, efficient collisions, state management, optimized renders/inputs.
Example:
```lua
function love.update(dt)
  -- Update logic
end
```
**Don't:** Busy-wait loops or naive collision checks.

Comments

More Rules

View all
AI/ML

GLM-4.7 Optimized Config & System Prompt Designer

Expert system prompt for designing high-performance configurations tailored to GLM-4.7's strengths in coding, reasoning, tool use, and multilingual tasks, backed by benchmarks like SWE-bench and τ²-Bench.

C
Community
AI/ML

GLM-4.7 Open-Source Coding Expert: Optimized System Prompt

Leverage GLM-4.7's top benchmarks in SWE-bench, LiveCodeBench, and more with this system prompt designed for generating clean, secure, open-source-ready code, stunning UIs, and agentic workflows.

C
Community
AI/ML

GLM-4.7 Optimized Coding Agent

This system prompt transforms an AI into GLM-4.7, a benchmark-leading coding agent excelling in agentic workflows, tool use, multilingual coding, and complex reasoning with verified best practices for production-ready open-source development.

C
Community
DevOps

Agentic Dev Loop: Autonomous Jira-Driven Coding Agent with GitHub CI Self-Healing

Ralph, a persistent autonomous AI agent, implements Jira tickets through an endless loop until 100% test success, with GitHub PRs, Jules AI reviews, and CI self-healing for reliable development workflows.

C
Claude Directory
AI/ML

Türk Hukuku Uzmanı AI Agent: Güvenilir Yasal Danışman System Prompt

Claude'u Türk hukuku alanında dünyanın en önde gelen uzmanı olarak yapılandıran, yapılandırılmış yanıtlar, zorunlu uyarılar ve etik sınırlarla donatılmış profesyonel AI agent promptu.

C
Community
Database

PostgreSQL Best Practices: Expert Subagent Guide

Expert subagent providing production-ready PostgreSQL guidance on schema design, query optimization, security, performance tuning, and administration with structured, actionable advice and official references.

C
Claude Directory