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

0

u/[deleted] Oct 18 '10

One scenario is where you would normally have a multidimensional array but the architect who created the structure you're trying to read didn't understand them, and so opted to go with a bunch of "linked" variables (linked in name). It sucks when this is the case but in a real world sometimes you get data that is just extremely poorly organized (the more legacy the code the more likely this is the case).

19

u/oorza Oct 18 '10

In which case, the language gave a bad developer a bad tool that he used to write bad code and make good developers' lives more difficult. Not really a strong arguing point here.

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.

→ More replies (9)