Is formatting if loops like this bad?

Hey! I have an if loop with a decent amount of conditions that I don’t want in the same line.

if table.find(keybinds, input.KeyCode) or input.UserInputType == Enum.UserInputType.Touch or input.UserInputType == Enum.UserInputType.MouseButton1 then
	-- code
end

I tried spreading it across multiple lines, and now it’s more readable, but it looks a little… weird.

if
	table.find(keybinds, input.KeyCode) or
	input.UserInputType == Enum.UserInputType.Touch or
	input.UserInputType == Enum.UserInputType.MouseButton1
then
	-- code
end

Is this acceptable?

Apologies if this is the wrong category

1 Like

I’m fairly sure that this second example included in your post is the more recommended way of formatting it since it improves readability (only 1 condition per line) and you don’t need to scroll to the right in the Script Editor to see everything that was written.

Only adjustment I’d make would be to move any operators (or, and, etc.) to the beginning of the next line rather than at the end of each one, as this will make it easier from first glance to know if only one condition needs to be true versus situations where all need to be true:

Example Revision:

if
    table.find(keybinds, input.KeyCode)
    or input.UserInputType == Enum.UserInputType.Touch
    or input.UserInputType == Enum.UserInputType.MouseButton1
then
    -- code
end
3 Likes

Thank you! I wish “if” was a longer word, it would look so much less weird, lol.

1 Like

Yeah, it took me some time to get used to it since it made it look like it wasn’t aligned properly, but it will definitely help save time reading through code in the long run, especially with much longer scripts.

Oh, and also one slight correction for future reference; these are called if statements / conditional statements and not if loops, as loops tend to refer to things like this:

for count = 0, 10, 1 do
    print(count)
end

-- Or

for index, object in workspace:GetDescendants() do
    print(object.Name)
end

So what you were referring to in your original post would be something closer to “an if statement with multiple conditions”.

3 Likes

I actually have another recommendation for this refactoring:

local function isInputValid(input)
    local acceptedInputTypes = {
        Enum.UserInputType.Touch,
        Enum.UserInputType.MouseButton1
    }
    return table.find(keybinds, input.KeyCode) or table.find(input.UserInputType, acceptedInputTypes)
end

if isInputValid(input) then
    -- code execution here
end

For context, I was once told that if the condition is becoming excessively long, try to squash it in a function like that and give it a name.

2 Likes

Why the Multi-line Format Works:

  • Clarity: Each condition is clearly separated, making it easier to scan.
  • Maintenance: If you ever need to add or modify one of the conditions, it’s simpler to do so without the risk of breaking the whole statement.
  • Readability: It prevents horizontal scrolling, which can be annoying when reviewing code.
1 Like

I was revisiting this thread and I had some quick questions about the code that I didn’t notice when first reading through your post:


return table.find(keybinds, input.KeyCode)

1. A keybinds table wasn’t included in the codeblock so I presume this is just an example for if you were to have a general list of keybinds outside of this scope, possibly somewhere at the top of the script?

Edit: @Ohio_trouble350 reminded me that keybinds was what they named the table in the original post. I forgot about that! :slight_smile:


or table.find(input.UserInputType, acceptedInputTypes)
  1. Is this meant to be the other way around, since table.find() accepts a table first and then what you want to look for in the table?


And also, thanks for highlighting that way of formatting it! That’s a much more useful alternative, especially for cases where you may need to repeatedly check for the same set of conditions across multiple parts of a script.

I can’t speak for Operatik but for me that is what I had defined for keybinds.

It was something like this, and it was for skipping dialog:

local keybinds = {
    Enum.KeyCode.E,
    Enum.KeyCode.Return,
    Enum.KeyCode.LeftShift,
    Enum.KeyCode.RightShift,
    Enum.KeyCode.Space,
}
1 Like

Personally, I prefer having it in one single line and using word wrap. However, all methods are valid as long as the code works as intended!

Oh, oops, I forgot that you had included keybinds as a table in the original post. I will edit my reply with a correction :grin:

1 Like

I honestly didn’t know you could leave spaces in the if statements LOL

Good to know now :rofl:

2 Likes

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