I have this huge typescript service, with many helper methods. I would like to make the service class code cleaner and separate the helper functions into a separate ts file. What is the best approach to achieve this? I am using Typescript 4.3.5 or higher.
CodePudding user response:
You have to do decomposition. Following the SOLID principle, you can find that your service includes functionality for different sub-tasks groups. After you determine a sub-task groups, you can make sub-services that will make your code follow the single-responsibility rule.
Also, It may be useful to check the design pattern Facade, it shows principles of how to create an interface for a big system of services.
CodePudding user response:
Step 1
Identify methods that are "clean", don't use service state and have no side effects.
Example:
function sum(a,b){return a b;}
Step 2
Move them out of the service but keep at the same file.
This will allow you to save your progress without breaking to code base.
Step 3
Create a file helpers.ts
and move all of them there. If you find that this helpers are useful for other parts of the code - move higher. Don't forget to update references.
Step 4
Test your helpers ;)