I have code that works, but it is getting harder to understand. How do I know when cleanup is worth the risk?
1
Leave it alone if the code is stable, isolated, and rarely touched. Refactor when repeated edits keep making you reread the same confusing branches.
2
Refactor when the function has more than one reason to change or when adding the next feature requires copying logic. Keep the behavior covered before you move code around.
0