Tweeted twitter.com/#!/StackProgrammer/status/261890378712629248
2 Capitals help for readability
source | link

what What is the best practice for when to check if something needs to be done?

let'sLet's say iI have a function that does xx. iI pass it a variable, and if the variable is not null, it does some action.

and i And I have an array of variables and i'm gonnaI'm going to run this function on each one.

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

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

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

thisThis 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?

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

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

pseudoPseudo code example:

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

versusVersus:

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

ifIf there are language-specific concerns, i'mI'm a web developer working in phpPHP and javascriptJavaScript.

thanks!

what is the best practice for when to check if something needs to be done

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 gonna run this function on each one.

inside the function, it seems like a good practice 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.

thanks!

What is the best practice for when to check if something needs to be done?

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.

1
source | link

what is the best practice for when to check if something needs to be done

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 gonna run this function on each one.

inside the function, it seems like a good practice 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.

thanks!