Use a Module or Class?

  1. 6 years ago

    Eugene D

    4 Aug 2013 Pre-Release Testers, Xojo Pro Canada scispec.ca

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

    Sincerely,

    Eugene

  2. Dwane M

    4 Aug 2013 Pre-Release Testers, Xojo Pro

    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.

  3. Christian S

    4 Aug 2013 Pre-Release Testers, Xojo Pro, XDC Speakers Germany

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

  4. Jeremy C

    4 Aug 2013 Pre-Release Testers, Xojo Pro Ohio, USA

    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.

  5. Norman P

    4 Aug 2013 Xojo Inc, Pre-Release Testers, Xojo Pro Seeking work. npalardy@great-w...

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

    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.

  6. Richard D

    5 Aug 2013 Pre-Release Testers, Xojo Pro Europe (UK, London)

    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??

  7. Richard D

    5 Aug 2013 Pre-Release Testers, Xojo Pro Europe (UK, London)

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

  8. 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.

  9. William J

    5 Aug 2013 Pre-Release Testers

    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.

  10. Paul L

    5 Aug 2013 Xojo Inc http://docs.xojo.com

    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
  11. 2 years ago

    Djamel A

    5 Jan 2018 Oran (Algeria)

    @Eugene D 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 :)

    Sincerely,

    Eugene

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

  12. Joost R

    5 Jan 2018 Pre-Release Testers, Xojo Pro The Netherlands

    @Richard D And i use to creating global variable

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

  13. Tim J

    6 Jan 2018 Pre-Release Testers, Xojo Pro Dehydrating in AZ
    Edited 2 years ago

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

    Well, everything I do is exceptional, so ... :P

    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.

  14. Markus W

    7 Jan 2018 Pre-Release Testers #JeSuisHuman New Zealand, Auc...

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

    ??? 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)

  15. Derk J

    7 Jan 2018 Pre-Release Testers, Xojo Pro
    Edited 2 years ago

    @Markus W ??? 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)

    I’d make those constants instead of properties

or Sign Up to reply!