Hey!
I’ve been seeing people starting to add a ; at the end of some lines, why?
Is there some sort of reason I am missing that you should do it too?
It’s a common habit that some people have from using other programming languages. Fundamentally it doesn’t change your code at all, other than resolve Luau ambiguities (which aren’t always a problem, if you know what you’re doing).
The most common reason for using the semicolon in Luau is to resolve ambiguities in cases like these:
local x = y
(x :: Whatever):Hello()
Is this code calling y
? Most people would answer “no”, because there’s obviously a line break there, but Luau sure doesn’t know - because Lua ignores most whitespace while parsing. However, this is a special exception because it actually throws an error.
“Syntax error: Ambiguous syntax: this looks like an argument list for a function call, but could also be a start of a new statement; use ‘;’ to separate statements”
So, fundamentally, ;
is used to unambiguously end a statement. That syntax error can be suppressed by doing one of two things, depending on your intention:
-
Don’t end the statement, you actually intended to call
y
.local x = y(x :: Whatever):Hello()
or
local x = y( x :: Whatever ):Hello()
-
End the statement, you actually intended to start a new statement.
local x = y; -- <-- semicolon! (x :: Whatever):Hello()
Putting ;
where it’s not necessary is largely, like @dukzae said above, a carry-over from other programming languages, and also a relatively useless practice in Luau. It’s not actively harmful, but it’s ugly.
tldr; Using semicolons isn't ugly for everyone. It all just boils down to personal preference.
I respect your opinion here and think you should code in the style that you’re most comfortable with, but my opinion is that if you’re going to have semicolons in a couple of places to prevent ambiguity issues, having a couple of semicolons here and there and then suddenly nowhere else throughout the rest of the code is what makes it look ugly. I’d prefer for all of the code to follow the same format. Due to this, rather than making code look more messy… it actually has the opposite effect for me and makes code feel more clean.
I understand that not everyone feels the same way, though. For the most part, people on Roblox view this behavior as messy. I just… straight up disagree.
Ambiguity problems isn’t the main reason why I do it, though. Using semicolons helps prevent me from falling out of the habit whenever I switch between languages (as mentioned already in the previous 2 posts). Whenever I first started learning multiple languages and then would come back to Roblox, I had no idea that semicolons could be used and found myself accidentally ending my lines with semicolons and thinking that I had to go back and remove them all. This got frustrating pretty quickly, until I had discovered that lua ignores them.
I also find hitting the semicolon key a lot easier than hitting the comma key whenever coding, since it’s a habit that has already been ingrained in me. I mention this because I find it particularly useful for creating tables - I’m already in the mindset of ending all of my lines with semicolons, and therefore can just end the lines for the entries of my table the same way I’ve been ending lines for the rest of my code. I think tables (dictionaries in particular) just look better this way, too.
Here’s an example of a dictionary separated with semicolons from one of the threads I share at the bottom of this post:
I don’t know if it’s some sort of symptom of OCD or what (since I’m definitely slightly OCD), but seeing an actual marker for the end of lines throughout the code also just feels a lot more comfortable for me when reading. Makes it feel more like code to me rather than a bunch of sentences.
There’s a lot of other threads on this topic, by the way. This is just a few (the first one has a few replies that I really like):
https://devforum.roblox.com/t/do-you-use-semicolons-in-your-code-why-or-why-not/795161