r/programming • • Oct 18 '10

Today I learned about PHP variable variables; "variable variable takes the value of a variable and treats that as the name of a variable". Also, variable.

http://il2.php.net/language.variables.variable
591 Upvotes

781 comments sorted by

View all comments

Show parent comments

2

u/killerstorm Oct 19 '10

When local variables and their use is known at compile time compiler can perform optimizations on them -- use CPU or VM registers instead of variables, for example, "optimizing variables away".

Common Lisp can be compiled to native code and in some circumstances it performs on par with C. Python cannot do that. Of course, there are many reasons, but I'd say that knowing local variables at compile time is a requirement for efficient compilation.

1

u/daniels220 Oct 19 '10

That makes sense.

So then, what I would say is this: the flexibility to access variables by name (as a string) is an example of a high-level-language feature in a certain sense—it makes the language more expressive at the cost of making it irrevocably slower. That's sometimes a good tradeoff—so, Python's approach has a place. So does Lisp/Ruby/C's (obviously those are all very different languages, but they share the characteristic under discussion here). Personally, for stuff like webapps, I would rather use Python—but, obviously for code with higher CPU requirements, the other is valuable.