Wowpedia

We have moved to Warcraft Wiki. Click here for information and the new URL.

READ MORE

Wowpedia

This is a guideline for best practices when writing Lua code.

Avoid using globals[]

References: WowAce coding tips

Try to use local variables whenever possible.

a = "hello" -- bad
local a = "hello" -- good

Ideally an addon should use only a single unique global.

SomeAddon = {}
SomeAddon.value = 0

function SomeAddon:Foo()
	self.value = self.value + 1
end

For instance, the global variable a is written and read by more than a hundred addons.

Replace getglobal[]

Use _G instead of the deprecated getglobal()

for i = 1, MAX_PARTY_MEMBERS do
	--local frame = getglobal("PartyMemberFrame"..i)
	local frame = _G["PartyMemberFrame"..i]
	print(frame, frame.unit)
end

Checking if something exists[]

Any value other than false and nil is truthy.

--if var ~= nil then
if var then
end

Similarly, use the not operator unless you want to explicitly check for false or nil.

--if var == false then
--if var == nil then
if not var then
end

Although this generally is a case of user preferences and coding style.