Thread: ZO_Object
View Single Post
07/07/15, 10:17 PM   #12
Sasky
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 231
Originally Posted by Atropos View Post
Functional - Have a single OnUpdate function which loops over the elements of the table and executes some function on each element sequentially.

Object Oriented - Create each element of the table as an object that inherits its own methods and updating function. Each child object updates itself.

Is one approach more efficient (in terms of performance)? It's generally not clear to me whether there is a gain from trying to use object inheritance in LUA other than simply making it easier to think about certain types of problems.
If you're using a homogenous list of elements to update, I'd expect the functional to be faster. But if you're looking at different types that just share an :OnUpdate interface, that gap would close and the OOP-version would be easier to maintain. It also depends how you code your object. See:
http://lua-users.org/wiki/ObjectBenchmarkTests

However, most addons don't need to update frequently enough that that's at all noticeable. So then the main thing is whether or not OOP is appropriate for the situation. Then again, if you have a list of tables/elements, you're doing some level of OOP in Lua, so formalizing it can usually help.

ZO_Object doesn't provide anything you can't do yourself. It's just a way to make things more compact if you use a lot of objects, which the base Lua code does. From that sense, it's more for the internal dev usage, and if you want to use it as well, it's right there. I've generally set metatables directly when I use objects, but that's more because I started that without looking at the source so didn't know what all ZO_Object was doing. Besides, I've used more of interface/implementation than subclasses, which really don't benefit from ZO_Object, especially if you want new() to do something.
  Reply With Quote