r/programminghorror • u/Coolfoolsalot • 15d ago
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
261
u/luthervespers 15d ago
you can turn the loop into a simple condition:
if(1){
do A
do B
}
81
u/northerncodemky 14d ago
I had an old boss who just loved if(1) and if(0)… he’d hack about and use it as an alternative to just commenting out. Called himself an engineer but I didn’t see a single engineer trait from that awful man.
26
u/MrKrakenied 14d ago
It's not that bad when making a patch based fork. It can help you reduce the diff.
5
u/gjk-ger 14d ago
If(option)
12
u/MrKrakenied 14d ago
Actually in many languages you can't do two returns like:
int t() { return 0; // your diff return 1; }but you can
int t() { if (1) return 0; // your diff return 1; }Additionally, you can use if (0) to disable parts of the code without generating additional diff noise.
3
u/gjk-ger 14d ago edited 14d ago
No, with if(0) you will have diffs scattered throughout anytime you change your mind. Also, the possibility to forget to change one of your if's. Also, a "0" or a "1" doesnt inform a reader about anything.
I try to make it more explicit:
#Some kind of init area:
Option_1 = true
Option_2 = false
#Actual Code
...
If(Option_1)
....
If(Option_2)
....
If(Option_1)
1
u/4xe1 14d ago
What's wrong with
int t() { return 0; // your diff // return 1; }? It's one more line of diff, but it's a line you do want in your diff. Dead code is also more easily identified if it's commented out.
You can even use
int t() { // /* return 0; // */ return 1; }or even
function t() ---[[ return 0 --[=[--]] return 1; --]=]to toggle it easily.
To me, it seems weird for the interpreter or compiler to even know about the useless line of code, use comment, or, better yet, macro to hide them.
5
u/northerncodemky 14d ago
I’d much rather see an explicit ‘this entire chunk of code isn’t active’ comment out in the diff, even if it made it big, than a one line. A giant chunk of code being commented out is going to make me check it wasn’t doing anything important.
4
u/sirkubador 13d ago
it's practical.
Compiler gets rid of it when it is a constant.
You can do neat stuff if the expression is a macro, such as having embedded lbrary with these set at compile time or a software library with these set at runtime where everything is compiled in.
Same code for both and it does not even look ugly
1
u/heyyouhere 9d ago
putting a block of code in constant false condition could be beneficial to keep that block type checked during compilation, while commented out blocks get stale
55
46
15
u/ElGuaco 14d ago
True. I'd take this as a readability improvement because it probably does nothing for performance.
3
u/MrKrakenied 14d ago
Tbh really bad improvement, for me it would be an unreadability improvement lol
3
u/ElGuaco 14d ago
Unfolding a loop leads to poor readability? Why?
5
u/arensb 14d ago
Allow me to introduce you to Duff's Device.
4
u/darkriftx2 [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 14d ago
I'm going to try this in a PR sometime and watch it get obliterated by everyone at work 🤣
2
u/MrKrakenied 14d ago
I thought that you're talking about keeping it in the loop. I partially agree then. But I still don't see the need for if (1)
2
u/joemckie 14d ago
Why are you leaving if(1) in?
13
72
u/high_throughput 15d ago edited 15d ago
it technically works fine
Won't it skip the first "iteration"?
Edit: OP's post originally said if(i = 0) // do A
45
2
u/mediocrobot 15d ago
No, it starts with i = 0. The increment should only happen after the first iteration.
5
u/high_throughput 14d ago
if(i = 0)isif(0)isif(false)so it should never run, but OP corrected their post toif(i == 0)0
32
u/RaleighRoger 14d ago
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
19
u/socal_nerdtastic 15d ago
Is there an if (condition) i++; in A?
42
u/Coolfoolsalot 15d ago
Nope :/ the code is effectively do A then do B
1
u/Positron505 12d ago
As a very fresh junior dev, why not just do
Function C() { Do A; Do B; }And this will do A then do B. Why pass by a loop?
4
u/Coolfoolsalot 12d ago
You should absolutely do that instead. The code in the post is horror
1
u/Positron505 12d ago
Okay good i thought i had missed something obvious, and also forgot on which sub i am
4
14
u/LizenCerfalia 14d ago
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]
9
27
6
u/4xe1 14d ago
for(int i = -1; ++i < 1;) {
switch (i) {
case 0: // do A
case 1: // do B
}
Would do the same or am I tripping?
6
u/RabbitHole32 14d ago
case 1 can never happen in your code
5
u/4xe1 13d ago
That's why I omitted the break
5
u/RabbitHole32 13d ago
Oh! That's clever to the point where I don't even care if there are edge cases where your code does something different. Well done, you were able to surprise me and thus to improve my day. :)
8
u/NonProphet8theist 15d ago
Brick, are you just pointing at things on the computer and saying you love them?
5
u/Dr-VanNostrand 15d ago
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 15d ago
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
3
4
u/richardathome 15d ago
Typo?
if(i = 0) // do A
Should be:
if(i == 0) // do A
5
1
u/sreekotay 15d ago
no that's the funny part that OP is pointing out --- it works as is though the loop is superflous and... idiotic lol
for(int i = 0; i < 2; i++) { if(i = 0) doA(i); // do A after i=0 assignment -- wait this won't run? if(i = 1) doB(i); // do B after i=1 assignment } // loop terminates after i++which could have been just (and reduces to):
if (0) doA(0); //this won't run ? if (1) doB(1);5
u/Coolfoolsalot 15d ago
doAanddoBdon't take input and don't alter i either. It's functionally equivalent to
doA(); doB();1
u/sreekotay 14d ago
if (i=0) doA(); // will not execute though?
1
2
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 14d ago
It was indented properly, right? Maybe edit the OP to use a code block tag instead.
1
u/DrMaxwellEdison 9d ago
Do ya better, I see tons of code samples in our legacy that read like:
for step in ["step1", "step2", "step3"]:
if step == "step1":
...
if step == "step2":
...
Most of the code inside those if blocks 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.
444
u/Xterm1na10r 15d ago
so that's the code AI was trained on...