Is this a good way to "assert"?

Hiya,

I have this OOP module which creates a new party, but I’m wondering if this is an okay way to format/check arguments.

Not only is the default assert() slow, but it’s also pretty ugly syntactically (In my opinion).

My question is, if you were to read this module and try to understand it, would you think this is a good way to custom assert arguments?

Code:

function Party.new(Id :string, Name :string, Leader :Player)
	local self = setmetatable({}, Party)
	
	-- Properties --
	self.Id = Id or error(debug.traceback("No id provided to create party."))
	self.Name = Name or error(debug.traceback("No name provided to create party."))
	self.Leader = Leader or error(debug.traceback("No leader provided."))
	self.Members = {Leader} 
	

Screenshot in code editor:

1 Like

You could set the level argument in error instead of wrapping it inside debug.traceback. That would expose the error as originating from the script interfacing the module instead from the module or script tree, which would be more accurate here.

error("oh no", 2) -- Logs 'Script' instead of 'Script.Module'
3 Likes

Instead of using this for error-handling (as a suggestion) you can also use a module called “roblox-lua-promise.” It is very good at error handling and async script running. With that module, you can get it to retry and attempt to do things again multiple times and etc.