Class with too many parameters: better design strategy?

后端 未结 13 934
醉酒成梦
醉酒成梦 2020-12-04 08:29

I am working with models of neurons. One class I am designing is a cell class which is a topological description of a neuron (several compartments connected together). It ha

13条回答
  •  粉色の甜心
    2020-12-04 08:36

    In my opinion, in your case the easy solution is to pass higher order objects as parameter.

    For example, in your __init__ you have a DendriticTree that uses several arguments from your main class LayerV:

    main_apical_dendrite = DendriticTree(
        bifibs=apical_bifibs,
        first_sec_L=apical_sec1_L,
        L_sigma=L_sigma,
        L_decrease_factor=ldecf,
        first_sec_d=9, 
        branch_prob=apical_branch_prob
    )
    

    Instead of passing these 6 arguments to your LayerV you would pass the DendriticTree object directly (thus saving 5 arguments).

    You probably want to have this values accessible everywhere, therefore you will have to save this DendriticTree:

    class LayerV(__Cell):
        def __init__(self, main_apical_dendrite, ...):
            self.main_apical_dendrite = main_apical_dendrite        
    

    If you want to have a default value too, you can have:

    class LayerV(__Cell):
        def __init__(self, main_apical_dendrite=None, ...):
            self.main_apical_dendrite = main_apical_dendrite or DendriticTree()
    

    This way you delegate what the default DendriticTree should be to the class dedicated to that matter instead of having this logic in the higher order class that LayerV.

    Finally, when you need to access the apical_bifibs you used to pass to LayerV you just access it via self.main_apical_dendrite.bifibs.

    In general, even if the class you are creating is not a clear composition of several classes, your goal is to find a logical way to split your parameters. Not only to make your code cleaner, but mostly to help people understand what these parameter will be used for. In the extreme cases where you can't split them, I think it's totally ok to have a class with that many parameters. If there is no clear way to split arguments, then you'll probably end up with something even less clear than a list of 15 arguments.

    If you feel like creating a class to group parameters together is overkill, then you can simply use collections.namedtuple which can have default values as shown here.

提交回复
热议问题