Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

12
  • 6
    #1 is a pretty ugly API, though. If I decided to make a class for this, I'd probably make a single public function that delegates to the class and make the whole class private. ThingDoer(var1, var2).do_it() vs. do_thing(var1, var2). Commented Sep 12, 2015 at 19:06
  • 7
    While option 1 is a clear winner, I would go one step further. Instead of using internal object state, use local variables and method parameters. This makes the public function reentrant, which is safer. Commented Sep 12, 2015 at 19:30
  • 4
    One extra thing you could do in extension of option 1: make the resulting class protected (adding an underscore in front of the name), and then define a module-level function def do_thing(var1, var2): return _ThingDoer(var1, var2).run(), to make the external API a bit prettier. Commented Sep 12, 2015 at 19:43
  • 4
    I don't follow your reasoning for 1. The internal state is already hidden. Adding a class does not change that. I therefore don't see why you recommend (1). In fact it is not necessary to expose the existence of the class at all. Commented Sep 13, 2015 at 11:44
  • 3
    Any class that you instantiate and then immediately call a single method on and then never use again is a major code smell, to me. It's isomorphic to a simple function, only with obscured data flow (as the broken out pieced of the implementation communicate by assigning variables on the throwaway instance instead of passing parameters). If your internal functions take so many parameters that the calls are complex and unwieldy the code doesn't get less complicated when you hide that dataflow! Commented Sep 14, 2015 at 8:13