When do I know it's time to refactor code?
Refactoring is different for every person but for me I follow these guidelines:
- If I don't know / understand the code, I don't refactor. Period!
- Is the code broken? If not then I generally don't refactor
- Can the code be made much simpler? If the amount of code can be reduced by 2x and made cleaner / simpler then I may refactor.
- Can the existing code handle the new feature / bug fix? If not then I may refactor.
- I will not refactor the code if I think I have a better way. Better my be for me but not other developers. I leave working code working.
Tests:
I never assume that tests for existing code:
- Has complete code coverage (I've never seen this yet). I've seen 100% coverage but not all combinations of logic or possible side effects of API changes, etc.
- That the tests are even correct. The tests only test what the developer wanted to test, not what could / should happen. Tests are never free from bugs just like the code that is being tested. If there are x% of bugs for every 1,000 lines of code then the tests will also contain basically the same rate of bugs.
For these reasons, I never rely on tests for catch bugs when I refactoring code. I need to fully understand the logic and intension of the code before I refactor. Otherwise I'll just create additional bugs and duplicate failed logic in the new code.
For me, I try to make sure that my code is as solid, understandable and as simple as possible. If code is not understandable then it's at rick of being churned by refactoring.
Refactoring is different for every person but for me I follow these guidelines:
- If I don't know / understand the code, I don't refactor. Period!
- Is the code broken? If not then I generally don't refactor
- Can the code be made much simpler? If the amount of code can be reduced by 2x and made cleaner / simpler then I may refactor.
- Can the existing code handle the new feature / bug fix? If not then I may refactor.
- I will not refactor the code if I think I have a better way. Better my be for me but not other developers. I leave working code working.
Tests:
I never assume that tests for existing code:
- Has complete code coverage (I've never seen this yet). I've seen 100% coverage but not all combinations of logic or possible side effects of API changes, etc.
- That the tests are even correct. The tests only test what the developer wanted to test, not what could / should happen. Tests are never free from bugs just like the code that is being tested. If there are x% of bugs for every 1,000 lines of code then the tests will also contain basically the same rate of bugs.
For these reasons, I never rely on tests for catch bugs when I refactoring code. I need to fully understand the logic and intension of the code before I refactor. Otherwise I'll just create additional bugs and duplicate failed logic in the new code.
For me, I try to make sure that my code is as solid, understandable and as simple as possible. If code is not understandable then it's at rick of being churned by refactoring.
Comments
Post a Comment