Saturday 16 February 2013

Operator precedence: difference between ColdFusion and Railo

This thing still remains a bit quiet. I'm currently in New Zealand and it's nice and summery here so there's less motivation to be sitting in front of the screen typing stuff about ColdFusion. And "the screen" is only my little wee netbook which isn't the most conducive to writing code & blog articles simultaneously as I can only fit one app on the screen at a time, and even then it's only  about 2/3 the size to be useful (1024x600). Still: it's good for watching movies on the flight from London (25 hours for those who haven't done it), and it is quite impressive in that it will run CF9, CF10, Railo and MySQL simultaneously without a problem, as well as a couple browsers and CFB.

Anyway, there was an interesting thread on the Railo Google group whilst I was in the air, and I've finally had a chance to look into it now.

The issue is an incompat between Railo and ColdFusion when it comes to the operator precedence of the * and \ operators.

Consider this code:

a = 700;
b = 50;
c = 3; 
results = {
    "1. a \ b * c"        = a \ b * c,
    "2. (a \ b) * c"    = (a \ b) * c,
    "3. a \ (b * c)"    = a \ (b * c)
    a    = a,
    b    = b,
    c    = c,
    results = results

On CF the output is:

1. a \ b * c4
2. (a \ b) * c42
3. a \ (b * c)4

Railo differs:

1. a \ b * c
2. (a \ b) * c
3. a \ (b * c)

The thing to look at here is that ColdFusion gives * precedence over \ (so the first equation results in 4); whereas Railo gives them equal precedence, so just evaluates the expression left to right, performing the \ operation first (resulting in 42).

My schooling (which was so long ago it was held in the local cave, and really just covered how to hunt dinosaurs) taught me that multiplication and division have equal precedence, so - on this basis - Railo is getting it right. However \ isn't a vanilla division operator, it does integer division; so is possibly not, strictly speaking, covered by the basic BODMAS rule. Although - equally - there's no reason to think it should be treated any differently to /, as they're both still division operations. Hmmm.

I googled about and did not find any hard and fast rule or precedent to cover this, indeed in most situations the distinction wasn't made between the two operators. However Adobe have actually specifically documented the order of operations here, and it explicitly states * and / take precedence over \.

Now I don't know whether ColdFusion should be making this distinction, but I reckon if they've made the point of implementing it this way, and actually documenting it, then it was a concerted decision to do so. For whatever reason. From a Railo perspective, I don't think there's any good to be had in this behaviour being different, so I think this should be considered a bug in Railo. I've mentioned this on that thread, and await with eager anticipation what other people think.

Now... back to watching Saturday night TV with me Mum & Dad.