I know that Lisp and Scheme programmers usually say that eval
should be avoided unless strictly necessary. I’ve seen the same recommendation for several program
eval
(in any language) is not evil in the same way that a chainsaw is not evil. It is a tool. It happens to be a powerful tool that, when misused, can sever limbs and eviscerate (metaphorically speaking), but the same can be said for many tools in a programmer's toolbox including:
goto
and friendsIf you find yourself having to use any of these powerful, potentially dangerous tools ask yourself three times "why?" in a chain. For example:
"Why do I have to use
eval
?" "Because of foo." "Why is foo necessary?" "Because ..."
If you get to the end of that chain and the tool still looks like it's the right thing to do, then do it. Document the Hell out of it. Test the Hell out of it. Double-check correctness and security over and over and over again. But do it.