Is there anything that could be improved in my Maid module?

Ive always been against using stuff such as open source modules. Ive always been a guy that wants to make my own modules from scratch. Soo today i decided i wanted to give a shot at creating my own Maid module. My only concern is if there is anything that could be improved or cause any sort of memory leak??

Should also note that this Maid was coded to only support RBXScriptConnections as you can see from the assert(). Nothing else.

Heres my Maid code currently (:

local Maid = {}
Maid.__index = Maid




function Maid.new()
	return setmetatable({},Maid)
end



local function GetNumCons(Table) -- // Cons Short for Connections
	local Num = 0
	
	for _, v in pairs(Table) do
		Num = Num + 1
	end

	
	return Num
end


function Maid:GiveTask(Func)
	
	assert(typeof(Func) == "RBXScriptConnection","Argument isnt an RBXScriptConnection. Maid only accepts connections!")
	
	local NumConnections = GetNumCons(self)
	
	local Key = "Task_"..tostring(NumConnections)
	
	self[Key] = Func
	
	
	
	return Key -- // Key used when disconnecting/cleaning a task
end


function Maid:ClearTask(TaskKey)
	
	if self[TaskKey] then
		
		local Task = self[TaskKey]
		Task:Disconnect()
		Task = nil

	else
		warn("No task with the key " ..TaskKey.." has been created before")
		return
	end
	
end


function Maid:ClearAllTasks()
	
	
	for i, v in pairs(self) do
		v:Disconnect()
	end
	table.clear(self)

end

return Maid

1 Like

I would use string.format instead of concentation.

Dont use assert, it leaks script memory
Instead, do an IF which is way more efficient.

1 Like

Ive never used format before? is it almost the same as concentation?

Also thanks for the tip about assert. I didnt know it could cause leaks O.o!

1 Like

Its almost that except more cleaner, here is what you would do with string.format

local name, amountOfMoney, currency = "John", 30, "dollars"
local formattedString = string.format("%s has %s %s!", name, amountOfMoney, currency)
print(formattedString) -->> John has 30 dollars!

No problem

1 Like

Alr thanks for the feedback it really means a lot (:!!

1 Like

Using assert is fine as long as you don’t do in-line string operations like string concatenation and formatting.

This means in your case, this is fine because there is no in-line string operation as the second argument for assert:

Also, assert doesn’t leak script memory; if you use assert frequently with in-line string operations, it will be unnecessarily costly for the CPU. If you need more info on this, please refer to my post.

The post title only says “Be Careful,” not “Avoid” for such reason.

1 Like