Which of == and =: = should I use?

The difference between equal and exactly equal word comparison operators explains the difference, but the important question also arises: what should I use when I do not compare floats with other things?

Erlang Pragmatic Programming recommends =: = and says that you should be suspicious of == and use it only when working with float. However, he also says that many existing codes do not follow this rule.

So, I have a little dilemma. Should I use "==" (even if not comparing floats with other values) for consistency with the surrounding code? Should I use "=: =" if necessary, even if it is incompatible with the rest of the file? Do I have to convert other expressions to a file in order to use "=: ="?

What are the tradeoffs? Is any operator more efficient than the other? If one of the operands is guaranteed not to be a number, does it matter which one I use? Are there any hidden traps (say, special float values ​​such as NaN, Inf, etc.), if Erlang supports them).

By the way, the code base I came across is ejabberd.

+6
source share
1 answer

Use =: = if you do not need to compare int with floats. The performance is the same (or at least the difference is too small to measure), but NaN, inf, etc. Do not exist in Erlang.

The reason that the OTP == library code is used a lot is probably because =: = is a fairly recent addition to Erlang.

+5
source

Source: https://habr.com/ru/post/916230/


All Articles