r/badcode • • Jun 02 '20

java return 2;

Post image
1.2k Upvotes

29 comments sorted by

View all comments

Show parent comments

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 :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.