r/badcode • • Jun 02 '20

java return 2;

Post image
1.2k Upvotes

29 comments sorted by

98

u/whattheclap Jun 02 '20

The fact that there’s no argument means that this is probably used to operate with 2 on another number

115

u/[deleted] Jun 02 '20

It was not! It was used to append “2” to a few strings. I removed it, but I really considered leaving it because it’s hilarious.

15

u/[deleted] Jun 03 '20

Even if they were gonna do it this way, why wouldn’t they just have a method like this

public static int appendTwo(){ return 2; }

I’m just very lost why they have the whole increment by one thing that has to be a joke right? 😂

25

u/[deleted] Jun 03 '20

From looking at how it was used, I’m guessing whoever wrote it intended for it to return a number that increments by 1 every time it’s called. Just didn’t think about it very hard.

6

u/[deleted] Jun 03 '20

Oooohhh i see what they were trying to do now. I guess then my next question would be if they’ve every heard of global variables? Haha

6

u/[deleted] Jun 03 '20

To be fair, in the context of this method a global variable wouldn’t have made sense, but there are definitely better ways this could have been achieved ;)

2

u/[deleted] Jun 03 '20

Well if they wanted it to be more than two couldn’t they just have declared the variable static at the class level then pass it as a parameter to incrementByOne and then it would have its desired behavior at least right?

3

u/[deleted] Jun 03 '20

You’re right! For the number of times it’s (attempted to be) used, though, I’d just use a few for loops.

65

u/[deleted] Jun 02 '20

[removed] — view removed comment

40

u/toastedcroissant227 Jun 03 '20

Not misleading. Clearly you start by appending “number” (1) to the string, then you append “increment ByOne” (2) to the string when you need to increment by 1, then, logically, you would append “incrementIncrementByOneByOne” (3) to the string when you needed to increment by 1 again, then “incrementIncrementIncrementByOneByOneByOne” (4) when you needed to increment again, and so on.

217

u/Trevski13 evil Jun 03 '20

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

45

u/Dumbo14 Jun 02 '20 edited Jun 02 '20

They probably intended for the variable to be static and never realised their mistake.

Edit: Wait, local static variables aren't legal in Java. This was probably written by someone coming from C

29

u/Exnixon Jun 02 '20

And even in C, the first invocation would return 2.

0

u/Lurchwart Jun 03 '20

And the second two

4

u/Dumbo14 Jun 03 '20

No, if the variable is static (in C at least) then every successive call to the function would return a incremented number.

0

u/ct402 Jun 03 '20

Is this Java or C# tho ? I mean, this clearly a valid Java syntax, but I'm not sure what would make it invalid C#. Also would local static be valid in C# ?

Edit : Looked it up, no local statics in C# either ^

11

u/ElloYellowHello Jun 03 '20
assert 2 == incrementByOne()

All tests passed.

9

u/[deleted] Jun 03 '20

XKCD 221

5

u/[deleted] Jun 03 '20

Var value = value - 1 + incrementByOne. You’re welcome , now we will be eliminating your position after you check this in.

0

u/Kilgaloon Jun 03 '20 edited Jun 03 '20

Nothing wrong with this, except missleading method name. 😂

Is it possible that user wanted to do something like static int incrementByOne = 1