Hello, I have a question about best practices in modular programming.
I currently have split my code into module scripts, each with its own purpose. But sometimes I find that I have a module script that doesn’t need to expose itself to other modules. It does not need initialization or any public functions. All it has are functions and a few connections to remote events.
Normally, for a module script to run for the first time and connect its events (if it has any), it would have to be required by a different module. But since this particular module that I was talking about does not have any connection to anything else, it would not be initialized since nothing is requiring it.
Let’s assume that I would have many more cases like this in my code. What’s the best practice for handling something like this? Would my main server script have to loop through all modules and require them?
I’m kind of confused by this, but what I think you’re saying is that you have modules that run some code when first required, but is not used other than that. If that is a case then you could just use a normal script, or just put all of the code into one module, possibly as separate functions.
That is really only useful when your using only one each for direct control over when things are happening. If a script needs absolutely 0 external information then having it in a module script is kind of pointless. The only reason I ever do that is for consistency with how some of my code is set up. There is practically no difference between the 2 methods. If it needs to just run though, putting it in a script will work just as well if not better because then an unexpected error elsewhere won’t affect its ability to run.
If your script needs another script to run before it, then requiring them in that order would make sense. How you require them specifically depends on implementation. You can require them all directly if you need a specific load order. You could also write a function that will go and require all your modules. You could put them in a coroutine if you don’t need the order information. It really just depends on how and when you need them initialized.