Thread Tools Display Modes
01-31-10, 11:46 PM   #1
Amenity
Guest
Posts: n/a
Calling functions inside other functions: bad?

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:
  1. local function masterfunction(self)
  2.     print("I'm doing some trivial crap.")
  3. end
  4.  
  5.  
  6. local function slashie(cmd, editbox)
  7.     --SLASHIE
  8.         if cmd == 'SLASHIE' then
  9.             masterfunction()
  10.         end
  11.     end
  12.  
  13.  
  14. local function eventdoer(self, event)
  15.     if event == "PLAYER_ENTERING_WORLD" then
  16.         masterfunction()
  17.     end
  18. end
  19.  
  20.  
  21. SlashCmdList["ADDON"] = slashie;
  22. SLASH_ADDON1, SLASH_ADDON2 = '/addon', '/add';
  23.  
  24.  
  25. MF = CreateFrame("FRAME")
  26. MF:RegisterEvent("PLAYER_ENTERING_WORLD")
  27. 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.
  Reply With Quote
02-01-10, 12:01 AM   #2
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,931
As long as you validate any parameters needed to be used in the function that should be fine. You'll find you will use functions more and more as your addons grow and you find that you end up with code so similar that you just create one function and pass a parameter in to make the difference.

It might be just me but make sure that the functions that are used in other functions are above them in the file so that when it processes the functions further down it doesn't get confused.
__________________


Characters:
Gwynedda - 70 - Demon Warlock
Galaviel - 65 - Resto Druid
Gamaliel - 61 - Disc Priest
Gwynytha - 60 - Survival Hunter
Lienae - 60 - Resto Shaman
Plus several others below level 60

Info Panel IDs : http://www.wowinterface.com/forums/s...818#post136818
  Reply With Quote
02-01-10, 12:01 AM   #3
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
One of the main purposes of a function is re-use. In fact, if I find myself with a function that I'm using in only one place I replace the call to it with the direct code.

Also, here's a little snippet to get rid of all those "if event == "SOME_EVENT" then" checks:

Code:
MF:SetScript("OnEvent", function(self, event, ...)
				if self[event] then
					return self[event] (self, event, ...)
				end
			end)

function MF:PLAYER_ENTERING_WORLD()
        masterfunction()
end
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote
02-01-10, 12:12 AM   #4
Amenity
Guest
Posts: n/a
Originally Posted by Xrystal View Post
It might be just me but make sure that the functions that are used in other functions are above them in the file so that when it processes the functions further down it doesn't get confused.
Yeah, you're right. That was just hastily copy/pasted and edited to remove all the non-pertinent stuff.

Originally Posted by Torhal View Post
Also, here's a little snippet to get rid of all those "if event == "SOME_EVENT" then" checks...
Yeah, those were just another carry-over from my copy/paste crapjob. Always interested in more ways to clean up shoddy code practices, though.

And I don't know why I'm so hung up over this. Even though I know it's not it just feels like a jump statement to me, and I've still got that nagging "GOTO/GOSUB is bad" voice in my head (even though this is nothing like that...well it's sort of like a GOSUB, but not really). Or maybe I'm just a neurotic mess. IDK.

Thanks for the help though, guys.
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Calling functions inside other functions: bad?


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off