I'm trying to make a good effort at teaching myself good coding practices because as my work gets more and more complex, my code gets messier and messier...so try to bear with my incessant questioning.
I recently came to a head on something I've been working on when I realized I had the exact same chunk of code in two different parts of a file I was working on. Being obviously ridiculous I tossed it all into a function and deleted the extraneous copy. Normally this wouldn't be a big deal, except that this particular function needs to be called for two different things: On a PLAYER_ENTERING_WORLD event (easy enough), and also as part of a slash command function.
For the sake of simplicity:
lua Code:
local function masterfunction(self)
print("I'm doing some trivial crap.")
end
local function slashie(cmd, editbox)
--SLASHIE
if cmd == 'SLASHIE' then
masterfunction()
end
end
local function eventdoer(self, event)
if event == "PLAYER_ENTERING_WORLD" then
masterfunction()
end
end
SlashCmdList["ADDON"] = slashie;
SLASH_ADDON1, SLASH_ADDON2 = '/addon', '/add';
MF = CreateFrame("FRAME")
MF:RegisterEvent("PLAYER_ENTERING_WORLD")
MF:SetScript("OnEvent", eventdoer)
Good/bad/doing it wrong/won't work? Can/should I combine eventdoer and masterfunction so that one function can do all of it? I feel like I'm overlooking something so simple and blaringly obvious that I'm going to feel really stupid when it's pointed out.