12.07.2015 Views

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

Wiley-World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

1320 Part V ■ AppendixesEven if you do not know be<strong>for</strong>eh<strong>and</strong> what conditions will be most likely,some simple pr<strong>of</strong>iling tests can help figure that out <strong>for</strong> you. For instance,you could maintain counters <strong>for</strong> each clause that increment whenever theyexecute. Then you could create a function to print out the stats. Most <strong>of</strong> thetime, such optimization is unnecessary. If you’re doing anything in OnUpdateor some other extremely frequent code path, though, it may just be worth theresearch. (Remember to remove said counters <strong>for</strong> the release version <strong>of</strong> yourmod because incrementing them has its own per<strong>for</strong>mance impact.)Exploit Shortcut EvaluationIn a similar vein, you can use a feature <strong>of</strong> Lua (<strong>and</strong> other programminglanguages) called shortcut evaluation to optimize the conditionals themselves.Consider the expression a or b.Theor operator evaluates to true if eithera or b evaluates to true. Once Lua knows that a is true, there is no reason toevaluate b. Similarly, as soon as it’s known that c is false in the expressionc <strong>and</strong> d, the entire expression is known to be false.This also has the interesting benefit (in Lua) that every non-nil, non-falsevalue is considered true. Then you can initialize in the following way:variable = variable or “Some default string“to accomplish something equivalent to the following:if not variable thenvariable = “Some default string“endDepending on your specific need, you may find the Boolean expressioneasier to read.In addition, any time you have a complex condition with several clauses,you should try to put the most likely <strong>and</strong>/or fastest-to-evaluate clauses first.In pseudo-code, this would look something like the following:if likely condition or less likely condition thendo stuffelseif fast condition <strong>and</strong> slow condition thendo stuffendNOTE This section describes the logical result <strong>of</strong> the evaluation <strong>of</strong> Booleanexpressions. As discussed in Chapter 2, the actual results <strong>of</strong> the <strong>and</strong> <strong>and</strong> oroperators are one <strong>of</strong> the two oper<strong>and</strong>s depending on whether the first one isinterpreted as true (anything other than false or nil)—thereturn table.remove(freeTables) or {} statement from the earlierGetTable function, <strong>for</strong> example.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!