Originally Posted by circonian
Yes, since your always doing:
Lua Code:
result = result and <something else>
if result is already false, its always going to return false, regardless of what <something else> is.
Someone please correct me if I'm misunderstanding this part, but isn't the if statement unnecessary?
Lua Code:
for _,v in pairs(filters[filterType]) do if(v) then result = result and v(slot) end end
Doesn't v just represent functions in the table that tell if something should be displayed or not... since v is not a boolean (correct?) then wont the: if(v) always return true. If v did not exist it never would have made it to that part of the code, it would have broken out of the for loop before that.
So then couldn't he just do:
Lua Code:
for _,v in pairs(filters[filterType]) do result = result and v(slot) if result == false then return result end end
|
The IF would return true as long as v isn't NIL or boolean false. If it's NIL, it shouldn't even be iterated over. If it's a function it can't be false.
As far as the short circuit, it could be simplified even more if v(slot) always returns a boolean:
Lua Code:
function(slot)
for _,v in pairs(filters[filterType]) do
if not v(slot) then
return false
end
end
return true
end
If you hit false, it'll carry the false throughout, and the rest of the filters aren't necessary (as merlight noted) so you can immediately exit. Similarly, this also means a true return will only happen if and only if it iterates through the whole loop. And every time it processes the whole loop it'll return true, so the running assignment is unnecessary. Not as much of an optimization as early exit, but does save some assignment operations.