+ 2

Designing a problem solution

Hi All I have total 5 different work flows. Those are almost simillar apart from few conditions. For example, Workflow names are A, B, C, D and E. All are doing 3 tasks like Task T1, Task T2 and Task 3. Almost 90% code is common for all 5 workflows. Below is just overview of current implmentation [All workflow follows / call a single method called PerformWorkflow]: PerformWorkflow(): DoTaskT1: Common Code 1 if Workflow A , Follow values as xyz Common Code 2 with above values DoTaskT2: Common Code 3 if Workflow A , Follow values as xyz if Workflow B , Follow values as abc rest all Workflow , Follow values as xyz Common Code 4 with above values DoTaskT3: Common Code 5 if Workflow C , Follow values as xyz if Workflow D , Follow values as abc rest all Workflow, Follow values as xyz Common Code 6 with above values I feel that this is not a rigt way to do as all workflow has same method and it violates Single Responsibility principle... Is this observation correct? What should be ideal way to design this system?

23rd Oct 2022, 9:37 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
4 Answers
+ 3
It is a little difficult to understand your pseudo code example (maybe a bit too abstract). But I think this topic is extremely important and not much discussed in the Sololearn lessons or forums. Because when we think our code is finally running and doing what it should, it is time to REFACTOR. What you mention about SRP might be correct but difficult to judge at this level of view. The main principle behind design patterns, and common practices such as DRY, KISS, and so on, is the point that code should be easy to maintain. So if you examine your code, check which parts are shared and common, and which parts are unique. Common code should be extracted to separate functions / methods. If they depend on their context or on global variables, put those dependencies in the function arguments, so they can be called in different ways from various context. If you have a single point of entry for all workflows, then deciding which subtask to run, could be controlled by a WorkflowType enum.
28th Oct 2022, 10:56 AM
Tibor Santa
Tibor Santa - avatar
+ 2
The term "functional programming" is more about a set of design principles which make your programs more resilient, less prone to bugs, easily testable and better suited for concurrency. The "pure function" takes all of its inputs from arguments, and is deterministic, it cannot have side effects, and always yields the same output from the same set of inputs. You can achieve this with free standing functions or with static methods as well, the really important point is what's inside :)
28th Oct 2022, 12:50 PM
Tibor Santa
Tibor Santa - avatar
+ 1
Thanks for insight Tibor Santa Got your point and have one query I have common functionality put it into function. Does this makes it a functional programming as we pass values of variable as function parameter ? Or static class is not a major drawback and can use these functions as static class member functions ?
28th Oct 2022, 12:23 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Thanks a lot âœŒïžđŸ‘
28th Oct 2022, 1:11 PM
Ketan Lalcheta
Ketan Lalcheta - avatar