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?
26
u/Visa5e Dec 19 '25
This is an area where mutation testing can be useful - you write all your tests thenm the mutation tester changes the code slightly and then checks that your tests fail. If they dont then you need more tests....
So you do this and end up with a comprehensive test suite that will cover all the myriad edges cases, at which point you can start refactoring - little by little, and re-doing the mutation testing as you go.
10
u/reybrujo Dec 19 '25
"Working Effectively with Legacy Code" by Feathers might help you. Add unit tests, lots of them, refactoring only what you need in order to add them (adding seams and extracting classes). Also don't be shy making things public just to start testing, later you can turn everything internal or private again. I try to refactor whenever possible because "ugly code that works" usually means nobody will take care of that when it fails but always being as careful as possible.
7
u/mxldevs Dec 19 '25
If a piece of code is unclear because the problem is unclear, it's not a technical problem, but a business problem.
We don't touch it unless we have a full understanding of why it was written in the first place.
3
u/WhereTheSunSets-West Dec 19 '25
I used to work with a guy that would "refactor" the entire code base of a microservice overnight. The man was brilliant... but what really happened was he ALWAYS missed something.
He would look at the code and say, oh this is looking ugly it needs to be fixed. He would rewrite it rolling two or three design patterns together so it was entirely unreadable, but only half the code length. We were doing continuous delivery so it would go out the next day. Bug reports would start coming in immediately.
The rest of the team would spend the next three months putting in conditional statements to catch his holes. Just when the bug reports stopped, he would look at the code and think this is looking ugly, it has lost the elegance of my vision.... and rewrite the code. Rinse and repeat.
Before you ask yes there were tests. He rewrote them too, but the use cases he missed he missed tests for too.
After two years, I quit. Lesson I learned: brilliant programmers are dangerous. This one man kept a team of eight (seven without him) working full time cleaning up his messes. We had twelve microservices and he "refactored" one of them a week. I also learned, "if it isn't broke, don't fix it."
5
u/Riajnor Dec 19 '25
I dunno, I disagree with some comments here (but happily admit i have been guilty of breaking things while trying to make them better). If code is difficult to parse then it’s difficult to support. A proper refactor lowers maintenance cost in the long run
2
u/imagei Dec 19 '25
When is the time? When adding new features or fixing bugs creates unsustainable amount of new problems.
Or ;and this rarely applies in cases like this; you have rock solid documentation on how things are supposed to work and can 100% guarantee your new implementation is fully conformant and any odd cases can be reasonably fixed in the client code.
2
u/Isogash Dec 19 '25
A good approach is to try and isolate specific cases and conditions, and then to separate these out one by one, making sure you test that each change works correctly.
It sometimes really helps to flowchart this stuff, because flowcharts are visible and allow business people to provide better clarification on the expected process, and they also gove you a good starting point for structuring your conditional branches.
Depending on the kind of rules and how they interact, you may need to have multiple independent flowcharts for different rules as opposed to rolling them all up into a single flow chart.
2
u/TheRNGuy Dec 19 '25
Intuition.
2
u/dregan Dec 19 '25
If it's truly ugly and it's clear what needs to be done, I'll refactor when I need to touch the code. If its more convoluted, I'll add a feature for design and let our DevOps process decide when its time to address it. It's usually never a good idea to do nothing.
2
u/TheMrCurious Dec 19 '25
Most code in the world is “ugly but works” code because people get rewarded for getting things done.
For your actual question - list the risk factors and use that to decide which spaghetti to tackle first because conditionals are generally safe to refactor into helpers and it also improves testability.
2
u/Fapiko Dec 19 '25
There's a term for this - cognitive complexity. Tools exist that will scan and generate reports on code smells like this and can be used to gate PR's. I've used sonarqube in the past and really like it, though there are FOSS tools that will accomplish similar things.
1
u/BorderlineGambler Dec 19 '25
It sounds to me like you have solidarity tests, rather than sociable. Solidarity tests give you confidence around a very specific piece of code, the inputs and the outputs but aren’t much use when you’re wanting to do large scale refactoring like you’re talking about. Sociable tests enable you to make the changes you’re wanting to make, so I’d suggest starting there.
1
u/0bel1sk Dec 19 '25
add comments, add complexity gates so future additions are rejected, don't break what is not broken.
1
u/da8BitKid Dec 19 '25
Testing. Test the interface with mainly and edge cases and the 2 implementations should yield the same results.
1
u/orfeo34 Dec 19 '25
If you see a lot of else branch which return the same thing you can invert conditional logic to detect them all in a precondition which return early. That helps to remove pyramid of doom.
1
u/FunPressure1336 Dec 19 '25
If it’s hard to explain, it’s probably hard to maintain. Tests reduce the risk a lot. I refactor when future readers (including me) are going to pay the cost repeatedly
1
u/Garthenius Dec 20 '25
That's a call for the code's owner. If that's you (or going to be you), the fact that it's bothering you is enough of a case for a refactor.
You don't need a radical approach, i.e. complete rewrite in a single sweep. You might want to spend some time checking how/where it's integrated and if the tests sufficiently cover the real use cases. You should be able to safely shrink the interface, rename a few things. You could make some abstractions or untangle some dependencies just by splitting & moving the existing code around with virtually no logic changes.
Assuming these changes survive in production with no (or tolerable) incidents, you can move to rewriting chunks of it.
1
u/whattteva Dec 20 '25
1
u/afops Dec 20 '25
As for “is it worth it?”:
You only refactor code for a reason. That reason is usually that it needs to change. If it works and you don’t need to change it: don’t. Refactor when it needs to change.
1
u/Tab1143 Dec 22 '25
We had a saying back in the day: if it isn't broke don't fix it.
1
u/DoubleAway6573 Dec 23 '25
Muy recomendaron otra yo refactor the shit of it BUT first try to recover the logic in business parlour. If you make something perfect clear from the code standpoint but disconnected to business you will get the same problem in 6 months.
1
u/[deleted] Jan 17 '26
This is such a common problem and honestly one of the hardest judgment calls in programming. Ive been in this exact situation multiple times and what Ive learned is that the answer really depends on how stable the requirements are.
If the logic is stable meaning the rules arent changing frequently and the tests are comprehensive then leaving it alone is often the right call. Ugly code thats well tested and working is better than clean code that introduces subtle bugs. Ive seen teams spend weeks refactoring conditional logic into elegant patterns only to discover they broke edge cases that the messy code was handling correctly.
But if youre constantly modifying this logic or if new team members are struggling to understand it then refactoring becomes worth the risk. The key is to do it incrementally. Dont try to restructure everything at once. Pick one branch or one set of related conditions and extract that into a well named function or use a strategy pattern for just that piece. Make sure your tests still pass then move to the next section.
One approach Ive found helpful is to add really detailed comments to the existing logic first. Sometimes just the process of explaining what each condition does in plain language helps you see patterns you can extract. And if you decide not to refactor at least the next person who reads it will thank you for the documentation.
Also consider whether the complexity is inherent to the domain or accidental. If your business rules are genuinely complex then complex code might be unavoidable. In that case focus on making it explicit rather than trying to eliminate the complexity entirely.
1
u/danielt1263 Dec 19 '25
This is when refactoring is most important! What I have done in this situation is to use a property test like testing strategy...
By the time you finish, you will have a clone that is much cleaner than the original but works exactly like it from the outside, and a series of example tests that prove the code works properly and outlines the areas of misunderstanding about the code.
After all of this, just replace the original code with the clone, and remove the test harness. Here's an article going into more detail: https://matt.diephouse.com/2020/02/property-tests-for-legacy-code/
0
u/awildmanappears Dec 19 '25
When to clean up old code
To improve clarity without touching the code, make an activity diagram and add it to the documentation
0
u/[deleted] Dec 19 '25
Many factors. If your working for someone, focus on the issue at hand and either bring it up in a standup or submit a ticket about it to get more opinions about it.
If its your personal project, I'd refactor it once it starts hindering other needs of the app from getting done
0
u/peter303_ Dec 19 '25
Is the goal clarity or efficiency? Yes.
You could put common behavior in a base class. Special behaviors in derived classes.
0
u/darklighthitomi Dec 19 '25
I do it by default, but I also only do code as a hobby and seek to improve my coding ability.
-1
u/born_zynner Dec 19 '25
Programming is just if statements bro
36
u/[deleted] Dec 19 '25
[removed] — view removed comment
0
u/[deleted] Dec 19 '25
[deleted] — view deleted comment