Jump to content
xjoker_

Is it better to have multiple functions or 1 big function ?

Recommended Posts

Hello, 

 

I was wondering if there is a difference in term of performance, between 

  1. defining multiple small fonctions in cfgFunctions
  2. defining one big function which handle everything with a switch/case and params

Or is it just a preference ? 

Share this post


Link to post
Share on other sites

Don't worry about performance until you run into performance issues caused by your scripts.

 

Other than that depends on your workflow I guess.

I've grown fond of having multiple smaller, specialized functions where possible.

Makes it easy for the eyes having to go over a few lines vs scrolling through 1000+ lines.

 

Cheers

  • Like 1

Share this post


Link to post
Share on other sites
5 hours ago, Grumpy Old Man said:

Don't worry about performance until you run into performance issues caused by your scripts.

 

Other than that depends on your workflow I guess.

I've grown fond of having multiple smaller, specialized functions where possible.

Makes it easy for the eyes having to go over a few lines vs scrolling through 1000+ lines.

 

Cheers

 

Dito. In general I'm all over the functions library however I do like using the single-file approach to get some "data-encapsulation"-like behaviour. I find single-file to be perfect for standalone modules that are ported a lot, but man you better comment those bad boys and do so well.

  • Like 1

Share this post


Link to post
Share on other sites
On 10.10.2018 at 11:45 AM, xjoker_ said:
  • defining multiple small fonctions in cfgFunctions
  • defining one big function which handle everything with a switch/case and params

I'd say the performance difference is quite clear. In your first case you just call the function and it does what you want.

The second case the function first has to find out what you want, which of course takes more time.

Share this post


Link to post
Share on other sites

1. Correctness

2. Readability and maintainability

3. Testibility

4. Reusability

5. Performance

Some things one should remember when measuring code quality (list is neither comprehensive nor mandatory). Most of the time I'd redcommend exactly this order (yes, performance is the last thing). So before you ask yourself, "does <some code> perform good?", you should probably go through steps 1-4.

So

On 10.10.2018 at 3:45 PM, xjoker_ said:

defining one big function which handle everything with a switch/case and params

might fail badly at least on steps 2 and 4 and 

On 10.10.2018 at 3:45 PM, xjoker_ said:

defining multiple small fonctions in cfgFunctions

has all the chances to be good in all aspects.

  • Like 2

Share this post


Link to post
Share on other sites

Technically speaking, a "function" in SQF is meant to be used by a call statement and return a value (I'm basing this on an old Taurus4K(?) document). It should be lean code that does one thing (single responsibility principle). More generally speaking, I usually write a script with some purpose in mind and after awhile, it gets too big for my sanity and I start breaking it up. If you look at my script library in the footer, I rarely ever follow SRP in SQF, but I sure do when coding in C# and Angular and all that.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×