Imports good practice ?
Monkey Forums/Monkey Programming/Imports good practice ?
| ||
Hello! Just wanted to know. It is good practice to put all imports in a file and import this actual file in all others classes seperates in different files ? Thanks! |
| ||
mmmm, it works but I would recommend to keep your code as modular as possible. If something does not require something to compile, don't make it import it. This can help making code more reusable and self-contained. |
| ||
Alright thanks! |
| ||
If you don't use reflection on a module, then only code that gets called will make it into the published project. |
| ||
So do you saying it doesnt matter if i do it the way i am doing it? I use reflection but only in one module so i can put this only there for this one. |
| ||
Because of Monkey's "only translate what is used" I tend to import everything if using a framework. However the benefit to use imports in each file is that it tells you spot on all the dependencies of that file. If you start to have loads, then maybe it could be a good time to think about the architecture :) |
| ||
Tibit: What you mean by Loads ? Using many import can increase loads and more ? in the editor or the game ? |
| ||
In a project I will import EVERYTHING in the main file and then each import will simply import the main project file. E.G. project.monkey Import classes.apple Import classes.pears classes/apple.monkey Import project I like to split each class into its own import and then have seperate imports for things like helper functions, constants/globals and setup routines. Instead of making all encompassing frameworks it's more sensible to compartmentalise code so things can run standalone. It's nice to be able to quickly import say an XML module without having to import an entire framework. |
| ||
For framework and module stuff I only import what is used in a particular file, but for games I just have everything Import the main file. |
| ||
I make each file import only what it needs to use. You'll soon find out if it needs more when you go to compile it. Skn3: It's nice to be able to quickly import say an XML module without having to import an entire framework. I'm thinking of stripping out the requirement of ArrayList and just replacing it with Stack or an array. Would that be alright? |
| ||
That is a good idea. |
| ||
I'm thinking of stripping out the requirement of ArrayList and just replacing it with Stack or an array. Would that be alright? I think as diddy is a framework and it is fine to rely on itself. Your arraylist is nice definitely worth keeping! It isn't a bad thing it is just sometimes for someone like me I tend to get a bit funny when I don't know exactly what gets imported along with a lib. A bit like the FreeImage module for blitzmax comes with its own PNG loaders which can play havoc with crashes if you happen to also import blitzmax png loader. |
| ||
To remove all dependencies I'd have to strip out all the exception handling, and I'm not willing to do that. |
| ||
Would be nice if the preprocessor could do something like:#IF HasModule("exceptions") ThrowException() #END |
| ||
Feature request then? ;) |
| ||
Definitivly i guess there some work that should be done in the module area. |
| ||
@Ziggy So if the project is rather big using JungleIDE... do you suggest to use the "only import necessary files" ? |