r/badcode • • Aug 10 '19

c# absolute genius!

Post image
931 Upvotes

59 comments sorted by

4

u/[deleted] Aug 10 '19

I can't even

2

u/[deleted] Sep 02 '19

square(int n) { return n << 1; }

Bitshift x one to the left equals x to the power of two, if you think about how binary works... And ignore overflow.

1

u/[deleted] Sep 02 '19

gamer moment

70

u/PyroBurnem greek comma Aug 10 '19

when you get paid per line of code

59

u/shizzy0 Aug 10 '19

When you get paid in time complexity.

PROGRAMMER 0: How much did you get?

PROGRAMMER 1: Just O(1). How much did you get, Fred?

PROGRAMMER 10X: O(n2), baby! Woohoo!

11

u/TheWindBlows Aug 10 '19

That'd be the dream...every routine would reference Bogosort.

25

u/calio Aug 10 '19

I want that comment as a license I can license my stuff under when I release them.

44

u/protosquirrel Aug 10 '19

Image Transcription: Code


// I don't know what I did but it works
// Please don't modify it
private int square(int n)
{
    int k = 0;
    while (true)
    {
        if (k == n*n)
        {
            return k;
        }
        k++;
    }
}

I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

7

u/WhyYouLetRomneyWin Aug 10 '19

Genius. Why didn't I think of that?

7

u/tcpukl Aug 10 '19

It's only a line change from a square root!

1

u/fb39ca4 depraved Oct 29 '19

And it will go into an infinite loop if the square root is irrational!

5

u/[deleted] Aug 10 '19

Efficiency 100

5

u/nomadicDev87 Aug 10 '19

O(n) > O(1) , duh.

4

u/[deleted] Aug 10 '19

You've gotta love it when bad code includes the simple solution to the problem.

3

u/[deleted] Aug 27 '19

Hey, calculates a quadratic result in linear time. Not bad. /s

2

u/ImAlsoRan Aug 10 '19

HyperGAN in a nutshell

4

u/DavidWigh Aug 10 '19

You could use a for-loop instead of a while-loop. Then you could define k, have your condition and k++ in one line

7

u/3636373536333662 Aug 10 '19

What's your point? You could also just return n*n

1

u/BiPoleArt Aug 10 '19

The point is to obfuscate it even more

1

u/wordbug Aug 10 '19

At least he commented.

1

u/GDZippN Aug 10 '19

That comment perfectly sums up all of my shitty code

0

u/[deleted] Aug 10 '19

[deleted]

7

u/[deleted] Aug 10 '19

damn did you actually take the time to write that? absolute lad

5

u/[deleted] Aug 10 '19

why tf are the methods private though, are we not even going to invoke this shit?

12

u/c3534l Aug 10 '19 edited Aug 10 '19

Of course not, the performance is terrible.

7

u/[deleted] Aug 10 '19

so we just have methods that will die of garbage collection?

0

u/[deleted] Aug 10 '19

[deleted]

4

u/[deleted] Aug 10 '19

rip, threads to threads, rust to rusts

70

u/pierovera Aug 10 '19

I get this is shitty code, but this wouldn't even compile.

19

u/fond42518 Aug 10 '19

Can't even try compiling bc I'm on mobile rn. Is it because the redeclaration of k in the second method?

14

u/Dr-RobertFord Aug 10 '19

No that wouldn't matter. In fact I'm pretty sure this would compile... It would run forever, however, unless n = 0

7

u/Dmium Aug 10 '19 edited Aug 10 '19

Edit: missread but I'm pretty sure it runs fine as long as you call square(n)

The code may have been edited since this discussion

17

u/BigMintyMitch Aug 10 '19 edited Aug 10 '19

It does matter in C#. You can’t redeclare a parameter in a C# function like the example above. If that line was removed, yes, it would run forever unless n = 0.

Edit: The comment above originally said int k = 0 not k = k when I commented this. I’m not an idiot, my comment was true at the time.

7

u/tcpukl Aug 10 '19

It's not redecorated though. It's just assigned to itself

7

u/BigMintyMitch Aug 10 '19

Ok, they edited the post since I left this comment. Now it’s just assigned to itself, before it was not.

1

u/Dr-RobertFord Aug 10 '19

I learned something today. Cheers!

1

u/[deleted] Aug 10 '19 edited Aug 10 '19

I’m not sure but I thought in most languages you can only overload the constructor like that?

Edit: Sounds like I was right not to be sure

3

u/fond42518 Aug 10 '19

I'm assuming this code is Java, in which a class's methods can be overloaded as long as their input parameters are different.

Edit: nvm, just saw the C# flair

2

u/dbgprint Aug 10 '19

What languages can’t do this? C++ can do it too, as long as the function signature is different

8

u/Doriphor Aug 10 '19

int k = 0;

This line needs to be deleted in private int square(int n, int k)

6

u/[deleted] Aug 10 '19

Shit, I missed that. Fixed it so that k is now set to k.

22

u/Doriphor Aug 10 '19

I like your solution, but you forgot about cosmic rays. Here I fixed it :)

int square(int n, int k)
{
    k = k;
    while (true)
    {
         if (k = n*n)
        {
            return k;
        }
        else
        {
            return square(n, k+1);
        }        
    }
}

int square(int n)
{
    int k = 0;
    while (true)
    {
         if (k = n*n)
        {
            return k;
        }
        else
        {
            return square(n, k+1);
        }

    }
}

int cosmicrayinterferenceprevention(int z)
{
    const int RUN_NUM = 100;
    int answers_arr[RUN_NUM];

    for (int i = 0; i < RUN_NUM; i++)
    {
       answers_arr[i] = square(z);
       for (int j = 0; j <= i-1; j++)
       {
           while(answers_arr[i] != answers_arr[i+1])
           {
               answers_arr[i] = square(z);
               answers_arr[i+1] = square(z);
           }
       }
    }
    return answers_arr[0];
}

7

u/[deleted] Aug 10 '19

It's so beautiful that I can't help but tear up a little.

3

u/MX21 Aug 10 '19

Lol wtf

5

u/xman40100 Aug 10 '19

You're going on a next level.

3

u/[deleted] Aug 10 '19

k will always be 0 in the recursive case when it's compared to n2 so you have an infinite loop unless n = 0.

2

u/machine3lf Aug 10 '19

You are my hero.

2

u/GideonMax Sep 06 '19

Beautiful code, amazing optimization

1

u/djsquaretube Aug 10 '19

Why do you need the while(true)?

2

u/EkskiuTwentyTwo i -= (i - (-1)) - i Aug 10 '19

Tradition.

0

u/tcpukl Aug 10 '19

Why did you keep the loops?

0

u/[deleted] Aug 10 '19

[deleted]

10

u/wind-raven Aug 10 '19

Nope, after seeing some of the code in the legacy app I'm working on I would love to come across code like this. It may make you go wtf but at least you can follow it.

18

u/[deleted] Aug 10 '19

no this is fake. This guy multiplied a number by itself so I'm sure that he knows how to return a square (return x**2)

0

u/[deleted] Aug 10 '19

[deleted]

11

u/satimal Aug 10 '19

Except for the == n*n bit

9

u/[deleted] Aug 10 '19

When we say reduced, we mean REDUCED

0

u/[deleted] Aug 10 '19

[deleted]

8

u/SlightlySaltedNuts Aug 10 '19

Yo, that’s the subreddit you’re on right now