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 = 1500For multi-word variables, use either camelCase or underscores consistently:
-- camelCase style
playerHealth = 100
maxSpeed = 50
-- underscore style
player_health = 100
max_speed = 50Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators 2 Relational & Logic
Equality OperatorsRelational OperatorsThe 'and' OperatorThe 'or' OperatorThe 'not' OperatorShort-Circuit EvaluationTruthy and Falsy ValuesRecap - Simple Logic7Basic Conditional Logic
The if-then StatementThe if-then-else StatementThe elseif StatementRecap - Treasure Chest2Variables and Data Types
What is a Variable?NumbersStringsBooleansThe Value 'nil'The type() FunctionNaming ConventionsRecap - Character Profile5Basic Output
Printing LiteralsPrinting VariablesPrinting Multiple ValuesCombining Strings & VariablesThe tostring() FunctionInputCastRecap - Status ReportRecap - Till 1208String Manipulation Basics
string.len()string.upper & string.lowerstring.sub()string.rep()string.find()Recap - Format Username3Operators 1 Arithmetic & Conc
Arithmetic OperatorsModulo OperatorExponentiation OperatorString ConcatenationOperator PrecedenceRecap - Simple Calculations6Project: Character Stats Disp
Welcome MessageDeclare Character Stats9Functions Basics
Declaring a FunctionCalling a FunctionFunctions with ParametersFunctions with Multiple ParamsThe 'return' StatementRecap - Area Calculator