I hope the code you work with is more like ravioli instead of spaghetti. I heard that from John Papa years ago. Most of the code we will work with is already existing. Let’s make it better as we work in it.
"Indeed, the ratio of time spent reading versus writing is well over 10 to 1. We are constantly reading old code as part of the effort to write new code. ...[Therefore,] making it easy to read makes it easier to write."
I had a an experience lately that prompted me to write this article. I recently had a bug where I started in on it, then 52 commits later I had broken things. This code wasn’t the best laid out and there were a lot of complicated user interactions. The risk was too great. I realized I had to get the value out of my work, then go back and fix the issue.
It took me a long time to recognize that I needed to do the refactoring earlier.
I ended up making a new branch, cherry-picking and copying in code to a point. Then I added the tests where I should have, then slowly moved the code forward. I made a PR out of that, then went back to fix the bug at hand.
Are there existing tests?
Count yourself blessed. Look for missing code coverage, if none, then refactor with less fear.
Are there 0 or minimal tests?
“If you have time to fix it later, you have time to make it right the first time” ~ A co-worker’s mother
This is extremely important if you find you’ve broken something and need to go back. Writing tests before changing code will help you avoid having to go back.
Adding new tests and refactoring is creating value. You want other developers to be able to use this as soon as possible and avoid merge conflicts. Writing tests first to maintain the current functionality will give you confidence to check in more often.
Please consider using Brave and adding me to your payment ledger. Then you won't have to see ads!