After programming for many years, I am not sure when it is ‘appropriate’ to use a Module or a Class? I realize this is not a ‘black and white’ question, so I am open to your thoughts. An example would be nice (no code needed or required).
I see a module as a great place for application specific properties and methods that are widely accessible. A class is a component of code that can be reused again and again in different parts of the program or in many programs. I might create a global property in a module for an instance of a class to be used in anywhere in the program. I.e. I have a settings class that handles all my common database I/O and I can use it across any of my applications. I then have a property in a module that I initialize when the application opens. This lets me reference that class from anywhere in my application. I can fix any issues in that one class and recompile any apps that use it and it’s done in one place. Classes are also well suited for threaded operations, as you can create a new instance of the class for each thread and you don’t have to work about things like if you were trying to use a common module etc.
There are no hard fast rules. I never really thought about it much, but a few that I go by:
If I want to use multiple instances of an object, for example a Person, that would be a class with FirstName, LastName, etc… properties. You wouldn’t create a module for that.
If the logical grouping of code must share a lot of state data, use a class. For example, a parser that has to deal parse lines of code, tracking current line/column positions, index positioning, a token list/tree, etc…
If you see yourself expanding the code, inheriting from a base structure making something more complex, go with a class. For example, a Shape class that is the basis later for a Circle, Square, Triangle.
I am sure there are others, but if it doesn’t fit the above, I’d look into using a module.
[quote=25062:@Christian Schmitz]a class with only shared methods, constants and shared properties is a module
(very similar at least)[/quote]
Not really
While it might behave as a module you CAN still create instances unless you explicitly block that by making the default constructor private.
I have a table called tblCompany with lots of boolean field for print option used in price list, invoice etc. Currently i simply open the tblCompany and read the options i need and then use it for creation of the price list report.
I haven’t write a class in Xojo or RS all myself and wondering is this a good class to write?? will it speed up if i use classes instead opening the table. What about a dictionary??
If you’re writing something that could be logically grouped together (Functions with specific data) and that “group” is likely to have several instances, use classes. If it’s a flat function /works on something regardless, you’d generally use a module.
You Might put math functions in a module as they would work with any value and be applicable to many legitimate uses without the need to class together the data with the function.
On the other hand, a GUI control is a prime example for when you use classes - You need multiple instance of a control (typically), they need to have data specific to that instance (width, height, etc) and functions specific to that control + they all need to be active in memory at the same time.
The real difference really is there will be a little overhead in creating a class, since you have to instance it, where a module doesn’t require that - so if you created a class that would only ever have one instance at a time and you created a module that did the same but simply swapped out the data it was working on, the module would gain a slight performance advantage vs the constant creating and deleting of class isntances
You could “just swap out” the data in the class instance too, but the OOP concept is against that. You’re not really supposed to access global data with them either, you’re supposed to “pass” data /objects to the instances that need it rather than dump it to a global location and point functions to that global location. Which is all about keeping data strictly in what it’s needed, protecting it from anything that could access and damage it.
After programming for many years, I am not sure when it is ‘appropriate’ to use a Module or a Class? I realize this is not a ‘black and white’ question, so I am open to your thoughts. An example would be nice (no code needed or required).
Thanks for your comments
Sincerely,
Eugene[/quote]
Migrate from MS VB6 to Xojo all my applications converted into Xojo does not use Class, only Module.
More seriously, to the best of my knowledge, a module can contain classes, but I don’t know of a way to have a class contain a module.
I look at classes as “structs with super processing powers”. That’s what Bjarne and Tim B.L. introduced as an explanation back in 1986 or so when I discussed it with them on comp.lang.c++. I build a class as I would a struct in C. I then add the methods/functions that are required to manipulate the data in the struct.
??? What does that mean? You shouldnt use them in normal programming?
They should be used sparingly, but they have their place. For example global variables should be used when they can be used in different places (like colours), and one can simplify their use and control their proliferation by grouping them in modules according to type (eg Colours.red, Colours.blue, etc)
[quote=367909:@Markus Winter]??? What does that mean? You shouldnt use them in normal programming?
They should be used sparingly, but they have their place. For example global variables should be used when they can be used in different places (like colours), and one can simplify their use and control their proliferation by grouping them in modules according to type (eg Colours.red, Colours.blue, etc)[/quote]