What is the best way to perform a dependent addition/subtraction operation?

Asked

Viewed 49 times

4

It is common when one is at the beginning to come across this doubt.

I have to perform an operation that is dependent on a condition :

Example 1

$operacao = 'somar';

$valor1 = 5;
$valor2 = 10;
$saldo = null;

if($operacao == 'somar'){
    $saldo = $valor1 + $valor2;
}else{
    $saldo = $valor1 - $valor2;
}

if($saldo != null){
    echo $saldo;
}

Example 2

$operacao = 'somar';

$valor1 = 5;
$valor2 = 10;
$saldo = null;

$valor2 *= (($operacao == 'somar') ? 1 : -1);
$saldo = $valor1 + $valor2;

if($saldo != null){
    echo $saldo;
}

Question

What is the best way to accomplish this task, so that it does not occupy many lines, nor much memory?

1 answer

3


It really tastes like.

Many people like the first because it seems clearer.

For me both are clear. Those who understand the programming can understand well any one of them.

Some people will say that the second example is a way clever (Clever) to do the same. And this can be good or bad depending on the context. Often the term is used pejoratively.

I would probably do so, after all the second example is not equivalent to the first:

$operacao = 'somar';
$valor1 = 5;
$valor2 = 10;
$saldo = null;
$saldo = $valor1 + $valor2 * ($operacao == 'somar' ? 1 : -1);
if ($saldo != null) echo $saldo;

Or you could use:

$saldo = $valor1 + ($operacao == 'somar' ? $valor2 : -$valor2);

Maybe I had a function for that, simply:

function InvertSignalConditional($value, $condition) {
    return $condition ? -$value, $value)
}

I put in the Github for future reference.

Note the inversion I did to be correct, after all you want the inversion if the condition is true.

In some cases one can opt more for one than the other for being more expressive of what the intention is. Some will say that the first example of the question is more expressive. This is an imperative way of seeing things, for those who know functional programming, which is much more expressive than the imperative, know that the second example is preferred.

  • But what about the memory?

  • Little memory, no?

  • Regarding function, not to need to do $var = Invert($var), I would change to Invert(&$var).

  • @Guilhermelautert there is someone who knows how to use references, rsrsrs. I don’t usually visualize usability for them, but this time I realize.

  • @Guilhermelautert What has the memory? I think would not do the function so, have to have better reason to generate side effect. In addition to what, again, could not be used in the case of the first example, they would not be equivalent, and the function would be too specific for other cases. If it simplifies the name, it loses the expressiveness that was the purpose of the function, if it goes to do something little expressive, then I prefer to write the code.

  • No, I narrowed down the name just for example, would keep yours, just wanted to comment on the pointer. As for memory, what I’d like to know is which one would use the least.

  • I don’t know, I would have to measure, maybe it’s the same, but if the difference is so little, but so little, that it doesn’t make any difference, especially in PHP. We are no longer programming in Sinclair with 2KB when I had to use "1" where I could use number because it saved 1 precious byte :D And it would be difficult to compare because the examples are not equivalent. In theory the first should occupy just over half a dozen bytes less because it does not need to store 2 constants. But my alternative would not cause this and would probably be the same.

  • 1

    I don’t know the memory composition of PHP deeply, but I know that it wastes a lot. So it’s not worth trying to keep looking at it. If it’s relevant, go to C/C++/Assembly or something :)

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.