Why the () on Lua?

This is a question, I couldn’t find an answer
It’s something I got used to do…
Why do some experienced programmers as @Crazyman32
programm like this:

if (true) then
print('hi');
end

exchange of:

if true then
print('hi');
end

The only logic, for me, which is probably false but, like the Syntax takes it like this:

image
(true if so)

1 Like

It really doesn’t matter, it really just comes down to preference. One way isn’t better than the other(unless you are talking about micro - optimisations, which the second example is better).

The latter looks cleaner, though.

3 Likes

There’s no difference between

local e = 1 == 2
if (e) then
    print(e)
end

and

local e = 1 == 2
if e then
    print(e)
end

I don’t know why Crazyman32 writes it like that, in my opinion, it looks more organized. I hope this helps :+1:

Why do some experienced programmers programm like this:

Well I have had the same question :rofl:

Since they are experienced they might have learned it off other languages such as java script where you would need to use () while comparing.

That’s just what I think in my opinion.

1 Like

This honestly just comes down to personal preference and has very minimal impact on your script performance.

1 Like

It is definitely a preference thing, but I prefer to follow the Roblox Lua style guide.

Some other programming languages require parenthesis like in the example you showed. It is likely one of two things: the programmer simply prefers to do this for neatness reasons, or they began learning Lua after coming from another language that requires it, and therefore just does it out of habit.

3 Likes

Sometimes its helpful to get used to doing () so you can be sure the code is running in an order that you want, such as in the case of doing math, PEMDAS you can use () to make it run the math in the order you choose.

Also when doing if something or something and something then, you would use () to determine what conditions should be grouped together for the final check.

Example: if (true and a + b == c) or (false and b > a) then
if you just left it like: if true and a + b == c or false and b > a then – that might give you undesired results.

but as far as just doing () around 1 variable, I guess thats just down to preference.

Though I do remember a time I needed to use () to use it on something, as without it, it would error.
Such as the similar case when checking for magnitude. if (vector3 - vector3).magnitude > 10 then

2 Likes

When there is a single condition it makes no difference whether you are using () or not.

The reason why Crazyman32 and some others naturally use () is because within our real life programming jobs, other languages typically require the use of () wrapping a condition in an if statement, like Java and JavaScript. It is more so just something you’re used to doing, which is why it can sometimes be done within Lua despite not actually requiring it.

2 Likes

I’m not sure if it’s so much a preference for most programmers, but more so a habit. I come from programming in C++ & C#, which are the two primarily languages I program in till this very day, whenever I program in languages like lua, placing () and semicolons ; are both pretty difficult to stop, as semicolons are used constantly and () are used after nearly every for, while or if statement and of course many more.

I can’t speak so much for Crazyman32, but I’m very sure it’s due to the nature of their work outside of Roblox.

Keep in mind that some Lua linters such as selene disallow using parenthesis around a condition by default, as Lua doesn’t require them and aren’t idiomatic.

Still, it’s up to personal preference. I personally don’t use parentheses conditions in Lua.

2 Likes

This is a common misconception. I remember hearing this years ago and adjusting my coding style whenever I was writing performance-critical code, until I actually learned more about how Lua works behind the scenes. There bytecode generated from both examples is identical. In terms of performance, there is no difference between the two examples.

You can test this by running the following code in a standalone Lua installation:

local a = string.dump(function() if (x > y) then print("Hi") end end) local b = string.dump(function() if x > y then print("Hi") end end)
print(a == b)

This script won’t work on Roblox because the string.dump function was removed in an effort to prevent certain types of exploits. It basically takes a Lua function object and returns a string containing the function’s binary representation, including all instructions, upvalues, constants, names of globals used by it, etc.

The first line in this example has to contain both functions because even just changing the line numbers causes the bytecode to change, because the bytecode contains the lines each instruction is on as debug information. Nonetheless, you can see that both functions generate identical binary strings.

It may be true that the second example compiles faster, but this won’t matter at all unless you’re dealing with hundreds of thousands of lines of code, and certainly won’t matter at runtime.

It really is just personal preference. I personally use the () on conditions because it’s required in most other languages, including C, C++, and Java.

4 Likes

IMO, it looks cleaner. Conditionals are encapsulated visually. This is just a style preference. Some linters will actually complain about it (see Rust’s linter, or Selene as mentioned earlier–I turn that off).

As others have mentioned, there’s times where it’s required regarding how you organize the order of ops for boolean logic.

Do whatever you want. Just stick to a style. The vast majority of Lua styleguides will recommend not using parenths.

4 Likes

I use this because if you write something like this

if not 1 + 1 == 3 and 1 + 1 == 2 then -- if 1 + 1 not equal to 3, and 1 + 1 not equal to 2
    print("hi")
end


if (not 1 + 1 == 3) and (1 + 1 == 2) then
    print("hi")
end -- if 1+1 not == 3 and 1 + 1 == 2