6

My code has 2 python methods defined, m1 and m2. m1 receives 6 arguments - p1,p2,p3...p6. It uses p1 in its own code, but passes p2-p6 to m2. Is there a recommended programming style here to prevent the definition of m1 from getting too verbose?

Is this a good scenario to use **kwargs in?

5
  • 1
    Just as a general comment, there's rarely (if ever) a best way to do something. There are typically a lot of (perhaps infinite) bad ways to do something and a handful of good ways.
    – JimmyJames
    Sep 29 at 20:31
  • I would see this as a code smell, and the appropriate way of fixing is not applying some rule or recipe, but analyzing and fixing the stink. Sep 30 at 6:14
  • 1
    Are m1 and m2 always linked like this? Or is m2 sometimes called separately, without calling m1 first? Sep 30 at 19:23
  • @JohnGordon As of now they are linked like this but it is possible that in the future m2 could be called from a context besides m1.
    – str31
    Sep 30 at 21:36
  • 2
    kwargs could be helpful but it also makes code hard to follow as actual parameters are hidden in deeper layers. It’s powerful but don’t reach for it “just” to clean up m1’s signature vis a vis m2, that’s not doing the callers any real favors.
    – JL Peyret
    Oct 1 at 5:02

3 Answers 3

16

Is there a recommended programming style here to prevent the definition of m1 from getting too verbose?

Sort of. The style is called "working in small iterations, doing regular refactorings, ideally supported by unit tests".

Fowler's refactoring catalogue contains some refactorings which can help to reduce the number of parameters, introducing parameter object is one of them. Another technique which is sometimes applicable is to combine functions into a class, so the class provides member attributes which can be accessed from every member function without being explicitly passed around.

Still, from anonymous names like m1 or p1,p2, ... it is impossible to say which of those refactorings make most sense for your specific situation (if any), or how to apply them exactly.

3
  • 8
    Indeed. The parameter object shouldn't just be assumed to be p2-p6. Look at what they are and ask if they really go together. Might be p2 & p3 go well together and while p4-p6 don't go well with them they also go well with each other. So fine, make two parameter objects. But don't just blindly think about what needs what. Think about what these things are. Sep 29 at 20:10
  • Without examples showing the techniques, this looks dangerously close to a link-only answer.
    – J. Mini
    Sep 30 at 12:15
  • 3
    @J.Mini I think that we can't give anymore than this as the question needs to provide more details. See the last paragraph of the answer
    – mmmmmm
    Sep 30 at 13:12
5

Create a data object with those grouped parameters. And then pass the data object between functions.

3
  • I've heard this called a "Parameter Bag", though there is probably a different name for it.
    – StingyJack
    Sep 30 at 14:37
  • 1
    I've heard this derogatively called a "big ball of state"
    – benxyzzy
    Oct 1 at 7:48
  • 3
    Applied unthinkingly, this is strictly worse than passing multiple parameters. The grouped parameters must have some semantic connection besides being the parameters to m2. Oct 1 at 12:42
2

Your description does not make it clear what m2 is. But if m2 is some kind of callback method, you could pass in a lambda where all the parameters p2-p6 are already bound to the passed in method.

In a way thats a variant on parameter objects.

1
  • 2
    …or in OOP style, pass an instance of a class with m2 as a parameter-less method and all the argument values as fields of that object
    – Bergi
    Sep 30 at 22:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.