Hey fellow developers!
I wanted to discuss the ways we name things. How we write them in Luau scripts. Naming conventions play a role, in making our code easy to understand, update and look professional. Lets take a look at the styles of naming and when its best to use them in Luau scripting, for Roblox projects.
1. camelCase
CamelCase is a popular naming convention where the first word is in lowercase, and each word after starts with an uppercase letter. It looks like this: myVariableName
.
When to use CamelCase:
-
Variables: Most commonly used for local variables and non-constant global variables.
- Example:
playerHealth
,enemyCount
.
- Example:
-
Function Names: Frequently used for function names.
- Example:
getPlayerScore()
,updateEnemyPosition()
.
- Example:
Example:
local playerHealth = 100
local function updatePlayerHealth(amount)
playerHealth = playerHealth + amount
end
2. PascalCase
PascalCase is similar to CamelCase, but the first letter of each word, including the first word, is capitalized. It looks like this: MyClassName
.
When to use PascalCase:
-
Class Names: Ideal for naming classes, modules, or constructors.
- Example:
PlayerController
,GameService
.
- Example:
-
Module Scripts: Commonly used for naming module scripts to differentiate them from other objects.
- Example:
DataManager
,AudioManager
.
- Example:
-
Services: Used for naming services, especially when referencing Roblox services.
- Example:
DataStoreService
,Players
,ReplicatedStorage
.
- Example:
Example:
local DataStoreService = game:GetService("DataStoreService")
local DataManager = {}
function DataManager:SaveData(player)
-- logic for save data
end
return DataManager
3. snake_case
In snake_case, words are separated by underscores, and all letters are in lowercase. It looks like this: my_variable_name
.
When to use snake_case:
-
Configuration Variables: Sometimes used for configuration settings or constants.
- Example:
max_player_count
,default_starting_position
.
- Example:
-
File Names: Helpful in naming script files to ensure clarity, especially in larger projects.
- Example:
player_data_handler.lua
,inventory_manager.lua
.
- Example:
Example:
local max_player_count = 16
local default_starting_position = Vector3.new(0, 10, 0)
4. UPPER_SNAKE_CASE
This is a variant of snake_case where all letters are uppercase. It looks like this: MAX_PLAYER_COUNT
.
When to use UPPER_SNAKE_CASE:
-
Constants: Perfect for defining constants in modules that are not meant to change.
- Example:
MAX_HEALTH
,DEFAULT_GRAVITY
.
- Example:
Example:
local MAX_HEALTH = 100
local DEFAULT_GRAVITY = Vector3.new(0, -196.2, 0)
5. kebab-case
Kebab-case is similar to snake_case but uses hyphens instead of underscores. It looks like this: my-variable-name
.
When to use kebab-case:
-
File Names: Commonly used in file names, most commonly in web development, but less common in Luau. Some developers use this for script names for clarity.
- Example:
player-data-handler
,inventory-manager
.
- Example:
Example:
- File name:
leaderboard-service.lua
Best Practices
- Consistency: Regardless of the case you choose, ensure consistency throughout your project. If your team decides on CamelCase for variables, stick with it.
- Code Standards: Not all people will use these cases properly, if you are coding a script after it has already been made and the cases used are inproper / incorrect, make sure to go with the flow and try not to change too much.
- Readability: Prioritize readability. Naming stuff properly should help clarify what a variable or function does, not obscure it.
By understanding and applying these different case styles appropriately, you’ll make your code more readable and maintainable. I hope this guide helps you in your Luau scripting!