How To Refactor – Tips

Raghav Chandra
Urban Company – Engineering
2 min readMar 6, 2017
  1. Draw a graph of the call stacks of all functions
  2. Figure out the service you want to create and the right abstractions
  3. To avoid merge conflicts, always try to create stuff in new files first
  4. Keep the old code there to run and test it parellel
  5. (req,res) should only be there in the API code. No where else should we be looking at them
  6. Always have APIs do ZERO logic. All they should do is validate, parse, transform data. Thats it!
  7. Ensure you have error-logic in your code. best places to check are when you have a data-base call made, or param validation.
  8. 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.
  9. 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.
  10. TIP: avoid mutating passed-in parameter objects. always use deep-clone, or a new object.
  11. TIP: using _.forEach, Map, Filter instead of for loop
  12. TIP: abstracting out code blocks — 1 line description, input schema, output schema
  13. TIP: abstract code into logical function blocks
  14. 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.

--

--

Raghav Chandra
Urban Company – Engineering

co-founder @urbancompany_UC ( previously, @urbanclap ). engineer. ex @twitter @cal .