Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Module vs object-oriented programming in vba

Tags:

python

vba

My first "serious" language was Java, so I have comprehended object-oriented programming in sense that elemental brick of program is a class. Now I write on VBA and Python. There are module languages and I am feeling persistent discomfort: I don't know how should I decompose program in a modules/classes.

I understand that one module corresponds to one knowledge domain, one module should ba able to test separately... Should I apprehend module as namespace(c++) only?

like image 820
nikaan Avatar asked Aug 31 '10 07:08

nikaan


People also ask

Can you do object-oriented programming in VBA?

Visual Basic provides full support for object-oriented programming including encapsulation, inheritance, and polymorphism.

What is a module in VBA?

VBA module is a “. bcf” extension file that holds the code in the visual basic editor. Each module has its own code window where you can write. You can insert a new module, delete, backup, and import it. In simple words, it's like a text file that you open in the notepad.

What is the difference between module and class module in Excel VBA?

The main difference between classes and modules is that classes can be instantiated as objects while standard modules cannot.

What is a module in object-oriented programming?

A module is a named collection of declarations (fields, methods, classes, interfaces, sub-modules, etc.) In object-oriented programming modules usually correspond to classes, packages, files, and components.


2 Answers

I don't do VBA but in python, modules are fundamental. As you say, the can be viewed as namespaces but they are also objects in their own right. They are not classes however, so you cannot inherit from them (at least not directly).

I find that it's a good rule to keep a module concerned with one domain area. The rule that I use for deciding if something is a module level function or a class method is to ask myself if it could meaningfully be used on any objects that satisfy the 'interface' that it's arguments take. If so, then I free it from a class hierarchy and make it a module level function. If its usefulness truly is restricted to a particular class hierarchy, then I make it a method.

If you need it work on all instances of a class hierarchy and you make it a module level function, just remember that all the the subclasses still need to implement the given interface with the given semantics. This is one of the tradeoffs of stepping away from methods: you can no longer make a slight modification and call super. On the other hand, if subclasses are likely to redefine the interface and its semantics, then maybe that particular class hierarchy isn't a very good abstraction and should be rethought.

like image 57
aaronasterling Avatar answered Oct 13 '22 23:10

aaronasterling


It is matter of taste. If you use modules your 'program' will be more procedural oriented. If you choose classes it will be more or less object oriented. I'm working with Excel for couple of months and personally I choose classes whenever I can because it is more comfortable to me. If you stop thinking about objects and think of them as Components you can use them with elegance. The main reason why I prefer classes is that you can have it more that one. You can't have two instances of module. It allows me use encapsulation and better code reuse.

For example let's assume that you like to have some kind of logger, to log actions that were done by your program during execution. You can write a module for that. It can have for example a global variable indicating on which particular sheet logging will be done. But consider the following hypothetical situation: your client wants you to include some fancy report generation functionality in your program. You are smart so you figure out that you can use your logging code to prepare them. But you can't do log and report simultaneously by one module. And you can with two instances of logging Component without any changes in their code.

like image 25
Bart Avatar answered Oct 13 '22 22:10

Bart