- Home /
Problems with large script
Hello, I was having a problem with unity crashing because it was running out of memory and traced it back to a script that has over 2000 lines of code in it. So I broke the script into several scripts to fix this. Not all of the lines are used at once, the way the script works is a function is called at the end of the start function as well as an integer it then uses if statements to find which block of code to use and then only needs about 100 - 200 lines of code to execute. My question is if anyone knows a max number of line you should use in one function? thanks
Well I have not ever heard of a problem in script size, code $$anonymous$$imization for that purpose , nor for commenting. I can't imagine the size EVER being the problem. But I could just be totally wrong of course, it's not like I coded for 43 years or something silly. But really when u reach that number (and me being a hypocrit with my 1.3k script) I'm splitting it, and you have a 95% chance of getting a good readability increase by doing so aswell
Answer by michaelvoigt · Dec 13, 2010 at 05:19 PM
Javascript or C#?
I have used monolithic scripts at 4k lines and they work fine, I don't think there is a limit.
Answer by Mike 3 · Dec 13, 2010 at 05:19 PM
It shouldn't matter how many lines it is, lines don't really attribute to memory use, new objects do. If it's throwing memory exceptions, you're probably doing something pretty dodgy inside the code itself.
Once I broke up the code into 6 functions ins$$anonymous$$d of 1 function with over 2000 lines of code it executes the function in a split second ins$$anonymous$$d of 30 and doesn't run out of memory and crash any more. The only thing I came up with is it reads through every line in the function regardless of what variable is being sent. But I am pretty new to scripting so I wouldn't throw out the possibility of dodgy code.
One function with 2000 lines is slightly different, yeah - what is probably happening is that it's not deallocating the memory for any of the stuff you're using until after the function has ended (which is right), splitting it up means that it'll be able to garbage collect after each snippet function. Still, you must be doing something pretty scary to max the memory usage in a single function
One other thing - it'll probably fun just as fast split up into seperate functions in the same script too. Either way - it's still not lines of code, but allocations done at a certain scope
Answer by Statement · Mar 13, 2011 at 03:38 AM
My question is if anyone knows a max number of line you should use in one function?
Well, this isn't a hard limit but more a practical limit for developers; I tend to avoid methods larger than 30 lines like the plague. Instead I often refactor code. This isn't because the program will run slow, but because I will code slowly if it was a huge "mess". I guess it's part subjective and part wisdom. It can become quite hard maintaining larger blocks of code. I guess everyone has their "pain thresholds" at some number of lines of code. Which is yours?
In fact, breaking into several functions cause a slight overhead but I don't try to squeeze out every single penny of my performance since I'd be working so much slower. I leave optimizations to things that matter. Reducing time/memory complexity, caching, and tightening out loops where I need them. Should I really need performance and I know it's because of these extra functions then I might opt to put them back inline again.
Readability is clearly improved if a part of the function says "UseJetpack()" instead of having several extra variables in the same scope, calling animations, decrementing fuel, playing sounds and so on.
It also be easier to cope with the relationship of variables if they are in clearly defined scopes. You know that a local variable in function X won't impact a local variable in function Y directly. This is also one of the reasons I limit my blocks of code to a handful.
Generally my functions are between 1 to 10 lines of code, but it's down to preference in my case. I usually create new functions if a method starts to grow out of hand. Refactoring in Visual Studio is so easy it becomes second nature.
Your answer
![](https://koobas.hobune.stream/wayback/20220613090903im_/https://answers.unity.com/themes/thub/images/avi.jpg)