When not to refactor code, when to do it, and how much effort to put into it are all questions programmers deal with on ongoing basis.
This article from wealthfront engineering team provides an interesting take.
https://eng.wealthfront.com/2021/05/17/refactoring-production-code/
The blog itself is worth following if you’re into this type of content.
Marc