Python – Decoupling Components – Design

designpython

I have created a a number of non-trivial scripts in Python that do some long running reporting. At first these reports started off as scripts with a config file. Then I added in the a database component to persist data. Now I'm building a GUI to sit on top of all of them.

My question relates to design. Each time I add an additional requirement, I find myself wishing I had decoupled the components of these scripts more than I did. When I consider how to recfactor my code to be more modular, I find that I need to pass quite a few references around the code, to the point that class initializers and function signatures have a ton of parameters. Also I find myself having parameters that are simply "pass-through", in that one object needs them only to pass to another object, doing nothing with them.

What is the best way to approach modularity in Python while minimizing the need to pass references all over the place?

I understand this is very conceptual and will vary by design, but I'm looking for guiding principles.

Best Answer

I don't tend to run into this problem when I'm trying to modularize. So clearly I am doing something different, but without seeing what you're doing, I can only guess what.

So I'll limit myself to general platitudes.

First of all, when writing a number of related scripts, it is usually best to build small scripts and a common library. This type of design tends to survive multiple refactors better.

Second, if you have a lot of parameters to a function, use named parameters and sensible defaults. (See http://docs.python.org/release/1.5.1p1/tut/keywordArgs.html if you don't know how to use named parameters.)

Third, there is nothing wrong with having methods in classes that just pass stuff through to another class. As long as the classes really serve different purposes, that is fine.

Fourth, there is everything wrong with having a ton of classes around simply because it serves some aesthetic purpose. For each and every class, unless you can explain exactly what having that class is doing to make your life easier, you can remove it.

Fifth, the book Code Complete has a tremendous amount of very detailed advice on everything from the proper naming of variables to effective modularization. If you have not worked your way through it, I highly recommend that exercise.

Sixth, if you can you really, really want a mentor that you trust to start reviewing your code. I would be willing to bet that what you most need to be taught is something you would never see (which is why you need help with it) which is fairly obvious to a more experienced person. Teaching someone without being able to see what they are doing is no more effective than driving a car without being able to see the road.

Related Topic