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

Asked

Viewed 379 times

6

I used to get "reassigned" a value of a certain 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 to say: $string is the first parameter of format, and I changed the value of the variable $string (which is the parameter variable) within that function.

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

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

    return "[{$new_value}]";
}

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

So:

  • Is there any recommendation or standard that instructs regarding this "re-allocation of the parameter"?

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

  • In case the variable is global and it has some other utility, then it would give a huge problem, I do not believe that there is any problem, it depends on the scope of the project.

  • @Marcelobonifazio, using global variable, by itself, is already a bad practice. In this case then...

  • It’s never a problem if you have conviction of what you’re doing.

2 answers

7


In general there are no problems.

Can whether the past type has semantics of passage 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, which would then have no problems because obviously it could not be used elsewhere, but in this case there was more the passage of the reference and not by reference.

In a kind string a reference is passed and does not copy the data as occurs with a number for example, but the passage is not by reference, so any change in it, does not reflect the variable used in the call.

Then nothing changes at all. Actually your example seems very interesting to reassign a value to the same parameter variable because in the background is the same given.

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

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

I know it’s just an example but since we’re on the subject, in which case the variable is even needed, this function would only need to have the return with the expression. That’s why I said it depends on the context whether you need to give readability or not. In general I think you don’t have to keep wanting 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 cannot discard it. And it would be silly to avoid discarding save this value in another variable and reassign a new value in this parameter.

1

No problem.

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

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

Below I have cited some practical examples, including 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
  • In this case of the object, it is usually already expected that the parameter is an object. And you’re not changing the variable to another value, but the property of the instantiated object

  • 1

    @Wallacemaxters Yes, just put to make clear the greatest number of possibilities.

Browser other questions tagged

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