Is there are good DRY fix for this painful design pattern?

Ben Finney ben+python at benfinney.id.au
Tue Feb 27 00:36:59 EST 2018


Steven D'Aprano <steve+comp.lang.python at pearwood.info> writes:

> Interesting, I'll certainly have a good read of that, thanks. But I
> fear I mislead you: you seem to have understood that either all the
> parameters are None, or none of them are, whereas what I meant was
> that they can vary independently of each other.

I think that issue – independent defaults for the values – is
orthogonal.

For avoiding the large set of parameters repeated everywhere, you
evidently consider that cluster of values a single concept, to be passed
around as a unit.

That's what the “Introduce Parameter Object” refactor operation is for:
Now that you recognise this cluster is an implicit conceptual unit that
different methods share, define an explicit type for it, and pass around
an instance of that between the functions.

> So it isn't enough to check whether the Parameter object is None, I'd
> need to check each field within the object.

No problem. You can do that within the methods, as before.
Alternatively, you can define it as behaviour of the type. The
“Introduce Parameter Object” refactor leaves that and other options
open, because it's an unrelated issue.

> On the gripping hand, I *think* that means the caller is responsible
> for creating and populating the Parameter object, instead of just
> passing keyword arguments... let me think about that one.

One good effect of this refactor is you're forced to explicitly *name*
this concept of cluster-of-values-with-some-specific-meaning. Once you
have a name – which will confont you with a decision about the meaning
of this specific cluster of values – you then get to thinking about how
the values should be initialised, etc.

All of that is hidden from the methods that just want to pass around the
cluster of values to each other. You then get to freely change it while
development continues.

> Either way, thanks for the suggestion.

Happy hacking.

-- 
 \        “I think it would be a good idea.” —Mohandas K. Gandhi (when |
  `\                    asked what he thought of Western civilization) |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list