PascalCase vs. camelCase, and Why?

  1. What do you want to achieve?
    I would like to know when it is appropriate to use PascalCase or camelCase, and the reasoning why it is important.

  2. What is the issue?
    I am very confused, and up until recently I have been exclusively scripting in PascalCase. I read the documentation on this topic, and from what I understand Pascal is reserved for API’s; but as to what those actually are I do not know. Also confusing me is why “HumanoidRootPart” is capitalized in scripting but “humanoid” or “player” is not.

Here is what I usually code like:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local intVariable = 3
local player = game.Players.LocalPlayer

Thanks!

There’s no lexical rules in lua that require you to do it one way or the other. In some languages a capitalized name is used to remind the user that it’s in the global scope. Camel case is more common I think. I mostly agree with the guidelines described here: Naming Things in Code – journal.stuffwithstuff.com

It is up to you as to which which case style you would prefer to use as it really doesn’t matter. Personally, I use camelCase.

The main purpose of case styles is to improve code readability.

PascalCase is commonly used for function names and variables;

local MyNumber = 10
local NumberToSubtract = 5

local function MyFunc(a, b)
    local Result = a - b
    return Result
end

local Value = MyFunc(MyNumber, NumberToSubtract)
print(Value)

camelCase is the most common and is very similar to PascalCase except it starts with the first word lowercase and the second uppercased;

local myNumber = 1
local myString = "Hi"
local myBool = false

local function foo(a, b, c)
    print(a, b, c)
end

foo(myNumber, myString, myBool)

Another case style is SNAKE_CASE and it separates each word with an underscore. It can be used for storing constants which are values you want to keep the same throughout your script;

local ROUND_LENGTH = 50
local INTERMISSION_LENGTH = 30

Again, it doesn’t matter which one you choose it is entirely up to you. It is only used for improving your code readability.

2 Likes

As long as the variable names are valid and unique, there is no functional difference between any of them.

People use specific ones so other other people and you can understand what type of variable the name corresponds to (e.g. if it’s constant, a class, just a regular variable, etc).

That being said, most people (sometimes roughly) follow the official Luau style guide from Roblox:

https://roblox.github.io/lua-style-guide/#naming

Here is the section about naming:


Naming

  • Spell out words fully! Abbreviations generally make code easier to write, but harder to read.
  • Use PascalCase names for class and enum-like objects.
  • Use PascalCase for all Roblox APIs. camelCase APIs are mostly deprecated, but still work for now.
  • Use camelCase names for local variables, member values, and functions.
  • For acronyms within names, don’t capitalize the whole thing. For example, aJsonVariable or MakeHttpCall.
  • The exception to this is when the abbreviation represents a set. For example, in anRGBValue or GetXYZ. In these cases, RGB should be treated as an abbreviation of RedGreenBlue and not as an acronym.
  • Use LOUD_SNAKE_CASE names for local constants.
  • Prefix private members with an underscore, like _camelCase.
    • Lua does not have visibility rules, but using a character like an underscore helps make private access stand out.

A simplified TL;DR:

Class names get PascelCase. Roblox methods get PascelCase. (For custom class methods+properties people do either PascelCase or camelCase, though I think camel is the official casing.) Constants (e.g. settings) get LOUD_SNAKE_CASE. Other variables and functions get camelCase.

1 Like

i didnt even know luau grammar was a thing until i read this post so i doubt it really matters

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.