What's the better practice with modules?

I would like to know which way is better for using modules scripts:

  1. Having some sort of loader that will require the module for me without any influence without a guaranteed order
  2. Requiring them manually and then calling :init() in a certain order

I feel like 2. might be better since you have full control over which module loads first, but I’m not sure if its that necessary.

just

local Ordered = { ModuleA, ModuleB, ModuleC }

local function Init()
    for _, Module in next, Ordered do
        local required = require(Module)
        if required.init then task.spawn(required.init) end
    end
end

Init()
1 Like

What is your use case? You’re not going to get type-checking information out of dynamically-required modules, which makes actually working with those modules a pain and generally hurts the viability of a “module loader” pattern.

Ideally, your modules would be written such that they can be required in any order, as this allows you to more freely utilize modules within one-another, and each system can handle its own initialization internally. You’re unlikely to be able to get everything to play nice this way, in which case I’d recommend defining a specific boot sequence like in your second option/@ChiDj123’s suggestion, but you should review your code to see where you can break any ordering requirements if at all possible.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.