How to shorten a long if statement

I have something that looks like this:

if inputUnWeldedReturnUnWelded[part] ~= nil or inputCylinderReturnArrayOfTwoJoints[part] ~= nil or displayPartReturnsArrayOfTwoBlocks[part] ~= nil or inputJointReturnJoint[part] ~= nil then		
	print("invalid mainpart selected") return
end

(pseudocode)

How do I shorten/simplify this?

Thanks!

1 Like

one way is via early returns

if inputUnWeldedReturnUnWelded[part] == nil then return end
if inputCylinderReturnArrayOfTwoJoints[part] == nil then return end
if displayPartReturnsArrayOfTwoBlocks[part] == nil then return end
if inputJointReturnJoint[part] == nil then return end

print("invalid mainpart selected")
return

You could try creating an early return table, your code seems like its indexing tables so this would take some more thought, but an example could be like below, where I am early returning keys that I want to ignore

local earlyReturns = {
      [Enum.Keycode.Y] = true
}
local UIPS = game:GetService("UserInputService")
UIPS.InputBegan:Connect(function(input, gpe)
     if earlyReturns[input.Keycode] then return end
     print("Y key not pressed")
end

There are split points. The problem here is how long your variable names are.

if inputUnWeldedReturnUnWelded[part] ~= nil or
	inputCylinderReturnArrayOfTwoJoints[part] ~= nil or
	displayPartReturnsArrayOfTwoBlocks[part] ~= nil or
	inputJointReturnJoint[part] ~= nil then		
	print("invalid mainpart selected") return
end

All I care about is that page run over… I really would do it this way or most likely I’d shorten up them names. Maybe both even.

iif unWeldedR[part] ~= nil or cylJointsR[part] ~= nil or
	dispPartR[part] ~= nil or inputJointR[part] ~= nil then
	print("invalid mainpart selected") return
end
if inputJointReturnJoint[part] ~= nil then
	if inputUnWeldedReturnUnWelded[part] ~= nil then
		if displayPartReturnsArrayOfTwoBlocks[part] ~= nil then
			if inputCylinderReturnArrayOfTwoJoints[part] ~= nil then
				print("invalid mainpart selected") return
			end
		end
	end
end

I like txcIove approach also considering it going to be a return.

I have the ultimate suggestion

local perfectionAchieved = true
local chks = {inputUnWeldedReturnUnWelded[part] ~= nil, inputCylinderReturnArrayOfTwoJoints[part] ~= nil, displayPartReturnsArrayOfTwoBlocks[part] ~= nil, inputJointReturnJoint[part] ~= nil}

for _, v in ipairs(chks) do
	if v == false then
		perfectionAchieved = false
		break
	end
end

if perfectionAchieved == false then
	print("invalid mainpart selected")
	return
end
-- code

Shortens the conditional statement to a singular boolean check :sunglasses:

if inputUnWeldedReturnUnWelded[part] ~= nil or 
inputCylinderReturnArrayOfTwoJoints[part] ~= nil or 
displayPartReturnsArrayOfTwoBlocks[part] ~= nil or 
inputJointReturnJoint[part] ~= nil then		
	print("invalid mainpart selected") return
end

Tbh I usually just indent it down a line, Lua isn’t line sensitive so you can do this with no issue, that way you don’t have to scroll to see the end of the line. Though, the other suggestions ppl commented are also good options

I ended up doing this:

local ArraayOfTables = {
	inputUnWeldedReturnUnWelded[part], 
	inputCylinderReturnArrayOfTwoJoints[part], 
	displayPartReturnsArrayOfTwoBlocks[part], 
	inputJointReturnJoint[part]
}
	
for _, v in ArraayOfTables do
	if v ~= nil then
		print("invalid mainpart selected")
		return
	end
end

-- code here

But I used your code as inspiration. It was kind of what I had in my head, but I needed to see it to realize it. Any objections?

Thanks!

1 Like