View Single Post
11/24/14, 04:06 PM   #15
Sasky
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 231
Originally Posted by circonian View Post
Yes, since your always doing:
Lua Code:
  1. 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:
  1. for _,v in pairs(filters[filterType]) do
  2.    if(v) then
  3.       result = result and v(slot)
  4.    end
  5. 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:
  1. for _,v in pairs(filters[filterType]) do
  2.    result = result and v(slot)
  3.    if result == false then
  4.       return result
  5.    end
  6. 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:
  1. function(slot)
  2.     for _,v in pairs(filters[filterType]) do
  3.         if not v(slot) then
  4.             return false
  5.         end
  6.     end
  7.     return true
  8. 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.

Last edited by Sasky : 11/24/14 at 04:09 PM.
  Reply With Quote