Menu
Coddy logo textTech

Naming Conventions

Part of the Fundamentals section of Coddy's Lua journey — lesson 10 of 90.

Good naming conventions make your code easier to read, understand, and maintain.

In Lua, variable names must follow certain rules. They can contain letters, numbers, and underscores, but they must start with a letter or underscore. Names like playerScore, health_points, and _tempValue are all valid, while 2players or my-score are not.

When naming variables, choose descriptive names that clearly indicate what the variable stores. Instead of using vague names like x or data, use specific names like playerLevel or gameScore. This makes your code self-documenting and much easier to understand later.

For multi-word variable names, Lua programmers commonly use either camelCase (like playerHealth) or underscores (like player_health). Both styles are acceptable - the key is to be consistent throughout your code. Choose one style and stick with it in your projects.

Cheat sheet

Variable names in Lua must start with a letter or underscore and can contain letters, numbers, and underscores:

playerScore = 100     -- Valid
health_points = 50    -- Valid
_tempValue = 10       -- Valid
-- 2players = 5       -- Invalid (starts with number)
-- my-score = 20      -- Invalid (contains hyphen)

Use descriptive names that clearly indicate what the variable stores:

-- Good naming
playerLevel = 5
gameScore = 1500

-- Poor naming
x = 5
data = 1500

For multi-word variables, use either camelCase or underscores consistently:

-- camelCase style
playerHealth = 100
maxSpeed = 50

-- underscore style
player_health = 100
max_speed = 50

Try it yourself

This lesson doesn't include a code challenge.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals