Why would a language NOT use Short-circuit evaluation?

后端 未结 18 2001
天涯浪人
天涯浪人 2020-12-03 09:42

Why would a language NOT use Short-circuit evaluation? Are there any benefits of not using it?

I see that it could lead to some performances issues... is that true?

18条回答
  •  孤街浪徒
    2020-12-03 10:07

    I'd say it's valid for readability issues; if someone takes advantage of short circuit evaluation in a not fully obvious way, it can be hard for a maintainer to look at the same code and understand the logic.

    If memory serves, erlang provides two constructs, standard and/or, then andalso/orelse . This clarifies intend that 'yes, I know this is short circuiting, and you should too', where as at other points the intent needs to be derived from code.

    As an example, say a maintainer comes across these lines:

    if(user.inDatabase() || user.insertInDatabase()) 
        user.DoCoolStuff();
    

    It takes a few seconds to recognize that the intent is "if the user isn't in the Database, insert him/her/it; if that works do cool stuff".

    As others have pointed out, this is really only relevant when doing things with side effects.

提交回复
热议问题