Is there a maximum line of code a method can have?

Hello,

I had a very annoying bug where the game would run fine in the editor and Script Debugging, but not on my phone as a non-developer build. After couple hours of debugging using GUIText, I learned that the problem was one of my methods that was extremely long.

By extremely long, I mean slightly over 500 lines. With that said, the number of operations it did(?) was not that much (it just compared several parameters and did couple things here and there).

What I ended up doing was remove one parameter and break the method in question up into multiple methods - and now it runs. Keep in mind that, with an exception of one if statement, the number of operation is the same.

Anyways, this made me wonder: is there a maximum line of code a method can have? If so, what is a safe number? And finally, is the maximum number acceptable dependent of what device is running the game?

Oh yeah, I’m using C#.

Thanks!

EDIT:

I really want to figure this out.

This is ALL I did to “fix” the “bug.”

I turned the following method:

    public void LongMethod(int integer, bool boolean)
    {
        if (integer == 0)
        {
            if (boolean)
            {
                // do stuff
            }
            else
            {
                // do something else
            }
        }
        else if (integer == 1)
        {
            if (boolean)
            {
                // do stuff
            }
            else
            {
                // do something else
            }
        }
    }

into this:

    public void LongMethod1(int integer, bool boolean)
    {
        if (integer == 0)
        {
            if (boolean)
            {
                // do stuff
            }
            else
            {
                // do something else
            }
        }
    }

    public void LongMethod2(int integer, bool boolean)
    {
        if (integer == 1)
        {
            if (boolean)
            {
                // do stuff
            }
            else
            {
                // do something else
            }
        }
    }

and then have the class calling the method figure out which method to be called instead of having the method figure it out. It did have much more parameters, but you get the idea.

There is not exactly a limit to the amount of lines of code which you can do, it depends more on how optimized it is, if you are doing lots of heavy math, you will slow the game down, however if its lots of light value setting and stuff, it wont really matter so much. You should not worry about how many lines you use, you should worry more about how optimized the code is.

There is no limit to the number of line a method can have. Remember that you can have a 1-line function that looks like this :

private void DoALotOfStuff () {
    if(foo == "something" && bar != null) transform.position = Vector.zero; foreach(string s in myStringContainer) for(int i = 0; i < s.Length; i++) Debug.Log("This loop is dumb"); ... and on and on and on...
}

However, if you can’t tell in one short sentence what does the method do, it is a pretty convincing indicator that this method does too much stuff. Encapsulate small portions of that said method into smaller, more concise methods (usually private, due to their “internal” use)

The problem with god-like functions (functions that are doing WAY more than what they should) is that it become very tedious to debug them and to pinpoint the problem, if problem occurs.

Stick to short, t the point method and everything should be fine.

As for your example, the bug didn’t come from the line count. It was probably a logic error you made in this manuscript of a function that you unconciously fixed.