2

Ok, first of all, i suspect this is going to be closed.

Right, i have a question relating to using function calls inside statements as opposed to assigning to a variable first.

For example:

(code is in php, but question applies generally. Also, code is overly simplified)

if (myAmazingFunction() === true) {
    // do something amazing
}

instead of

$amazingresult = myAmazingFuncton();
if ($amazingResult === true) {
    // do something amazing
}

The question is:

  1. Is there any performance, or other underlying pros or cons to each approach
  2. Stylistically, is any of the approaches considered better than the other
4
  • 4
    if checking true always use === is 1 rule of thumb! Commented Aug 22, 2012 at 14:42
  • Either is valid; just listen to @Zac. Use === when comparing boolean values (unless you just want something truthy or false-y, in which case == is fine). Commented Aug 22, 2012 at 14:42
  • There is no need to save the result in variable if you don't need it later on ;-)). Both versions are totally legal :-) Commented Aug 22, 2012 at 14:43
  • I would always do the second, you may need to check the result again further in the script and you don't want to rerun the function. Commented Aug 22, 2012 at 14:44

3 Answers 3

2

In most languages, there will be no performance difference. In the first case, the compiler will allocate storage for the result of the function call before checking whether it is true. In the second case you're simply making this explicit.

If you are debugging, sometimes the second form is easier, as you can set a breakpoint on the second line and check the value returned by the function before the comparison is made - but then you see the result of the function by the path the executing code takes anyway in the example you've given. You can also re-use the value without rerunning the function, as Zac says in his comment.

Stylistically, this is going to be largely subjective. The only thing I'd say here is that if your variable name makes the purpose of the function output clear, then you might be adding something to the ability for others to understand your code easily.

Sign up to request clarification or add additional context in comments.

Comments

1

@DavidM's answer is correct. However, I'd just like to add that stylistically, I think it depends on the name of the function and its context.

Example:

if ($food->tastesGood()) {
    echo 'Mmmm!';
}

// vs.

$foodTastesGood = $food->tastesGood();
if ($foodTastesGood) {
    echo 'Mmmm!';
}

In this case, it's very clear that the return value of the method tastesGood() is going to be a boolean from both the name of the method and its context. Using a temporary variable adds nothing to your code except making it redundant and less-readable at a glance. In addition, if the variable is not defined right before its used, then you have to go find the definition to understand the condition. In these cases, I would say use of a variable is worse.

Another example:

if ($dishes->wash() !== FALSE) {
    echo 'Sparkly!';
}

// vs.

$dishesAreClean = $dishes->wash() !== FALSE;
if ($dishesAreClean) {
    echo 'Sparkly!';
}

In this case, we can't really infer the return type of the wash() method from its name, and indeed, it would seem that it returns nothing on success and FALSE on errors. Checking if the dishes are clean then requires us to make sure that there were no errors, but the first case doesn't make for particularly readable or self-documenting code. The second case, however, adds very explicit information about what's going on by way of the temporary variable. In these cases, I would say use of a variable is better.

Comments

0

Is there any performance, or other underlying pros or cons to each approach

Performance-wise, assigning an extra variable that you will use only in your if condition will use extra memory, and one useless line of code. So it will use more memory. Will it be noticeable? Probably not.

Stylistically, is any of the approaches considered bad

Using the method in your if statement is perfectly valid, and I think it's a better approach, since you can read the code and see exactly what value is being tested in the if condition. No need to look for the variable and search where it was affected.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.