I have some topic to discuss. I have a fragment of code with 24 if
s/elif
s. Operation
is my own class that represents functionality similar to Enum
.
Here is a fragment of code:
if operation == Operation.START:
strategy = strategy_objects.StartObject()
elif operation == Operation.STOP:
strategy = strategy_objects.StopObject()
elif operation == Operation.STATUS:
strategy = strategy_objects.StatusObject()
(...)
I have concerns from readability point of view. Is is better to change it into 24 classes and use polymorphism? I am not convinced that it will make my code maintainable... From one hand those if
s are pretty clear and it shouldn't be hard to follow, on the other hand there are too many if
s.
My question is rather general, however I'm writing code in Python so I cannot use constructions like switch
.
What do you think?
UPDATE:
One important thing is that StartObject()
, StopObject()
and StatusObject()
are constructors and I wanted to assign an object to strategy
reference.
case
statement, instead? that or have an array of method references to call dynamically, e.g. whatever python's equivalent ofarray_of_methods[operation]()
would be... – Gonfanon