3 ms·
I think I can argue that a raw "eval" is always going to be the worst way to achieve a given result. Since eval calls the entire system to do something, it inv
by joe_the_user 12y ago
I think I can argue that a raw "eval" is always going to be the worst way to achieve a given result.
Since eval calls the entire system to do something, it involves every possible danger that programming the system ordinarily involves and it involves dangers particular to it (not being able to directly check the syntax any of the code beforehand, constructing values with strings, the possibility the scope of variable-binding may vary from call to call, etc). The danger is inherent in the approach.
This isn't a reason to never use eval, just a reason to never use it if you can think of an alternative. Sometimes you can't find an alternative in the time allotted.
And if you use eval or any equivalent, just remember you have opened a bottomless can of worms. We have spent generations now cleaning up SQL injections, for example.
- vidarh 12y agoIt's not just the danger. In sufficiently dynamic languages, it wreaks havoc on optimization unless you're prepared to essentially re-jit the entire code. Ruby for example - if you enter eval() with a string, you may exit it to find that even basic stuff like adding two integers together has changed (and may suddenly have side effects), so any work you've done to inline (or cache) fast paths may suddenly have been undone. It's not likely that the worst changes will have happened, but they may, and so unless the string is constant and you can analyse it upfront, you need to be prepared for them unless you want to restrict yourself to a subset of the language.