Declare variables at or near the top of where they are used?

Asked

Viewed 124 times

3

Is it better to declare all the variables right at the beginning of the file, even if they only come to be used, I don’t know, a thousand lines later? Or better go declaring as the program evolves?

My case is that I will unite several scripts in just one (I’m creating each function separately because it was getting too big and confusing), and I want to doubt if I should just copy and paste these files, with the variables where they are (what’s working)or if it is caught all variables and play at the beginning of this large file, and then I put only the functions.

What are the advantages and disadvantages of each of these options? Does it make a difference in performance? There is a path considered more correct?

Like, it’s better this:

// variáveis e função 1

$reds = $_POST ["Tredsalim"];
$redvalan = $_POST ["Treds4"];
$redvalan2 = $_POST ["Treds9"];

if ($reds == "sim") {
    $remmes += $redvalan + $redvalan2;
}

// variáveis e função 2

$difsal = $_POST ["Tdifsalim"];
$saldev = $_POST ["Tdate5"];
$saldev2 = $_POST ["Tdate9"];
$saldev3 = $_POST ["Tdate13"];

if ($saldev > 1) {
    $remmes = $saldev;
}
if ($saldev2 > 1) {
    $remmes = $saldev2;
}
if ($saldev3 > 1) {
    $remmes = $saldev3;

Or this:

// variáveis caso 1 

$reds = $_POST ["Tredsalim"];
$redvalan = $_POST ["Treds4"];
$redvalan2 = $_POST ["Treds9"];

// variáveis caso 2

$difsal = $_POST ["Tdifsalim"];
$saldev = $_POST ["Tdate5"];
$saldev2 = $_POST ["Tdate9"];
$saldev3 = $_POST ["Tdate13"];

// função 1

if ($reds == "sim") {
    $remmes += $redvalan + $redvalan2;
}

// função 2

if ($saldev > 1) {
    $remmes = $saldev;
}
if ($saldev2 > 1) {
    $remmes = $saldev2;
}
if ($saldev3 > 1) {
    $remmes = $saldev3;

1 answer

3


It is probably best to ensure that all variables should be declared at the beginning. This goes against what is usually recommended. But merging several codes is also not recommended. So since you are going to do something not recommended that at least try to minimize the damage and it will be minimized if you have to analyze all the code to discover all variables to declare at the beginning. Who knows finds that some bugs would be caused by overlapping variables or other conflicts.

In this specific example it is even more obvious that it should do this because in the background the variables are well related, in the background one group is continuation of the other.

This should be done only for the sake of organization, it does not influence performance.

Of course in another situation the change can change the logic of the code.

If you’re creating multiple functions in the same file, then it gets simpler because the variables turn to places, and it has to be all close by anyway, you can’t group the variables without creating new possible problems.

  • That’s what I needed to know. The scripts are well related even, and I just put in different files while I’m creating so as not to get confused. But I’m going to do just that, declare the variables first, and I’m going to add more comments too, because otherwise it’s going to be impossible to work properly. Thanks!

Browser other questions tagged

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