Imports good practice ?

Monkey Forums/Monkey Programming/Imports good practice ?

Rushino(Posted 2013) [#1]
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!


ziggy(Posted 2013) [#2]
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.


Rushino(Posted 2013) [#3]
Alright thanks!


MikeHart(Posted 2013) [#4]
If you don't use reflection on a module, then only code that gets called will make it into the published project.


Rushino(Posted 2013) [#5]
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.


Tibit(Posted 2013) [#6]
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 :)


Rushino(Posted 2013) [#7]
Tibit: What you mean by Loads ? Using many import can increase loads and more ? in the editor or the game ?


Skn3(Posted 2013) [#8]
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.


Belimoth(Posted 2013) [#9]
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.


Samah(Posted 2013) [#10]
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?


Belimoth(Posted 2013) [#11]
That is a good idea.


Skn3(Posted 2013) [#12]
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.


Samah(Posted 2013) [#13]
To remove all dependencies I'd have to strip out all the exception handling, and I'm not willing to do that.


Skn3(Posted 2013) [#14]
Would be nice if the preprocessor could do something like:
#IF HasModule("exceptions")
ThrowException()
#END



Samah(Posted 2013) [#15]
Feature request then? ;)


Rushino(Posted 2013) [#16]
Definitivly i guess there some work that should be done in the module area.


Rushino(Posted 2013) [#17]
@Ziggy So if the project is rather big using JungleIDE... do you suggest to use the "only import necessary files" ?