When to use IMP operator in ColdFusion?

443 views Asked by At

Implication: The statement A IMP B is the equivalent of the logical statement “If A Then B.” A IMP B is False only if A is True and B is False. It is True in all other cases.

http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec09d55-7ffc.html

I can vaguely remember what is an "implication" from college. When to use IMP operator in the real world?

3

There are 3 answers

1
Henry On BEST ANSWER

After I applied my google-fu

Found this: http://www.cfug-md.org/meetings/RichardBierregaardLogicwCFConditionals.ppt

and it inspires me to find out that IMP might be useful for writing unit test:

assertTrue(Income >= 200000 IMP TaxRate == 0.35);
assertTrue(Income < 200000 AND Income >= 70000 IMP TaxRate == 0.28);
assertTrue(Income < 70000 AND Income >= 35000 IMP TaxRate == 0.20);
assertTrue(Income < 35000 AND Income >= 15000 IMP TaxRate == 0.10);
assertTrue(Income < 15000 IMP TaxRate == 0);

instead of

if (Income >= 200000) assertTrue(TaxRate == 0.35);
if (Income < 200000 AND Income >= 70000) assertTrue(TaxRate == 0.28);
if (Income < 70000 AND Income >= 35000) assertTrue(TaxRate == 0.20);
if (Income < 35000 AND Income >= 15000) assertTrue(TaxRate == 0.10);
if (Income < 15000) assertTrue(TaxRate == 0);

Do you think IMP version is better?

3
Adam Cameron On

In the real world, it would be handy to be able to do something like this, to perform validation on optional params:

<cfif structKeyExists(URL, "a") IMP validateId(URL.a)>

Wherein we only care about validation of URL.a if it exists. That'd be about the most useful application of IMP, IMO (well, like, it would be).

However due to a bug in the implementation of IMP, this doesn't work :-(

I think Dale's assertion that the much longer (and incorrect) logic is easier to read than the shortened version is self-evidentally wrong, and based on a specious premise to boot. His position is based on an idea that "someone not knowing something" is a continuous state, ie: when someone doesn't know something (like what "IMP" means), then they will forever not know it. This is not true. A person might not know something initially, but once they find out about it, then they will know about it. So the problem of not knowing how the IMP oerator works is a very short-lived one.

I don't think situations arise wherein IMP is needed arise very often, but it's handy to have it there. And it'd be handier still if it worked properly ;-)

6
Dale Fraser On

I think you should stay clear of it, I've never seen it used and never had a need to use it. Other developers mostly wont understand what it is or means.

I would rather write

<cfif a eq true and b eq false>

than

<cfif a imp b>

The first is much clearer.