Why exactly is eval evil?

前端 未结 12 1657
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 09:51

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

12条回答
  •  一生所求
    2020-11-22 10:19

    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 friends
    • lock-based threading
    • continuations
    • macros (hygenic or other)
    • pointers
    • restartable exceptions
    • self-modifying code
    • ...and a cast of thousands.

    If 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.

提交回复
热议问题