MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/badcode/comments/gvf0dp/return_2/ftmillz/?context=3
r/badcode • u/[deleted] • Jun 02 '20
29 comments sorted by
View all comments
217
Fixed it for you:
public static int incrementByOne() { int incrementByOne = 0; incrementByOne++; return ++incrementByOne; }
12 u/RovingRaft Jun 10 '20 it actually took me a while to realize the joke 5 u/TheEnderChipmunk Jun 10 '20 I don't get it. Is it because he isn't saving the increment, and then just increments in the return statement? 10 u/RovingRaft Jun 10 '20 I think it's this:incrementByOne is equal to zero in its definition.Then it gets incremented up by one.Then it gets incremented up by one again.incrementByOne is 2 right now. In the same statement, it's returned.so this would be equivalent to "return 2;" 2 u/TheEnderChipmunk Jun 10 '20 Does the first increment not need to be assigned to the variable? I'm pretty sure it does 2 u/RovingRaft Jun 10 '20 what do you mean? 3 u/TheEnderChipmunk Jun 10 '20 Whoops I was being small brain. I thought x++ meant x+1 and not x += 1. Of course, with the actual name of the variable, not x. 1 u/RovingRaft Jun 10 '20 Ah, brain fart :Phappens to the best of usin any case, the code the guy made as a joke is identical to the one in the postjust that the variable starts at 0, instead of 1.
12
it actually took me a while to realize the joke
5 u/TheEnderChipmunk Jun 10 '20 I don't get it. Is it because he isn't saving the increment, and then just increments in the return statement? 10 u/RovingRaft Jun 10 '20 I think it's this:incrementByOne is equal to zero in its definition.Then it gets incremented up by one.Then it gets incremented up by one again.incrementByOne is 2 right now. In the same statement, it's returned.so this would be equivalent to "return 2;" 2 u/TheEnderChipmunk Jun 10 '20 Does the first increment not need to be assigned to the variable? I'm pretty sure it does 2 u/RovingRaft Jun 10 '20 what do you mean? 3 u/TheEnderChipmunk Jun 10 '20 Whoops I was being small brain. I thought x++ meant x+1 and not x += 1. Of course, with the actual name of the variable, not x. 1 u/RovingRaft Jun 10 '20 Ah, brain fart :Phappens to the best of usin any case, the code the guy made as a joke is identical to the one in the postjust that the variable starts at 0, instead of 1.
5
I don't get it. Is it because he isn't saving the increment, and then just increments in the return statement?
10 u/RovingRaft Jun 10 '20 I think it's this:incrementByOne is equal to zero in its definition.Then it gets incremented up by one.Then it gets incremented up by one again.incrementByOne is 2 right now. In the same statement, it's returned.so this would be equivalent to "return 2;" 2 u/TheEnderChipmunk Jun 10 '20 Does the first increment not need to be assigned to the variable? I'm pretty sure it does 2 u/RovingRaft Jun 10 '20 what do you mean? 3 u/TheEnderChipmunk Jun 10 '20 Whoops I was being small brain. I thought x++ meant x+1 and not x += 1. Of course, with the actual name of the variable, not x. 1 u/RovingRaft Jun 10 '20 Ah, brain fart :Phappens to the best of usin any case, the code the guy made as a joke is identical to the one in the postjust that the variable starts at 0, instead of 1.
10
I think it's this:
incrementByOne is equal to zero in its definition.
Then it gets incremented up by one.
Then it gets incremented up by one again.
incrementByOne is 2 right now. In the same statement, it's returned.
so this would be equivalent to "return 2;"
2 u/TheEnderChipmunk Jun 10 '20 Does the first increment not need to be assigned to the variable? I'm pretty sure it does 2 u/RovingRaft Jun 10 '20 what do you mean? 3 u/TheEnderChipmunk Jun 10 '20 Whoops I was being small brain. I thought x++ meant x+1 and not x += 1. Of course, with the actual name of the variable, not x. 1 u/RovingRaft Jun 10 '20 Ah, brain fart :Phappens to the best of usin any case, the code the guy made as a joke is identical to the one in the postjust that the variable starts at 0, instead of 1.
2
Does the first increment not need to be assigned to the variable? I'm pretty sure it does
2 u/RovingRaft Jun 10 '20 what do you mean? 3 u/TheEnderChipmunk Jun 10 '20 Whoops I was being small brain. I thought x++ meant x+1 and not x += 1. Of course, with the actual name of the variable, not x. 1 u/RovingRaft Jun 10 '20 Ah, brain fart :Phappens to the best of usin any case, the code the guy made as a joke is identical to the one in the postjust that the variable starts at 0, instead of 1.
what do you mean?
3 u/TheEnderChipmunk Jun 10 '20 Whoops I was being small brain. I thought x++ meant x+1 and not x += 1. Of course, with the actual name of the variable, not x. 1 u/RovingRaft Jun 10 '20 Ah, brain fart :Phappens to the best of usin any case, the code the guy made as a joke is identical to the one in the postjust that the variable starts at 0, instead of 1.
3
Whoops I was being small brain. I thought x++ meant x+1 and not x += 1. Of course, with the actual name of the variable, not x.
1 u/RovingRaft Jun 10 '20 Ah, brain fart :Phappens to the best of usin any case, the code the guy made as a joke is identical to the one in the postjust that the variable starts at 0, instead of 1.
1
Ah, brain fart :P
happens to the best of us
in any case, the code the guy made as a joke is identical to the one in the post
just that the variable starts at 0, instead of 1.
217
u/Trevski13 evil Jun 03 '20
Fixed it for you: