I usually put semicolons at the end of most lines, for example:
local function Hello()
print("Hello!");
end;
Hello();
I’m not sure why I got the habit, but I’ve seen that in other languages they are used + I use them in the command bar (to separate different pieces of code) so those might be the reasons. I’m pretty sure they can be used to separate statements, which I doubt is useful in normal scripts. Is using them a bad practice? Do they have any cons? Does someone else use them or I’m the only one? I’ve been wanting to ask this question for a while now, and I haven’t found much on the DevForum except a couple posts about using ; or , in tables, which is not what I’m looking for. On the DevHub it’s also not specified so I thought about directly asking to other people.
(Also, please move this post to #help-and-feedback:scripting-support if that’s a better category to post this in)
You’ll rarely ever be in a situation in Lua where you’ll actually need to use a semicolon, and I would recommend only using them if needed, as adding them to your scripts everywhere will reduce the readability slightly, unless you’re using them inside of tables, which I do. I prefer them in tables since I add a trailing comma/semicolon on the last entry in a table, and I think a trailing semicolon looks better than a trailing comma.
Yes, it is considered bad practice as they serve no purpose, languages like Java use them but isn’t needed in Luau, when compiled to bytecode, they are ignored.
How would serving no purpose and being ignored when compiled to to bytecode is bad practice though? According to your logic, spreading your code to multiple lines is also a bad practice because technically newline characters are also ignored when compiled to bytecode.
The reason it is considered bad practice is because Lua doesn’t require them and it is heavily unneeded, Lua doesn’t work like Java or some other languages which necessarily require semi colons to be used after every statement or line.
This is incorrect.
In this instance, an explicit statement separator like a semicolon are required
local a = setmetatable({1}, { __call = function() return function(x) return x end end })
(print){2}
print(a[1])
This will error in Luau because it’s ambiguous without semicolons due to a newline which looks like it’s a new statement. Lua 5.2+ will print this as 2 without a semicolon at the and of the first line and will print it as 1 with a semicolon.
Good/bad practice does not necessarily correlate with performance. A lot of best-practices are created to help code be more structured and readable. Adding a bunch of semicolons just looks messy in a language that literally doesn’t need them. It makes code easier to maintain when practices are kept relatively consistent.
However, there are a few weird edge-cases where semicolons in Lua are required. For instance, if you call two anonymous functions right after each other, you must provide a semicolon:
(function() print("Test1") end)(); -- That semicolon is required
(function() print("Test2") end)()
This is because whitespace is ignored in Lua. So the above code parses out weirdly without the semicolon. But I can’t think of other cases where this pops up.