Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Let's say I have a function that does x. I pass it a variable, and if the variable is not null, it does some action. And I have an array of variables and I'm going to run this function on each one.

Inside the function, it seems like a good practice is to check if the argument is null before proceeding. A null argument is not an error, it just causes an early return.

I could loop through the array and pass each value to the function, and the function will work great.

Is there any value to checking if the var is null and only calling the function if it is not null during the loop?

This doubles up on the checking for null, but:

  • Is there any gained value?
  • Is there any gain on not calling a function?
  • Any readability gain on the loop in the parent code?

For the sake of my question, let's assume that checking for null will always be the case. I can see how checking for some object property might change over time, which makes the first check a bad idea.

Pseudo code example:

for(thing in array) {
    x(thing)
}

Versus:

for(thing in array) {
    if(thing not null) x(thing)
}

If there are language-specific concerns, I'm a web developer working in PHP and JavaScript.

share|improve this question
add comment

3 Answers

up vote 6 down vote accepted

Is there any value to checking if the var is null and only calling the function if it is not null during the loop?

In practical terms, very little unless you're using a language that imposes incredibly high function call overhead.

If your function claims it will "do x if the thing you pass is not null, nothing if it is," then inside the function is where that behavior should be determined. Otherwise, the first time the function is called with a null pointer, you run the risk of unpredictable or undesired behavior. Additionally, if you want to change the function's behavior in the presence of a null, you only have to change it in one place.

share|improve this answer
3  
"incredibly high function call overhead" is a good description of a web service call. –  TMN Oct 26 '12 at 20:23
    
are there really languages that have high over head for calling (user-defined) functions? any body care to help make a list? –  changokun Oct 29 '12 at 14:13
    
@changokun: In languages? Not really. TMN's comment about web service calls is something to consider, but even the determination to skip going off-machine could be made by a local function. The decision to do a null/not-null check in a caller would have to be made after determining that the otherwise-useless function calls were causing an actual pain point. –  Blrfl Oct 29 '12 at 14:41
add comment

If passing null is legal from the point of view of the function that you call, adding an additional check outside the loop is superfluous, and should be avoided.

The classic example is C's free function which allows NULLs: technically, null-checking before the call does no harm, but it clutters the code, and has negative impact on readability.

With that said, you can save yourself some CPU cycles when you pass other parameters that must be prepared:

var index = 0
for(thing in array) {
    if(thing not null) x(thing, prepareOtherParameters(index))
    index++
}

is faster than the same call without a "guard", because calls to prepareOtherParameters is avoided.

share|improve this answer
add comment

I don't know how you populate the array, but if it is a database query, you can tell the SQL to omit the rows where that column is null. Maybe the correct thing to do is the array not having null values to begin with.

Either way, since parameter beeing null is not an error, then the first option is OK.

for(thing in array) {
    x(thing)
}

There shouldn't be any apreciable difference in performance, etc.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.