Jump to content
cybercoco

Minimize number of similar lines in algorithm

Recommended Posts

I'm sure there's a way to minimize the number of lines in this code.

I have three variables, each of them needs to get through a simple math function : f(x)=10x.

_var1 = 0.2;
_var2 = 0.7;
_var3 = 0.5;
_array = [_var1,_var2,_var3];

_var1 = _var1 * 10;
_var2 = _var2 * 10;
_var3 = _var3 * 10;

If I have let say ten vars, is it better to use the brute way (see snip above), use a foreach command or use a function (optimization wise).

Probably the function, isn't it ?

Share this post


Link to post
Share on other sites

Use forEach and push the results back into an array.

Share this post


Link to post
Share on other sites

Ok thanks, also another thing : if I use a function and I want to output the result.

Should I do

_varProcessed = _varUnprocessed call _fnc_processor;
// and then
hint format ["%1",_varProcessed];

or simply

hint format ["%1",_varUnprocessed call _fnc_processor];

Share this post


Link to post
Share on other sites

Doesn't really matter, only difference is that the first example is easier on the eyes. What's all that stuff about reduceing lines anyways? The performance gain is negligible and local variables become nil after the script has run.

Share this post


Link to post
Share on other sites

Doesn't really matter, only difference is that the first example is easier on the eyes. What's all that stuff about reduceing lines anyways? The performance gain is negligible and local variables become nil after the script has run.

I am preoccupied with that and making the code lighter and smaller, but I realise, like you said, that it doesn't matter.

Thanks for the help ;)

Share this post


Link to post
Share on other sites

Doesn't really matter, only difference is that the first example is easier on the eyes. What's all that stuff about reduceing lines anyways? The performance gain is negligible and local variables become nil after the script has run.

 

I wouldn't be that quick with a judgement like that. For scripts which are run only once in a while, I'd agree. But think about scripts which run waitUntil or while loops or which run onEachFrame, probably even with rather complex code inside. Producing unnecessary variables and operations may at some point impact the game's fluency.

 

I personally do not assign local variables if the operation is easily readable and I would use the variable only once. So as per cybercoco's example, I prefer latter:

hint format ["%1",_varUnprocessed call _fnc_processor];

And reducing lines of code to reduce redundance is simply senseful, because you only have to maintain the respective code in one place rather than in many. That's one of the main reasons why functions were invented.

Share this post


Link to post
Share on other sites

Thank you Heeeere's johnny! for your reply

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

×