Short circuit evaluation relying on Java (coding style)

Is there a good coding style that relies heavily on short circuits in Boolean evaluation?

I know who likes it For example, if the business logic is "if Alice is not hungry, or if Alice and Bob are hungry", instead of writing

// if Alice is not hungry or both alice and bob are hungry
if (!A || A && B)`

He can write

// if Alice is not hungry OR both alice and bob are hungry
if (!A || B)

It is considered that 𞓜 is short circuited, so the right operand is evaluated if and only if the first is false (which means a = true)

(the annoying thing is that at first glance, you think it's a mistake, but later you think you'll look stupid if you change it to more obvious!)

Solution

You can and should rely on expression shorting, but the example you give is just bad programming The logic of the expression should match the comment and human - readable test logic The optimizer fully understands Boolean logic and optimizes any obvious inefficiencies that your teammates may complain about

The most important thing is to make the developer's code clear and easy to understand Writing smart code to prove how smart you are is never a good habit

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>