0plus1
0plus1

Reputation: 4555

Is it better to call function in a nested way or to split each passage inside a var?

Take these three examples:

ONE

return lowercaseKeys(json_decode(trim($json),true));

TWO

$trimmed = trim($json);
$array = json_decode($trimmed,true);
$return = lowercaseKeys($array);
return $return;

THREE

$return = trim($json);
$return = json_decode($return,true);
$return = lowercaseKeys($return);
return $return;

Aside from readability, which is the best performance wise? What is considered best practice?

p.s. code is only an example, not related to the question, I just copy pasted from my notepad window.

Upvotes: 5

Views: 110

Answers (8)

Mike
Mike

Reputation: 923

Number one rule is do whatever is most readable when dealing with micro-optimizations. But here is a small test I did.

<?php
$iterations = 1000000;
$tests = array('one', 'two', 'three');
$json = json_encode($tests);

foreach ($tests as $function) {
        echo $function;
        $start = microtime(true);
        for ($i = 1; $i <= $iterations; $i++) {
                $function($json);
        }
        $end = microtime(true);
        echo ' - ' . ($end - $start) . " sec\n";
}

function one($json) {
        return array_change_key_case(json_decode(trim($json),true), CASE_LOWER);
}
function two($json) {
        $trimmed = trim($json);
        $array = json_decode($trimmed,true);
        $return = array_change_key_case($array, CASE_LOWER);
        return $return;
}
function three($json) {
        $return = trim($json);
        $return = json_decode($return,true);
        $return = array_change_key_case($return, CASE_LOWER);
        return $return;
}
?>

Results:

one - 3.3994290828705 sec
two - 3.5148930549622s sec
three - 3.5086510181427s sec

Option one is indeed a tiny bit faster, but that was one million iterations, and the time difference still wouldn't even be noticeable. With smaller amounts of iterations, the timing is too variable to even have a pattern to declare one better than the other.

Upvotes: 1

Phill Pafford
Phill Pafford

Reputation: 85318

Personally I like option one, as for your questions:

which is the best performance wise? Option One

What is considered best practice? Option One and Two depending on the complexity of the function.

But let's look at how they look as a function

Option One:

// Simple, readable
function optionOne($json) {
    return lowercaseKeys(json_decode(trim($json),true));
}

Option Two:

// Still readable with a little more detail to a novice developer
function optionTwo($json) {
    $trimmed = trim($json);
    $array   = json_decode($trimmed,true);
    $return  = lowercaseKeys($array);
    return $return;
}

Option Three:

// This might cause some problems with the $return variable
// Still looks like it would work but I'm not fond of this option
function optionThree($json) {
    $return = trim($json);
    $return = json_decode($return,true);
    $return = lowercaseKeys($return);
    return $return;
}

Upvotes: 0

ArrayDude
ArrayDude

Reputation: 74

I like the first option, and it use less memory but the difference is not significant

Upvotes: 0

user527892
user527892

Reputation:

I tend to do thing the 'option 2' way, myself. You never know when you might need something from half way through. Helps with readability too. Ok, it's not quite as efficient but for the sake of being able to understand and edit your code at a later date (especially if someone else has got to do it), option 2 feels best to me.

Upvotes: 0

Joel Etherton
Joel Etherton

Reputation: 37533

Option 1 will be maybe a microsecond faster than the other 2 options and use a few bits less physical memory, but any difference will be completely negligible unless multiplied on an exponential level. The core here is going to be the ultimate readability. I find Option 1 to be perfectly suitable personally, but I recognize that in some of the teams where I work, this option would not meet the standard for the lowest common denominator of developer. Option 2 and Option 3 really are exactly the same since you're not doing anything with the extra variables that are created. They both create 3 separate tokens. Option 2 is more explicitly readable in that the variables describe the method being applied at that stage, so I would vote for 2 as well.

Upvotes: 0

gamen
gamen

Reputation: 1007

If PHP is doing it right, these should all be the same once tokenized. Assuming that's true, option two is the most readable and easiest to modify in the future. Perhaps this page is worth reading.

Upvotes: 0

Kristoffer Sall-Storgaard
Kristoffer Sall-Storgaard

Reputation: 10636

There should be no difference performance wise in your example.

Upvotes: 0

xil3
xil3

Reputation: 16439

Performance wise, I don't think you'd really notice any difference. Would be minuscule...

But I vote for option 2, because it's the easiest to read (as opposed to option 1), and I don't think it's a good idea to keep overwriting it like you do in option 3 (not very good form).

Upvotes: 0

Related Questions