r/programminghorror • u/Coolfoolsalot • Sep 30 '26
Java I love loops
Working in a legacy codebase. Dozens of people have contributed over the last decade. User spotted a bug & I traced the function calls and found
for(int i = 0; i < 2; i++) {
if(i == 0) // do A
if(i == 1) // do B
}
Wasn’t the source of the bug, and it technically works fine. I love loops
800
Upvotes
70
u/high_throughput Sep 30 '26 edited Sep 30 '26
Won't it skip the first "iteration"?
Edit: OP's post originally said
if(i = 0) // do A19
u/socal_nerdtastic Sep 30 '26
Is there an
if (condition) i++;in A?4
u/richardathome Sep 30 '26
Typo?
if(i = 0) // do A
Should be:
if(i == 0) // do A
444
u/Xterm1na10r Sep 30 '26
so that's the code AI was trained on...
262
u/luthervespers Sep 30 '26 •
you can turn the loop into a simple condition:
if(1){
do A
do B
}
32
u/RaleighRoger Oct 01 '26
whenever I see stuff like this I think that maybe at one point it was a for loop that made sense and just evolved over time. Different devs mad different changes and it just ended up that way because people were just changing a line at a time without considering or bothering to deal with the lines surrounding it. or maybe I'm wrong and it's just terrible
15
u/LizenCerfalia Sep 30 '26
Couldn't it have technically just been two functions back to back? Like do A function then B? Because even if it uses the index for stuff, it's pretty much the same as doing myArray[1]
26
u/patrlim1 Sep 30 '26
How horribly inefficient
Replace it with a switch statement
7
u/4xe1 Oct 01 '26
Would do the same or am I tripping?
8
u/NonProphet8theist Sep 30 '26
Brick, are you just pointing at things on the computer and saying you love them?
4
u/Dr-VanNostrand Sep 30 '26
Could have been used like a go-to.
Instead of having "if (x) { long code }" you can do "if (!x) { break }" and wrap the if statement in a loop to break out of.
3
u/Edxactly Sep 30 '26
I found an if statement with the only thing inside of it was a comment saying the if statement is true .
Production code of course
3
u/Deathisfatal Sep 30 '26
Ah the good old Loop-switch sequence
3
u/the-year-is-2038 Oct 02 '26
Sounds like it's time to introduce them to Duff's device
2
u/shizzy0 Oct 01 '26
hey babe, wake up. new semi-colon just dropped.
2
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Oct 01 '26
It was indented properly, right? Maybe edit the OP to use a code block tag instead.
1
u/marxama Oct 01 '26
This is horrible.
Clearly, you should have a list of objects implementing the "BusinessLogic" interface. Then you can dynamically add any business logic you want!
...now imagine how easy this would be to sell to the higher-ups.
1
u/DrMaxwellEdison Oct 05 '26
Do ya better, I see tons of code samples in our legacy that read like:
Most of the code inside those
ifblocks is duplicated from the other ones, making these 50-line monstrosities that no one ever thought to refactor.Can't fix it without a ticket specifically for that, either; otherwise "the diff is too large", "it's risky", and having changes rejected.
1
u/NooneAtAll3 Oct 16 '26
I have seen
it usually happens when there was some common code to execute for every index, but it was removed later