Intermediate Variables

Intermediate Variables

2017-11-08 paulf:

In my code, you will find a lot of what might be called “intermediate” variables. They are separate variables from a prior process that are now used elsewhere. You’ll often find them in this sort of configuration:

$intermediate_variable = process();
return $intermediate_variable;

The above code could be written this way:

return process();

A lot of programmers will abbreviate things to the n’th degree, just because it looks kewl and they think other programmers will marvel at the economy of their code. Yeah, okay. Whatever.

There will be inevitable complaints about this aspect of my code, since it slows down the code and uses up memory needlessly. So let me tell you why I do it:

Testing and debugging.

I can’t tell you how many times I’ve needed to test the value of a variable returned by a process, but didn’t have a variable to examine. I had to go back and break the process down so I’d have something to verify that the former process worked the way it was supposed to.

I could take these variables out, once I’ve verified that a process does what it’s supposed to do. But if I change the way the process works internally, or change the process returning the value, I may need that variable for testing again. The cost is not that great, neither in time nor memory, so I prefer to leave them in place.

I don’t do this universally. But I do it a lot, enough that a seasoned programmer would notice the habit. It’s a judgment call.

And since it’s my code, it’s my judgment. Do as you will with your code.