I am trying to make my code easier to understand and I was wondering, how long should a function be? For example, if I were to make something like when a player steps on a block, an explosion is triggered and the block is set on fire, should I create one large function to deal with the entire thing or split it into smaller functions, one to create the explosion and one to create the fire?
I would say making stuff into one function would be better because then you don’t have to look over every functions. Everything will just be set up by one function
There’s no real set length a function should be, I only recommend splitting if you plan on using the explosion code and or the fire code in some other event/condition in the same script, if you only use the explosion and the fire code for a single thing, best it’s done in 1 function
Or another thing to point out as well, if you plan on using the same explosion/fire code for something else in another script, put it in a ModuleScript
depends on the use of the function.
the primary purpose of a function is not necessarily to make code easier to read, mind you. though it certainly does that. the primary purpose of a function is to allow you to easily reuse code, and to organize it into a neat block.
so, to follow your example, if you only ever plan to make a block set on fire and then explode, then one function is best. however, if you ever plan to, say, just light a block on fire. or just make a part explode. or make something that is not a part light on fire and explode. then it may be better to make a few functions that handle the different parts.
the best thing to do is ask yourself “can I use this in multiple places?” for a given chunk of code. if you can, making it a new function might not be a bad idea.
The way you write your could should strike a balance between optimization and readability. Sometimes you have to sacrifice either one depending on what you intended for your script to do. As far as your example goes, assuming “creating the explosion/fire” is just a basic Instance.new
call with a chain of property sets below, and that you only use it there and there, then there’s no point in making that into a closure. Context also matters, whether it be where/how many times that block of code is seen in your game, but also logically how frequently it gets ran in practice (e.g. something that runs every stepped tick vs something that you use for initializing an object or something one time/very infrequently).
Keep in mind that functions in Luau, while having progressed significantly in speed since 5.1, are still slower than if you directly ran the block of code inside them. Assessing whether or not that slight slowdown is worth the cost of making your code more unorganized/ugly is a skill that gets more and more refined as you advance as a developer.
Is the overhead for function calls in Lua that significant? I had always figured that if you were running code 2 or 3 times anyway, that it wasn’t a huge deal to put in a function.
Most of the times that’s fine - we’re talking about a timing difference of a fraction of a millisecond or so. However for massive chunks of code that get ran really often, it might be worth it to benchmark and see the difference as these miniscule timings can sometimes culminate into a noticable efficiency issue. For the average developer, I doubt that’s ever going to be even remotely a problem. Most of the things I mentioned concern stuff I make as I deal with VM obfuscators which can significantly slow down even basic idiomatic code.
wow, interesting stuff! i had never thought of it that way. thank you for enlightening me