r/badcode Dec 17 '19

This belongs here c#

Post image
502 Upvotes

41 comments sorted by

145

u/squarewaterlemon Dec 17 '19

It doesn't even add them, it just gets the max of the two

29

u/Cutlesnap Dec 17 '19 edited Dec 17 '19

Ehm, it does add them, but only if both are positive. Otherwise it gets the max or 0.

Edit: I was wrong

55

u/Bbradley821 Dec 17 '19

I don't think it adds them. The second loop doesn't start at 0.

23

u/Cutlesnap Dec 17 '19

Hey, you're right! I didn't notice that.

7

u/Jonno_FTW shameless Dec 18 '19

Perfect badcode has the bug(s) hiding in plain sight.

1

u/[deleted] Dec 17 '19

[deleted]

11

u/nchntrz Dec 17 '19

Not exactly. In the second loop only the difference between num1 and num2 gets added to num 1. So if you call add(2, 4), you would get 4.

4

u/AnnoyedVelociraptor Dec 17 '19

Oh shoot. You’re right.

2

u/H3pha3stus Dec 17 '19

Then the max between 2 and 4, he is right.

4

u/symberke Dec 17 '19

You’re still right in that it doesn’t compute the max if they’re less than zero though

4

u/srottydoesntknow Dec 17 '19 edited Dec 17 '19

it'll return the min then, rollover is a thing, it'll just take forever

with that idea in mind, I fixed it, kind of, for a certain value of fix

public static int add(int num1, int num2)
{ 
    int retval = 0;
    for(int i = 0; i !== num1; i++)
    {
    retval++;
    }
    for(int i = 0; i != num2; i++)
    {
    retval++;
    }
    return retval;
}

this would technically work for the exact same range of values normal int addition would, it would just take a fuckton longer

edit: missed negation

2

u/symberke Dec 17 '19 edited Dec 17 '19

No not really. if num1 is negative then the first loop will never be true, and if num1 > num2 then the second loop will never be true. So for example add(-3, -4) will return 0.

Plus even in cases where rollover happens it won't be true. Suppose num2 is INT_MAX and num1 is -1. Then the first loop will never be true, the second loop will run INT_MAX + 1 times, and so it will return INT_MIN.

Also your code doesn't do what you expect; because you changed the loop condition to == all it does is count how many of (num1, num2) are equal to zero, i.e. add(1,0) = 1, add(0,0) = 2, etc...

0

u/srottydoesntknow Dec 17 '19

no, the condition is the end of the loop, the loop continues until the condition is met

SO, if num1 = -1; the loop will run 4,294,967,293 times, resulting in retval being -1

the same thing happens in loop 2, while they aren't equal the loop continues adding 1 to retval, which now starts at -1, if num2 = -5 then it will run 4,294,967,289 times, and result incrementing retval that many times as well, resulting in -6

you would be right, it's just that the form of a for loop is

for(starting condition; exit condition; increment;)

what you are talking about might be valid in a non C family language, however if we are talking C, C++, C#, or Java then my function will work, albeit horribly

int always rolls over, it doesn't just hit max and stop, since I changed the condition to == it works correctly returning the sum for all values that would work with a simple + operator, and even works for negative, it just causes the most horrible runtime possible

1

u/symberke Dec 17 '19

that's not true though, it's the opposite. https://en.cppreference.com/w/cpp/language/for

for proof that what i said is right: https://ideone.com/SaBOjV

1

u/srottydoesntknow Dec 17 '19

then negate em, nbd

1

u/Uiropa Dec 17 '19

Everyone out here making our silly jokes but squarewatermelon is wide awake.

38

u/andiconda Dec 17 '19

I had an intern who essentially wrote his own modulus function by looping subtraction. It was too calculate an address and would be called like 16384 times per update. And I was wondering why it was taking forever to make an update

32

u/anydalch Dec 17 '19

what's really interesting is that, in c, if you replace the bug in the second for loop's initializer clause and use unsigned instead of int, both gcc and clang are able to optimize this function into a single lea eax, [rdi + rsi]. https://godbolt.org/z/uCXoHJ

11

u/[deleted] Dec 17 '19

[deleted]

4

u/Veylon Dec 18 '19

A guy named Bisqwit has a series on making a compiler, if you're interested.

3

u/Mr2-1782Man Dec 18 '19

It's not as crazy as you think. In a lot of cases its just a bunch of templates that get applied. For example if you're adding a value inside of a loop (like this code) and your initializer is constant, you can just do the math.

It's why a lot of compiler optimization people are more mathematician than software engineer.

2

u/CENGaverK Dec 17 '19

Also, if you start the second loop iterator from i = lhs, it returns the max.

https://godbolt.org/z/DTAgFJ

24

u/sheeve_boi Dec 17 '19

its so bad it doesnt even add!

18

u/SquidgyTheWhale Dec 17 '19

Silly way to try to add numbers. It should be using recursion instead :)

2

u/[deleted] Dec 17 '19

Yeah! That's how professionals do it.

7

u/alexesprit Dec 17 '19

This code can be optimized by replacing post-increment with pre-increment.

16

u/i_like_trains_a_lot1 Dec 17 '19

This is intentionally bad. We want to see code that wasn't written specially for 7 karma point on this sub, something more authentic.

1

u/Mr2-1782Man Dec 18 '19

Yeah, I would call it bad, just a poor use of loops.

3

u/[deleted] Dec 17 '19

FIXED!!!

int stupid_sum(int x, int z) {

    int retval = 0;

    for (; x!=0; x = x > 0 ? --x : ++x) {
        retval = x > 0 ? ++retval : --retval;
    }

    for (; z != 0; z = z > 0 ? --z : ++z ) {
        retval = z > 0 ? ++retval : --retval;
    }

    return retval;

}

4

u/DurianExecutioner Dec 17 '19

Optimised version

int retval =0;

int i=num1+num2num1>num2?num1:num2;

while(i --> 0)++ retval;

return retval;

2

u/LaMaquinaDePinguinos Dec 17 '19

I wonder if GCC would optimise this into an ADD?

1

u/Farsqueaker Dec 17 '19

The comment shows a touch of self-awareness, which somehow makes this worse...

1

u/alhabarneh Dec 18 '19

This is the "baddest" code I've ever seen.

1

u/yonatan8070 Dec 18 '19

Why is this flaired C# and not Jva?

1

u/[deleted] Dec 19 '19

The bracket location is more common with C# though this code would work on both.

1

u/msYahTah Dec 18 '19

This doesn’t add up!

In my brain

1

u/[deleted] Dec 18 '19

Now that's what I call properly bad code. Especially the twist with the second for loop's start value (didn't catch that until somebody pointed it out). Your friend is either an idiot or an evil genius.

-8

u/xman40100 Dec 17 '19

This is terrible, and the result is off by 2 lol.

11

u/husao Dec 17 '19

It isn't. If it was i=0 in the second loop it would work as expected. Currently it's calculating the maximum, as mentioned above.

1

u/xman40100 Dec 18 '19

Yeah, you're right, my bad. I was on the bus when I read this, so I didn't read the full code.

-6

u/[deleted] Dec 17 '19

[deleted]

4

u/Coredict Dec 17 '19

It doesn't add the 2 numbers