On 23/02/2012 00:13, David Arno wrote:
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.
Hello David,

I am aware of that definition and I use that logic just for variables of custom types that can not be 0 but just null/undefined. Also it doesn't matter for internal data holders. I am also aware that null and undefined are not the same. I use it specially for object pooling a lot and it allows lazy initialization
with relatively few code.

Also another thing is that

if(a == null) is slower than if(a) .... at least compiled with mxmlc.

yours
Martin.

Reply via email to