Thread Tools Display Modes
01-12-11, 01:21 PM   #1
CobraA1
A Cliff Giant
 
CobraA1's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 73
Questions about the API and clean code

Hey, I'm the author of the Reagent Restocker addon, and I'm trying to add guild bank support to my addon.

Currently, Reagent Restocker makes several calls to GetContainerItemLink and GetContainerItemInfo. However, all of that changes when I want it to access the guild bank.

When the guild bank is being accessed, it needs to use GetGuildBankItemLink and GetGuildBankItemInfo for items in the guild bank.

Problem is, I'm unsure of how to do this cleanly. I started putting in a lot of if...else..end statements, but it just makes the code a lot more complex and more difficult to work with, and has to be done pretty much anywhere where I need choose between a guild bank function and the regular function.

Is there a way to cleanly use these API calls without making my code a mess?
  Reply With Quote
01-13-11, 01:08 AM   #2
Ekaterina
A Theradrim Guardian
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 65
Could you do something like:

Code:
 local localItemLink = GetContainerItemLink
 local localItemInfo = GetContainerItemInfo
- Then when the guild bank is accessed set them to
Code:
 localItemLine = GetGuildBankItemLink
 localItemInfo = GetGuildBankItemInfo
That way in most of the code you just use the variable names.

I'm not sure if it would work, but may be worth a try.
  Reply With Quote
01-13-11, 01:34 AM   #3
Ailae
A Rage Talon Dragon Guard
 
Ailae's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2007
Posts: 318
Couldn't you wrap the calls in a local function and do the logic there? Then at least you wouldn't have lots of ifs running in the code.

You could either do those if-statements you mentioned or if you can tell for sure when a player is or isn't at the guild bank you could pass a boolean to the function.

Code:
local function RetrieveInfo(gbank)
	if gbank then
		-- use GetGuildBank*
	else
		-- use GetContainer*
	end
end
__________________
Oh, the simulated horror!
  Reply With Quote
01-13-11, 02:33 PM   #4
sylvanaar
A Warpwood Thunder Caller
AddOn Author - Click to view addons
Join Date: Sep 2006
Posts: 92
Code:
function ProcessInfo(link, info)

end

function RetrieveBagOrBankInfo()
  local link = GetContainerItemLink()
  local info = { GetContainerItemInfo() }

  ProcessInfo(link, info)
end

function RetrieveGuildBankInfo()
  local link = GetGuildBankItemLink()
  local info = { GetGuildBankItemInfo() }

  ProcessInfo(link, info)
end
I would keep each separate so it is clear what api's are being called. This code is more readable imho.
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Questions about the API and clean code


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