Use a Module or Class?

Hello everyone,

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 :slight_smile:

Sincerely,

Eugene

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.

That’s my take on it anyway.

a class with only shared methods, constants and shared properties is a module :slight_smile:
(very similar at least)

There are no hard fast rules. I never really thought about it much, but a few that I go by:

  1. 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.
  2. 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…
  3. 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 :slight_smile:
(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??

And i use to creating global variable for all those print option and assigned them from the table when i login to the application.

Really depends on what you’re writing.

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.

If you need to use expressions then you might want a method in a module. I sometimes have a class and module method just for this reason.

Excessive use of modules is probably not the best idea for good object-oriented design.

Here’s what I suggest:

In general, modules should used sparingly in an object-oriented language such as Xojo. In most cases, you will probably be better served by a class.

With that said, here are some common (and valid) uses for modules:

  • Global constants, particularly for localization
  • Grouping project items into namespaces
  • Class extensions
  • Global properties and methods, but these should be minimal

[quote=25054:@Eugene Dakin]Hello everyone,

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 :slight_smile:

Sincerely,

Eugene[/quote]

Migrate from MS VB6 to Xojo all my applications converted into Xojo does not use Class, only Module.

I think global variables should only be used in exceptional cases.

Well, everything I do is exceptional, so … :stuck_out_tongue:

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 shouldn’t 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 shouldn’t 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]

I’d make those constants instead of properties