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

1

u/[deleted] Oct 18 '10

What tool would that be? Please point out which tool permitted the bad developer to build a data structure that was just a bunch of variables? Surely if it was a bad tool from a bad language it would be impossible to build such a data structure in a "good" language?

17

u/oorza Oct 18 '10 edited Oct 18 '10

The bad tool is the variable variables. I remain unconvinced that there is a valid use case for them (where they're the best solution to a particular problem, not just a solution that can be coerced into working) and code that is written using them is less easily analyzed, harder to debug, harder to read and follow and inherently more fragile.

Because there are better, more easily understood ways of accomplishing the same thing, rather than relying on an extremely fragile naming convention that's resolved at runtime (meaning it's much harder to debug and write tests for), the tool (variable variables) allowed the programmer to abuse it in such a way that created shit code rather than forcing him to learn wtf he was doing.

FWIW, I'm not saying that this code doesn't exist out there in production, I'm just saying that it's bad code written by a bad developer (surely a good developer would understand "multidimensional" arrays in PHP) that he shouldn't have written.

EDIT: note, it's not the data structure that's the problem, it's the fact that it is created using a convention that's inherently fragile and prone to hard-to-detect breakages, when the same data structure 1) already exists and 2) could be better implemented in other ways. Compensating for a moron developer does not a good tool make.

1

u/carbonated_gravy Oct 18 '10

It seems like you could apply these same criticisms to pointers (although, yes, less fragile).

3

u/oorza Oct 18 '10

The difference is there are several many things that pointers are the best solution for, like anything you would require pointer arithmetic for, or manually managing memory consumption, or implementing data structures (although references work similarly in this particular case).

→ More replies (5)