Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which one of == and =:= should I use?

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.

like image 727
Marcin Owsiany Avatar asked May 21 '12 08:05

Marcin Owsiany


1 Answers

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.

like image 95
Lukas Avatar answered Nov 10 '22 14:11

Lukas