I have a class that I use for data analysis. Generally I need to create a couple instances of the class and then run a series of methods on each. The methods I need to run will vary from instance to instance.
Today my code (using the class) usually looks something like this:
object_alpha = MyClass()
object_alpha.method1()
object_alpha.method2()
object_alpha.method3(arg1)
object_alpha.method4()
object_bravo = MyClass()
object_bravo.method1()
object_bravo.method3(arg1)
object_bravo.method4()
object_bravo.method5()
I know the above example is not an example of method chaining. Method chaining is not currently possible because the methods do not return an object of the class.
This format gets a bit repetitive a tedious, especially with long descriptive variable names. My primary complaint is that I do not find it very readable.
I thought about changing my class to return a new object from each method call, so that I could do method chaining. But the side-effect of the methods do not change the class--they are making changes to a database via an API, so it feels strange to return a new object.
My thought is to create a runner class that would take a list of method names as strings. So that I could do something like this.
object_alpha = MyClass().runner([
'method1',
'method2',
'method3(arg1)',
'method4'
])
object_bravo = MyClass().runner([
'method1',
'method3(arg1)',
'method4',
'method5'
])
Is this a bad idea; is there a better approach?