Store returned values in variables

Share this topic:



Link to this posting

Postby Ursego » 21 Feb 2013, 11:31

Don't directly use values, returned by subroutines and expressions, as an input of other subroutines and expressions; instead, store them in interim variables, and use these variables in the rest of the method.

There are at least two reasons to do that:

1. It makes debugging easier - you can see the returned value immediately, before it is "buried" in any kind of complicated collection or data set, or even disappears at all after a comparison (like if (myVar == GetSomething())).

2. It helps to break one complicated line of code into 2-3 simpler and clearer lines if the called function is nested in an expression.

These two reasons dictate to store the return value of a function in a variable always - even if the function is called only once (if more than once then we have no questions at all!). The only exception from this rule - a well-named Boolean function which is called only once in the script and is used directly in an if statement: in this case we can understand the returned value - true or false - looking in the debugger if the program flow goes into the if or skips it (or goes into the else section). Ensure the called function doesn't return null which is treated as false by the program flow.

If you need to call a same deterministic function (i.e. giving always the same output for the same input in the given context) more than once in your script in order to re-use its ret. value in different fragments, then there are additional advantages of storing the ret. value in a variable:

1. Better performance (the function is run only once), especially if the function is not extremely efficient.

2. Signals that the called function is deterministic (otherwise developers can think it returns different values on each call).

So, call the function only once, and use the returned value in the rest of the script as many times as you need. This advice is especially important when the result of a function is used as a stop condition in a loop. For example:

PB:
Code: Select all
for ll_row = 1 to dw_cust.RowCount()
   ...
next

C#:
Code: Select all
for (i = 1; i++; i < SomeCollection.GetRowsCount())
{
   ...
}

Imagine that the function returns 10000 - it means, it is called 10000 times! The solution is obvious:

PB:
Code: Select all
long ll_row_count
ll_row_count = dw_cust.RowCount()
for ll_row = 1 to ll_row_count
   ...
next

C#:
Code: Select all
int rowsCount = SomeCollection.GetRowsCount();
for (i = 1; i++; i < rowsCount)
{
   ...
}

But be careful - if the loop can change the number of rows in the collection, then function is not deterministic!
User avatar
Ursego
Site Admin
 
Posts: 111
Joined: 19 Feb 2013, 20:33

Return to Coding Style

Who is online

Users browsing this forum: No registered users and 1 guest


Store returned values in variables

Share this topic:


If you think that this site is not too bad, please LIKE it in Facebook. Thanks!





cron
free counters

eXTReMe Tracker