Is it a bad practice to override variables declared as a function parameter?

6

I had the habit of "reassigning" a value of a particular variable that was declared as nome of the function parameter.

Example:

function format($string)
{
    $string = ltrim(rtrim($string, ']'), '['); // A questionada atribuição

    return "[{$string}]";
}

That is, $string is the first parameter of format , and I have changed the value of the $string variable (which is the parameter variable) within that function.

On the other hand, could also have done as follows:

function format($string)
{
    $new_value = ltrim(rtrim($string, ']'), '['); // A questionada atribuição

    return "[{$new_value}]";
}

Then I started to question the kinds of problems that could be generated by doing things like that.

Then:

  • Is there a recommendation or some standard that instructs you about this "parameter reassignment"?

  • In which cases would this reallocation cause me problems - or would it solve the problem?

asked by anonymous 11.08.2015 / 17:19

2 answers

7

In general there are no problems.

There may be if the passed type has passing semantics by reference. Obviously in this case the change will affect the variable that was used as an argument, if a variable was used instead of a literal, that there would be no problems because obviously it could not be used elsewhere, but in this case there was more passing reference and not by reference.

A string type passes a reference and does not copy the data as it does a number, for example, but the pass is not by reference, so any change in it does not reflect the variable used on the call.

Then nothing changes. In fact your example seems very interesting to reassign a value to the same parameter variable because the background is the same given.

I will not go into the merit of the parameter name to be bad because it is an example, but in a condition style question this can be important. If you are using meaningful names, assigning them to another variable can make a difference in readability, have names that indicate what that text is. If the parameter was called $texto , it could call the new variable of $texto_formatado to make it clear that there the content had the change. Give meaning to content.

Reinforcement that in simple cases so this type of readability is overkill and does not make sense, but can make in code more complex. While some will say more complex codes is already a bad practice.

I know it's just an example but since we're on the subject, in this case the variable is not even needed, this function would only need to have the return line with the expression. That's why I said it depends on the context whether you need to readable or not. In general I think you do not have to be willing to document all operations by creating intermediate variables, I only do this when it's really confusing, but it's taste.

Of course, if you need to use the original value of the parameter other times, you can not discard it. And it would be silly to avoid discarding this value in another variable and reassigning a new value in this parameter.

    
11.08.2015 / 17:32
1

No problem.

Within a function , any change in value in a variable normally passed $variavel , will only have effect within the context, which in this case is within that function .

One occasion where it would have effect in another context, for example in who called this function , is if the variable was passed by reference &$variavel .

Here are some practical examples, even when it comes to objects:

// caso 1
funcaoQualquer($var) {
    $var = 10;
    var_dump($var); // imprime 10
}

$variavel = 1;
funcaoQualquer($variavel); // chama a função        
var_dump($variavel); // imprime 1


// caso 2
funcaoQualquer(&$var) {
    $var = 10;
    var_dump($var); // imprime 10
}

$variavel = 1;
funcaoQualquer($variavel); // chama a função
var_dump($variavel); // imprime 10


// caso 3
funcaoQualquer($var) {
    $var->propriedade = 10;
    var_dump($var->propriedade); // imprime 10
}

$variavel = new Object();
funcaoQualquer($variavel); // chama a função
var_dump($variavel->propriedade); // imprime 10
    
11.08.2015 / 17:53