Difference between equal to and exactly equal to term comparison operators explains the difference, but an important question is also: which one should I use, when I don't compare floats to other things?
"Pragmatic Programming Erlang" recommends =:= and says you should be suspicious about == and only be using it when dealing with floats. However it also says that a lot of existing code does not follow this rule.
So I have a little dilemma. Should I use "==" (even when not comparing floats to other values) for consistency with surrounding code? Should I use "=:=" as appropriate, even if this would be inconsistent with the rest of the file? Should I convert the other expressions in the file to use "=:="?
What are the tradeoffs? Is either operator any more efficient than the other? If one of the operands is guaranteed to not be a number, does it matter which one I use? Are there no hidden traps (say, wrt. special float values like NaN, Inf, etc.. - in case Erlang supports these).
BTW, the codebase I'm facing is ejabberd.
Use =:= if you do not need to compare ints with floats. The performance is the same (or at least the difference is too small to measure) and NaN, inf etc do not exist in Erlang.
The reason why a lot of OTP library code used == is probably because =:= is a rather recent addition to Erlang.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With