Zend certified PHP/Magento developer

Do “unnecessary” variables matter that much?

I had a discussion a few months ago with my (previous) boss about unnecessary variables where as my boss thinks a lot of variables I created are unnecessary, while those I do extra, are the ones I use for readability.

What does my boss mean with unnecessary here? Unnecessary in his eyes is: as long as you don’t need the variable, then don’t create it.

I think we all agree on that statement, but there are different opinions about specific scenarios. Let me give you an example of unnecessary variables that me and my boss argued about:

This is what I would do:

“` < ?php

class Database extends BaseClass {

public function getQuery() { $clauses = [‘user_id’ => $this->user()->id]; return $this->_query($clauses); }

} “`

This is what my boss would do:

“` < ?php

class Database extends BaseClass {

public function getQuery() { return $this->_query([‘user_id’ => $this->user()->id]); }

} “`

The reason I am adding a variable $clauses is because if I create this function and someone else reads it when the other person should know, without looking up the function definition, what I am passing into the $this->_query() function. That array could be anything, so I decide to name the chunk of code using a variable. This improves readability for other people that will read this code later on.

Now the discussion point here is: does this variable matter that much? I presume a single variable isn’t noticeable, but imagine there are thousands of unnecessary variables like this. How much does this influence my performance of my webapplication and should I be worried about this?

I have been programming for 8+ years but my knowledge of these things is knowledge I lack hard. I don’t know how a single variable is handled in the background when it’s processed when I run it in the browser and how many unnecessary variables are needed to influence performance.

Correct me if I am wrong, I presume the knowledge I am missing is how compilers works for languages like this where I know how variables are parsed and what is being done under the hood.

As I repeat, I’d like to know from everyone: how much do unnecessary variables influence my performance and does a single one of them matter? Can I use an unnecessary variable to improve readability?

submitted by /u/koomenk
[link] [comments]