How To Refactor – Tips
Published in
2 min readMar 6, 2017
- Draw a graph of the call stacks of all functions
- Figure out the service you want to create and the right abstractions
- To avoid merge conflicts, always try to create stuff in new files first
- Keep the old code there to run and test it parellel
- (req,res) should only be there in the API code. No where else should we be looking at them
- Always have APIs do ZERO logic. All they should do is validate, parse, transform data. Thats it!
- Ensure you have error-logic in your code. best places to check are when you have a data-base call made, or param validation.
- Your return objects should always have a defined schema. return null if need be, atleast the schema would be clear as to what is to be returned vs whats returned. no unexpected behaviour.
- TIP: promise nesting — strict no-no. never have a promise return inside a promise. that is an indication of you breaking functional paradigm. you are nesting logic.
- TIP: avoid mutating passed-in parameter objects. always use deep-clone, or a new object.
- TIP: using _.forEach, Map, Filter instead of for loop
- TIP: abstracting out code blocks — 1 line description, input schema, output schema
- TIP: abstract code into logical function blocks
- TIP: important – calling a property/variable from a parent scope is dangerous. You don’t know who else has called it and made amends. While composing function / call chains, its a good practice to explicitly pass down the properties that you will use in the function’s scope as parameters. This keeps the code functional , predictable and easier to debug.