DEV Community

Discussion on: Please refactor your code.

Collapse
 
rvxlab profile image
RVxLab

Be careful though, always go refactor with baby steps.

And for the love of god write tests. Having tests is the safest way to go around this, if your refactor breaks something your tests will catch it.

Collapse
 
dumazy profile image
Fré Dumazy

Big fan of writing tests and these are a blessing for refactoring, but there are some considerations to make.

  • Not all areas of the code are easy to test. UI is mostly harder to test than business logic, for example, and tends to change relatively fast, which requires updating the tests. In some cases, the effort vs confidence balance will be better with manual testing.
  • writing tests after a feature was made could be misleading (and boring also). The test would be written to validate the current state of things, instead of the requirement. There could always be a bug in the original code that hasn't been spotted, and you end up with a test that falsly says it's working fine.
  • To be able to test code, the code has to be written in a way that made it testable. Mostly this means having it "loosely coupled". Changing this during a refactor might lead you down a rabbit hole.
  • Writing tests is a practice that takes years to master. It's an investment in the long-run, one that might outlive the specific project or only give advantage after the deadline.

As always in IT, it's a trade-off. It's not "write tests" vs "don't write tests". It's about finding the balance between "effort" and "confidence" that's most fitting for the problem at hand.