What is the best way to define a module function?

Been thinking about this question for a bit recently, since I have adjusted how I do it. Is there a “better” way of doing this, whether is it optimization, cleaner code whatever the case.

Method 1
module.functionName = function()

end
Method 2
function Module.FunctionName()

end
Method 3
— Similar to the first one
module[“FunctionName”] = function()

end

What do you think and why?

  • Method 1
  • Method 2
  • Method 3

0 voters

1 Like

there is another:

local Module = {
    FunctionName = function()
        
    end
}

return Module

in terms of optimization all your three end up looking like this compiled, it’s just defining a function in or outside of the dictionary before its returned to whatever is require()'ing the modulescript

That’s true, wonder why so many people prefer the 2nd one. Maybe there is a reason or just what they were taught

1 Like

The developer wiki uses method 2 in its examples so it’s likely popular by first impression.
Method 1/3 remind me of Ye Olde times when an event connection might look like:
game:GetService("ReplicatedStorage").Event.OnClientEvent = function() [...] end

Haha, I really been liking first one recently it looks cleaner for me

1 Like

This can vary a lot, depending on what you are using it for.

What I usually do to keep everything clean is:

CommandsManager.Messages = {
	NoPermission 		= 'You do not have access to execute this command! Required rank is %s.',
	NotHighEnoughRank 	= 'You do not have high enough rank to do that!',
	Error 				= 'Unable to execute command! Contact a developer for more information.',
	NoPlayerFound		= '%s does not exist!',
	WrongUsage			= 'Wrong usage! Use %s.',
	TooManyRequests		= 'Slow down! Too many requests are being made against this key.',
	AlreadyBanned		= '%s is already banned!',
}

Another example for functions:

CommandsManager.Functions = {
	Alert = function(Player, String)
		local Speaker = ChatService:GetSpeaker(Player.Name)
		if (Speaker)
		then
			Speaker:SendSystemMessage(String, CommandsManager.Settings.Channel)
		end
	end
}
1 Like

Huh that’s a cool way to do it. I have never seen people do that

1 Like