Critique my code!
I am new to lua and coding in general so any feedback on these 2 small addons is appreciated.
Context: Hides minimap buttons until mouseover. https://pastebin.com/gdcXHF5k Context: Hides the enitre micro menu until mouseover. https://pastebin.com/aUh1b9Zp |
Quote:
So, come up with "better", clearer names for them. It'll probably be hard, because the two hardest things in software development are regular expressions, naming things, and off-by-one errors. Also, I believe convention for local variables/functions and function params is camelCase. Not that that point matters much, as long as you're consistent in which casing you use. |
Any instance of "in pairs" loops:
Lua Code:
should be written as: Lua Code:
or something similar. "in pairs" uses more CPU than just running through the table's values with an integer for loop. |
Would you mind explaining a little further the differences between what I did and what you suggested?
|
He is basically saying that an integer loop is faster than ipairs. But... to be honest...
that really does not matter. You are not doing any stuff which needs a optimized performance. Your code is fine. The only thing I would criticize is that you are not covering every minimap button, only those in your table. You could do something like this to get every button: Lua Code:
In general I would always recommend to choose speaking variables and function names. Also I would recommend anyone who starts to code in any programming language to get to know the basic programming and engineering skills. Quote:
|
But it seems more complicated to do what youre doing vs just grabbing the frame and executing the functions
|
whats more efficient and why?
for i = 1, #MinimapButtons do MinimapButtons[i]:SetAlpha(0) end for _, Frame in pairs(MinimapButtons) do Frame:SetAlpha(0) end |
The integer loop, it doesn't perform a function call, pairs, which actually calls another function.
|
okay, thanks for the reply, for the integer loop, is it infinite or does it stop at the end of the table?
|
Quote:
Lua Code:
"#MinimapButtons" means the amount of values in MinimapButtons. So if you have 10 minimap buttons it would be: Lua Code:
Quote:
If you use that code only for yourself you dont need that. But if you are going to public an addon for anyone your code would be quite useless if other players have minimap buttons that you dont have. |
Note that an integer loop is the same as ipairs(), but not the same as pairs(). The latter can be used when there are holes in your table or when your table does not have integer-based keys.
|
so youre saying if my table is for instance containing {PlayerFrame, FocusFrame, TargetFrame}. I should use pairs() or the numeric loop?
|
That person is saying "for i = 1, #table do" and "for k, v in ipairs(table) do" are basically the same, with the first being slightly more performant. If you had a table that looked like this:
Lua Code:
Lua Code:
In the case of the second example, Lua Code:
Code:
1, table:... Lua Code:
Code:
1, table:... |
All times are GMT -6. The time now is 05:26 AM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI