How large can you make this function without adding any lines of code that do nothing

-1

1

    public int UndocumentedCode(int a, int b)
    {
        while (true)
        {
            a -= b;
            b += a;
            a = b - a;
            if (b < a)
            {
                return b;
            }
            else
            {
                b += 1;
            }
        }
    }

First figure out what it does (or look at the spoiler) the challenge is to make it as long as possible without adding lines that do absolutely nothing e.g. if (true) or do anything completely meaningless e.g. a declaration that isn't used/needed.

any lines of code that can be removed, with the result still being the same, do not count

the more confusing looking, the better.

it is a function for getting the lesser of two numbers. the if (b < a) return b is self explanatory. The a and b calculations at the top are to swap b and a around, so when it loops it will check a < b. the b+=1 is to prevent infinite loops if they are the same

puser

Posted 2014-03-10T16:27:28.127

Reputation: 107

Question was closed 2017-08-10T04:29:53.650

this isn't normal code golf, and this sort of 'challenge' may not even be a good thing to have on codegolf.stackexchange. if so please say – puser – 2014-03-10T16:28:56.333

5I don't currently see it as a good question. You could just add an infinite amount of lines that create and use variables, doing loops of various sorts, finding prime numbers perhaps, etc., while the function itself still at the end returns the same result – Claudiu – 2014-03-10T16:30:04.570

@Claudiu i meant for "without adding lines that do absolutely nothing e.g. if (true) or do anything completely meaningless e.g. a declaration that isn't used/needed." to say you can't do this. but I haven't worded it well – puser – 2014-03-10T16:31:36.983

@Claudiu maybe adding a clause that if any lines were taken out, then the code would not complete correctly then that is not allowed? – puser – 2014-03-10T16:32:46.267

If you want longest code rather than shortest code, the tag should be code bowling, not code golf – Level River St – 2014-03-10T16:34:26.320

Hmm you will have to word it better. But it seems the answer is still "infinitely large". For example, have part of the code which evaluates to 1 if the tenth fibonnaci number is 55 and 0 otherwise, then use that code wherever you'd use the literal 1. Then you can always keep going since the code that computes the 1 will also have literals, etc. – Claudiu – 2014-03-10T16:34:56.823

I don't think it is good question. I can add 3 lines that swap values of a and b, infinite times in between the original code anywhere. But does it make any sense? – microbian – 2014-03-10T16:42:45.727

@Claudiu yeah, you're right. I can't think of a properly enforceable way of wording that. other than maybe saying it has to be something that a very sleep deprived programmer could legitimately justify doing – puser – 2014-03-10T16:43:28.007

@puser: you can mark it as popularity-contest in which case there's no objective criteria, just the most popular answer wins. you'd have to change the criteria from being as long as possible to something like you just said – Claudiu – 2014-03-10T16:47:01.780

@microbian no, because swapping values is needed for the code to work, if you removed it then it wouldn't work. and if you added it 4 extra times, you can remove those 4 extra times and it would still work – puser – 2014-03-10T16:47:08.240

I would write return a<b?a:b; – None – 2014-03-10T16:47:22.040

@puser, that would hold true for any answer for this question. Because your current code works as it is. So any added lines can be removed safely to restore original code. How is it different? – microbian – 2014-03-10T16:52:24.773

@microbian there's a difference between removing and changing. though maybe i shouldn't have put an example answer in the question thinking about it... – puser – 2014-03-10T16:54:47.250

Answers

5

Not sure this is quite in the spirit of things, and I'm not going to write all the code, but technically all of the lines in the following would be necessary for the function to work. Note, for the sake of brevity I've changed the types to char, which I will assume is an 8-bit signed type (I didn't spot any mention of a specific language):

public char UndocumentedCode(char a, char b)
{
    if (a == -128 && b == -128)
        return -128;
    if (a == -127 && b == -128)
        return -128;
    if (a == -126 && b == -128)
        return -128;
    ...
    // can you see where I'm going with this?
    ...
    if (a == -128 && b == -127)
        return -128;
    if (a == -127 && b == -127)
        return -127;
    if (a == -126 && b == -127)
        return -127;
    ...
    // yawn...
    ...
    if (a == 125 && b == 127)
        return 125;
    if (a == 126 && b == 127)
        return 126;
    if (a == 127 && b == 127)
        return 127;

    // i think you get the idea by now
}

The code would of course be platform dependant, and cases would need to be written for all combinations of negative and positive numbers. Perhaps some kind of script to write the file, to save aching fingers, and to automatically generate the code based on platform-specific options.

If we just went with 8-bit ints, then the final code would be roughly 131075 lines long, with all lines being necessary to cover all possibilities.

icabod

Posted 2014-03-10T16:27:28.127

Reputation: 191

as horrendous as this is, I quite like it – puser – 2014-03-10T17:15:19.677

1@puser: Horrendous? This is production code ;) - It would actually be more interesting to write the script to produce it I think. – icabod – 2014-03-10T17:17:53.157

0

 public int UndocumentedCode(int a, int b)
 {
   while (true)
   {
       a -= b;
       b += a;
       a = b - a;
       bool isGT? = b < a;
       if isGT?
       {
          return b;
       }
       else
       {
         b += 1;
       }
    }
}

Add another statement with boolean condition.

Mamun

Posted 2014-03-10T16:27:28.127

Reputation: 181

0

public int UndocumentedCode(int a, int b)
{
    int a1 = a;
    int a2 = a1;
    int a3 = a2;
    int a4 = a3;
    int a5 = a4;
    int a6 = a5;
    int a7 = a6;
    int a8 = a7;
    int a9 = a8;

    // etc

    int a9999 = a9998
    int c = a9999;
    while (true)
    {
        c -= b;
        b += c;
        c = b - c;
        if (b < c)
        {
            return b;
        }
        else
        {
            b += 1;
        }
    }
}

Ypnypn

Posted 2014-03-10T16:27:28.127

Reputation: 10 485