What is the difference between using variable and passing direct parameter?

2

I wanted to know the real difference and between

$sql = 'select * from tbl_blablabla';
$result = $connect->prepare(sql);

and

$result = $connect->prepare('select * from tbl_blabla');

Not only in this specific case, but several other cases I've seen a lot of people doing a lot of variable to receive parameters being that if you type less in example 2, does this affect performance? Safety? If security matters, what exactly happens?

    
asked by anonymous 08.05.2018 / 21:41

2 answers

6

In this example the difference is that you have written one more line in the first one.

There are zero advantages out there, and the only reason I see people using it like this is that they do not know what a and for what it serves. Of course, the person's taste may be another reason, but there is no reason.

If I had a more complex expression I would even understand what was done before and save it in a variable with a meaningful name. A name that is not informative or informs the truism does not increase legibility.

If the value was used elsewhere, it has the exact reason for a variable to exist.

Doing something because one day you might need it violates the YAGNI principle, and to a certain extent KISS . Having a good IDE change the expression to a variable if you need is the simplest thing you have.

It's not to save code, it's to simplify the code. It is debatable, but putting the statement / definition of what you will use as close to where it is used usually gives you more readability. Long code is harder to follow. Outside the semantics that if there is a variable is why it should be used in several places. Code is expression, when expressing wrong passes the wrong idea and makes the code convolute.

Depending on the language there is difference in performance and even memory consumption.

Never do anything without knowing why you are doing it. Do not go in the automatic, do not follow rules that others have said is good. Look for what is good for you, but do it with awareness of choice.

    
08.05.2018 / 21:57
0

It's basically the same thing as:

int numero = 1;
printf("%d",numero);

and

printf("1");

The difference is that at first you had to declare the variable (and store memory for an integer, 4 bytes) and in the second not.

    
09.05.2018 / 15:38