Originally Posted by laukond
Would you mind telling me what libaries do and how to use them wisely?
|
Let's say you want 10 checkboxes in your addon's config panel. You can either:
(1) write 50 lines of code per checkbox to define its size, position, textures, font strings, clickable regions, and script handler functions, for a total of 500 lines of code, or:
(2) write 40 lines of code inside a function, and then call that function 10 times to create 10 checkboxes, and then add 10 lines of code per checkbox to set its position, its text, and its script handlers, for a total of 150 lines of code.
Obviously #2 is more efficient, and will be much easier to maintain, since if you want to change how all the checkboxes look, you only need to change what's inside the "make a checkbox" function, instead of changing the same lines for every checkbox. #2 also makes it much easier to add new checkboxes, since you don't have to copy/paste/edit 50 lines of code every time you want to add a checkbox. You just call the function again to make another checkbox, and add a few lines to position it correctly.
A library is nothing more than doing #2 on a bigger scale.
Let's say you want to write a second addon, and add some checkboxes to its options window. Now you can:
(1) copy/paste your 40-line function into your second addon, for a total of 80 lines of code and two functions loaded in memory, or:
(2) put that 40-line function in a separate file and make it into a library, so that only 40 lines of code and one function is loaded into memory.
Again, #2 is clearly more efficient, especially when you look at libraries that are used by thousands of addons. Even if a user is running 100 addons that use the same checkbox library, they will still only have 1 copy of the "make a checkbox" function loaded in memory.
Now let's say that someone else already wrote a library to make checkboxes. Think of how much time you can save by simply using their checkbox making function(s) instead of writing your own. Plus, if there's ever a problem, or the code needs to be updated because of a game patch, it only needs to be updated in one place (the library) instead of in many places (every addon using its own checkbox code).
There can be downsides to using libraries, though.
(1) There will necessarily be some overhead by putting code into a library instead of just putting it in your addon. Mainly, this is because the library needs to have extra code to provide easy ways for addons to use it. This overhead is usually minimal (assuming the library is well written), and when the library is shared, overall efficiency is still improved.
(2a) Because a library needs to be used by many addons, it has to be very generic, and this can lead to the library becoming bloated by trying to accomodate all possible uses.
(2b) On the other hand, if a library remains very specialized, that limits what kind of addons can use it, so it becomes less efficient overall when it isn't shared. If a library is so specialized that most people will only be running one addon that uses it, then it probably shouldn't be a library in the first place.
(3) There are also some libraries that don't really do anything, yet are in widespread use anyway, for reasons I can't fathom. AceEvent-3.0 is a good example of this; it doesn't do anything that the basic
frame:RegisterEvent(
event) and related API does not do, and is probably less efficient since it's doing all the work in Lua code instead of letting the client handle it in much faster C code. There was probably some reason for its creation, but nobody seems to remember what it was.
Basically:
(1) If a library provides a common function, has a clear benefit over doing it yourself, and is used by many addons, it is probably a good option.
(2) If a library provides a very specialized function, has no apparent benefit over doing it yourself, or is not used by very many addons, you should think very carefully before using, or not use it at all.
(3) If a library is bigger than your entire addon, it is probably overkill for your addon, and you should probably look for a smaller library, or not use one for that function.
(4) If you only want one or two checkboxes, it's probably better to just write them yourself. If you want many checkboxes, dropdowns, sliders, and buttons, using libraries is probably a good option that will save you a lot of time and headaches, without sacrificing much (if any) efficiency. A well-written library will not impact the user's performance enough for them to notice it, if it does at all.