lundi 23 mars 2015

Program design - how to improve it and how to avoid coupling (Python)?

In the Python program structure below there are two modules with functions.


Problem description:



  1. Functions are used inside other functions all over the place. Strong coupling (dependency). For example func1() in module 2 that reads from file is called many many times because it's called in almost every other function in both modules. That's resource and performance intensive, yet alone a bad practice.

  2. Reading from file so many times (each time funct1() is called) can be costly.


Question: What is a good way to improve this design?


For example, passing functions from helper module 2 to functions in module 1 AS PARAMETERS? [ If I do that, I still have functions in module 2 dependent upon each other (ie funct4() uses funcr1(), funct2(), funct3(), etc) ]


My paradigm I started with: I was thinking: "Ah, I'm going to have a main module where the main thing happens. Then I will have a side module for functions that do some prep work and clean-up and called them from module 1." (Well, now I have this mess.) I wonder if a good design is to make functions less dependent on each other? Each function does one thing and DOESN'T call other functions?? Should I use classes? How? Is it about when to use classes versus modules? Functional versus OO design?


How would you redesign this so as little coupling as possible and file is read only once (or as few times as possible)?


Thanks for any tips.



module 1 (main module, program execution happens here)


from module2 import *


f1() - uses funct1() 4 times - uses funct2() 2 times


f2() - uses func1() 2 times - uses func2() 2 times


f3() - uses func1() 1 time


f4() - uses func2() 3 times - uses func3() 3 times - uses func4() 1 time


f5() - uses func1() 2 times - uses func2() 2 times - uses func4() 4 times


f6() - uses func1() 3 times - uses func2() 3 times - uses func4() 2 times


module 2 (has helper functions that provide clean up and other


functionality to moduie 1)


func1() reads from file (with open('x.txt', 'r') af f: ...


func2() - uses func1 1 time


func3() - uses func1 1 time


func4() - uses func1() 1 time - uses func2() 2 times - uses func3() 1 time



Aucun commentaire:

Enregistrer un commentaire