tags:

views:

85

answers:

4

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?

+1  A: 

Idioms of languages are different and thats the reason a problem solved in different languages take different approaches.

  1. "C" is all about procedural decomposition.
  2. Main idiom in Java is about "class or Object" decomposition. Functions are not absent, but they become a part of exhibited behavior of these classes.
  3. "Python" provides support for both Class based problem decomposition as well as procedural based.

All of these uses files, packages or modules as concept for organizing large code pieces together. There is nothing that restricts you to have one module for one knowledge domain. These are decomposition and organizing techniques and can be applied based on the problem at hand.

If you are comfortable with OO, you should be able to use it very well in Python.

pyfunc
>If you are comfortable with OO, you should be able to use it very >well in Python.In Java, one file <-> one public class. It seems that application of this rule is very ugly in Python. In Python one file <-> one small task, I think.
Nikita Kalinin aka nikaan
@Nikita Not so. In python (often) one file <-> collection of many related small tasks and maybe a few big ones.
aaronasterling
The organization of code is totally dependent on coder. The rules are not set in stone. Typically, if there are small multiple classes , they usual habit is to club them in a single file, shall I say module. If classes are large, they are usually segregated into multiple files. Python has package as a way of grouping these files.I guess the flexibility allows coders to identify whats suitable to them.
pyfunc
+1  A: 

VBA also allows the use of classes. Unfortunately, those classes don't support all the features of a full-fleged object oriented language. Especially inheritance is not supported.

But you can work with interfaces, at least up to a certain degree.

I only used modules like "one module = one singleton". My modules contain "static" or even stateless methods. So in my opinion a VBa module is not namespace. More often a bunch of classes and modules would form a "namespace". I often create a new project (DLL, DVB or something similar) for such a "namespace".

WizzardsApprentice
Yes, singleton is right concept in this context! So, something is module level function if it is singleton function.
Nikita Kalinin aka nikaan
A: 

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.

aaronasterling
yes it is very cool that in python I can whiteif a: import a_handlerelse: import b_handlerbut in VB I can't do it :( Do you know example of module level function which is not a singleton function (see other answers)?
Nikita Kalinin aka nikaan
@Nikita I don't know what you mean by singleton function. A singleton is one (seldom good) way of thinking about a module but I think that you're trying to force OO concepts on a non OO idiom.
aaronasterling
+1  A: 

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.

Bart
Yes, I understood. I have opposite situation - there is big add-in on VBA, excel, approximately of 1000 line at every of 10 modules and there is no classes at all :) But the next improvement demands 5 case of very similar behaviour and I want to introduce classes. It is more difficult than in Java :) VBA prohibits many things. Oh! Do you know some IDE for VBA? Or add-ins with syntax highlighting, for example?
Nikita Kalinin aka nikaan
Well, IDE embedded in Excel isn't as bad as it appears on first sight. I'm writing in 2003 edition and it has syntax highlighting and some ancient pre intelli-sense system ;-) Debugger is also reasonable. If you get stuck with something I will try to help. During that time I've got some exp.
Bart
thanks! I used to try 2003 and 2007. I have got accustomed for eclipse etc - what about highlighting similar notepad++?(when I select one word, all the same word are hightlighted..) Or call hierarchy? Because, for example, search for 'some' finds not only 'some' entities but 'someWho', 'handsome' and so on. Call hierarchy is very helpy if you are using global variables :) mmm. Also I have problem with formatting code - haw can I tab or indent big piece of code? May be, what can I help you - svn tool for VBA (http://www.codeproject.com/KB/office/SourceTools.aspx)
Nikita Kalinin aka nikaan
Thanks for that link. It is really interesting concept :-) I know how hart is to work without source control when you get used to it.Unfortunatelly I work for huge company and I can only use predefined environment. Installing something new as notepad++ is prohibited. And we are using MS Team Foundation Server, so no SVN.
Bart