> From: Martin Heidegger [mailto:m...@leichtgewicht.at] 
> Sent: 22 February 2012 14:55
> I just remembered another thing that makes me a little mad every time I
read into coding with haXe...

The problem with your examples Martin is that you have the resultant code
equivalent slightly wrong unfortunately and this slight difference can be
the cause of problems. The correct version is:

   var a: String= ...;
   return a || "b";
   // equals
   return !(a == null || a == "") ? a : "b";

Whilst nowhere near as bad as JavaScript, the way that ActionScript will
implicitly cast anything to a boolean is a source of some horrible bugs.
Remember that null, 0 and "" all cast to false. So:

function f(p1:Object)
{
    // initialise p to a new object if null
   p1 ||= {};
   ...
}

breaks if f(false) or f(0) are called. This was a real-world bug with AS3
Signals at one point I believe.

Whilst ||= looks succinct, it is dangerous to use. We have banned it where I
work therefore and doing "if (obj != null)" rather than "if (obj)" is
mandatory for us. The improved reliability of the code outweighs the slight
disadvantage of longer syntax IMO.

David.

Reply via email to