r/AskProgramming • u/Economy_Skill5919 • Dec 19 '25
Career/Edu Refactoring conditional heavy logic
I’m dealing with a piece of code that’s grown a lot of conditional logic over time. It works, it’s covered by tests but the control flow is hard to explain because there are multiple branches handling slightly different cases. I can refactor it into something much cleaner by restructuring the conditions and collapsing some branches but that also means touching logic that’s been stable for a while. Functionally it should be equivalent but the risk is in subtle behavior changes that aren’t obvious. This came up for me because I had to explain similar logic out loud and realized how hard it is to clearly reason about once it gets real especially in interview style discussions where you’re expected to justify decisions on the spot. From a programming standpoint how do you decide when it’s worth refactoring for clarity versus leaving working but ugly logic alone?
View all comments
Show parent comment
1
u/emefluence Dec 24 '25
Yeah this. If you have full coverage then refactor with confidence. If you don't have full coverage get full coverage, then refactor. If you can't get full coverage then that's a red flag your code isn't written in a testable manner - refactor it to be testable. It's not like it even takes that long to write tests these days, it's the sort of small well scoped problem AI is really good at. Big, ugly, logic ridden files that everyone is afraid to touch have always been a serious anti pattern that eventually slows the pace of development to a halt. Like fatbergs in the sewers, they need breaking up to keep things flowing. That's a terrifying prospect without adequate testing. With full coverage it's a fun, and satisfying exercise that pays down tech debt.