I saw a script the other day that had code like this:
:WaitForChild"Humanoid"
I didn’t even know this was possible! I thought you had to use the parentheses.
Do you remove the parentheses in methods or do you keep them?
I saw a script the other day that had code like this:
:WaitForChild"Humanoid"
I didn’t even know this was possible! I thought you had to use the parentheses.
Do you remove the parentheses in methods or do you keep them?
From what I can gather its inherited from lua. it is a thing just less common in luau code. Its up to personal preference. I personally use parentheses because its what I’ve been using since I started scripting and code that doesnt use it looks “wrong” to me.
a lot of lua syntax is optional. it’s recommended by roblox’s own style guide to include all syntax when programming for clarity. the only time i ever don’t do this is when the function i’m calling takes a table as an argument:
self:sort {
element1,
element2,
element3,
}
it’s just clearer this way because there isn’t another set of brackets.
No, I keep them. It feels more natural with parentheses for a multitude of reasons, one being that the parameter is separated from the function, which looks nicer
I honestly forgot that it’s technically possible to not use parentheses. Personally I would always prefer :WaitForChild("Humanoid")
to :WaitForChild"Humanoid"
. The latter just looks weird to me.
I keep them. But it’s mostly just personal preference. Some people do it, and some people don’t.
I think you’d have to be insane to not use parenthesis when calling a function.
The only exception I’d probably make though, is when dealing with constructors like NumberSequence and ColorSequence. Such that:
local numSeq = NumberSequence.new {
-- ETC...
}
But still pretty bad when it’s not used… plus for anyone also scripting with you, and they’d add another parameter to your function, now you have to go to all the calls and add parenthesis just because you didn’t feel like adding them (unless it’s optional argument).
I use parentheses because it looks cleaner and if you have a function that has multiple arguments, then you basically have to use parentheses.
Personally, I just use parentheses all the time because that’s a fairly standard syntax for many programming languages.
The parentheses also help visually seperate the function name from it’s arguments. You don’t gain much by not having them, unless there is a specific situation where the code somehow looks better without them, and makes the code more readable in this case.
But a vast majority of cases, the parentheses make the code easier to read.
And like I said, it’s standard. So when people see code that uses an uncommonly used syntax like this, it’s likely to be more confusing.
With code, style consistency is key to good and readable code.
I used to do this myself quite recently (and I still sometimes do out of habit) because I did some tests to see which version was fastest and my results were that not using parentheses was admittedly slightly faster
But then I realized that if Roblox were to add a useful second argument to that method or function (or I happen to need an existing second argument in the future), I would need to reinclude the parentheses anyways so I stopped doing it
The more I code, the more I find myself following Roblox’s code style guide
it isnt faster at all they are the exact same speed, people that dont use parentheses dont like lua syntax and people that do use them are normal
I’ll test them right now and share the result, but I do remember it being very slightly faster when I previously tested it
they both get compiled into call opcodes
I did the test a long time ago so I might have made a mistake somewhere, but this time the test resulted in a draw or to be more exact, I ran the new test 10 times for 99999999 iterations and sometimes parentheses won and sometimes no parenthesis won but the rate was close to 50/50
because they do exactly the same thing after compiled
I’m not disagreeing with you, but I prefer to test things out myself since unless you’re a Roblox employee we don’t actually know how Luau does things behind the scenes
How the compiler operates in standard Lua doesn’t automatically mean it’s the same in Luau
luau is open source GitHub - luau-lang/luau: A fast, small, safe, gradually typed embeddable scripting language derived from Lua
They literally can’t do different things because that would instantly break backwards compatibility with Lua.
Also, proof that neither is faster:
It’s basically random.
Source code (apologies for bad coding):
function ab(arg)
return arg .. "2"
end
function time1()
local startTime = os.clock()
for i = 1, 1e5 do
ab"arg"
end
local endTime = os.clock()
print(`No parentheses: {endTime - startTime}`)
end
function time2()
local startTime = os.clock()
for i = 1, 1e5 do
ab("arg")
end
local endTime = os.clock()
print(`Parentheses: {endTime - startTime}`)
end
time1()
time2()
Also, even if we grant your point, so what? Any speed gain would be absolutely minute, and it would most certainly not be worth sacrificing this much readability.
I did say:
And also said:
And it’s not like I’m defending not using parentheses:
So I don’t understand why people are attacking me, I made a mistake and I admitted the mistake and corrected it:
spreading misinformation is bad