Hi so I have been wondering, would you guys say its cleaner and more readable to use early returns in Roblox lua. From what I have googled and found, some people prefer early return because it in a way validates the values passed into the function first, and then it gets that out of the way from the code. Another thing is if you should do the same thing with loops. For instance in the example below I use continue in a similar fashion of using return, but just skipping the current loop. I am very interested in hearing you guys thoughts on these so lmk what you think
Early return
local function earlyReturnExample(partUnixLifeTime, player)
if not partUnixLifeTime or player then
return
end
for x = 1,10 do
local buildingBlock = game.Workspace:FindFirstChild(tostring(x))
if not buildingBlock then
continue
end
buildingBlock.Colour = Color3.fromRGB(255,0,0)
buildingBlock:SetAttribute("partUnixLifeTime", partUnixLifeTime)
buildingBlock:SetAttribute("player", player)
end
end
No early return example
local function noEarlyReturnExample(partUnixLifeTime, player)
if partUnixLifeTime and player then
for x = 1,10 do
local buildingBlock = game.Workspace:FindFirstChild(tostring(x))
if buildingBlock then
buildingBlock.Colour = Color3.fromRGB(255,0,0)
buildingBlock:SetAttribute("partUnixLifeTime", partUnixLifeTime)
buildingBlock:SetAttribute("player", player)
end
end
end
end