WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   General Authoring Discussion (https://www.wowinterface.com/forums/forumdisplay.php?f=20)
-   -   Questions about the API and clean code (https://www.wowinterface.com/forums/showthread.php?t=38344)

CobraA1 01-12-11 01:21 PM

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?

Ekaterina 01-13-11 01:08 AM

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.

Ailae 01-13-11 01:34 AM

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


sylvanaar 01-13-11 02:33 PM

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.


All times are GMT -6. The time now is 06:01 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI