Iif() Evaluation Weirdness
All: I've run into a problem that I could use some new eyes on... I have a function inside a CFC that accepts a query (importData) and a series of indicators (nicknameInd) that indicate if the value from the query should be used (indicator = 1) or not (indicator = 0). In the below code, the first argument in the IIF() function evaluates to false (both conditions are false) so only the third argument, De(), should be evaluated. However, for some reason, both the second and third arguments are being evaluated and I'm getting an error message that Element NICKNAME is undefined in IMPORTDATA. Because importData.nickname is not defined. cfset variables.nickname = Iif( (arguments.nicknameInd NEQ 0) AND (IsDefined(importData.nickname)), De(importData.Nickname), De()) I've replaced both the second and third arguments with debugging code that showed that they are both being evaluated, regardless of the condition in the first argument. Not sure why that is though given that they're both wrapped in DE() functions. Any suggestions? Thanks. -- Mosh Teitelbaum ~| Order the Adobe Coldfusion Anthology now! http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:360045 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm
RE: Iif() Evaluation Weirdness
Dave Watts wrote: cfset variables.nickname = Iif( (arguments.nicknameInd NEQ 0) AND (IsDefined(importData.nickname)), De(importData.Nickname), De()) CF is going to verify that the variable exists, because you're referring to it. That's what's happening here. Just because you're using the DE() function doesn't mean that CF isn't going to evaluate importData.nickname at runtime to verify that there's actually something called importData.nickname in case it needs to reference that value. Dave: Thanks for the reply. My misuse of terminology aside, that would mean that the following cannot be converted to using IIF(): cfif IsDefined(importData.nickname) cfset variables.nickname = importData.nickname cfelse cfset variables.nickname = /cfif I thought the whole point of the DE() function was to delay evaluation of its argument until it was actually needed (e.g., when the IIF() function's first argument evaluates to true). If not, then the only way to use a variable, that may not be defined, is by way of the full cfif treatment. Is that right? -- Mosh Teitelbaum ~| Order the Adobe Coldfusion Anthology now! http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:360047 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm
Re: Iif() Evaluation Weirdness
I've run into a problem that I could use some new eyes on... I have a function inside a CFC that accepts a query (importData) and a series of indicators (nicknameInd) that indicate if the value from the query should be used (indicator = 1) or not (indicator = 0). In the below code, the first argument in the IIF() function evaluates to false (both conditions are false) so only the third argument, De(), should be evaluated. However, for some reason, both the second and third arguments are being evaluated and I'm getting an error message that Element NICKNAME is undefined in IMPORTDATA. Because importData.nickname is not defined. cfset variables.nickname = Iif( (arguments.nicknameInd NEQ 0) AND (IsDefined(importData.nickname)), De(importData.Nickname), De()) I've replaced both the second and third arguments with debugging code that showed that they are both being evaluated, regardless of the condition in the first argument. Not sure why that is though given that they're both wrapped in DE() functions. Any suggestions? The word evaluated here, I don't think it means what you think it means. Whenever you have a CF statement, the entire statement is evaluated. So, if you have something like this: function(variable) CF is going to verify that the variable exists, because you're referring to it. That's what's happening here. Just because you're using the DE() function doesn't mean that CF isn't going to evaluate importData.nickname at runtime to verify that there's actually something called importData.nickname in case it needs to reference that value. Dave Watts, CTO, Fig Leaf Software 1-202-527-9569 http://www.figleaf.com/ http://training.figleaf.com/ Fig Leaf Software is a Service-Disabled Veteran-Owned Small Business (SDVOSB) on GSA Schedule, and provides the highest caliber vendor- authorized instruction at our training centers, online, or onsite. ~| Order the Adobe Coldfusion Anthology now! http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:360046 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm
Re: Iif() Evaluation Weirdness
If you are going to use a variable that doesn't exist then using this method will cause you issues as cf will still try to evaluate both sides of it even if false. There are ways round it using nested evaluate and de statements, but frankly it just becomes messy and unreadable, I used to do this myself. You are better off just using cfif or switch statement instead. On Tue, Feb 3, 2015 at 19:32 PM, Mosh Teitelbaum mosh.teitelb...@evoch.com wrote: All: I've run into a problem that I could use some new eyes on... I have a function inside a CFC that accepts a query (importData) and a series of indicators (nicknameInd) that indicate if the value from the query should be used (indicator = 1) or not (indicator = 0). In the below code, the first argument in the IIF() function evaluates to false (both conditions are false) so only the third argument, De(), should be evaluated. However, for some reason, both the second and third arguments are being evaluated and I'm getting an error message that Element NICKNAME is undefined in IMPORTDATA. Because importData.nickname is not defined. cfset variables.nickname = Iif( (arguments.nicknameInd NEQ 0) AND (IsDefined(importData.nickname)), De(importData.Nickname), De()) I've replaced both the second and third arguments with debugging code that showed that they are both being evaluated, regardless of the condition in the first argument. Not sure why that is though given that they're both wrapped in DE() functions. Any suggestions? Thanks. -- Mosh Teitelbaum ~| Order the Adobe Coldfusion Anthology now! http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:360048 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm
RE: Iif() Evaluation Weirdness
Thanks Russ (and Dave). That's the route I ended up going to. Kind of annoying as I think the Iif() method produces cleaner code (and I had already written it) but it is what it is. -- Mosh Teitelbaum Russ Michaels wrote If you are going to use a variable that doesn't exist then using this method will cause you issues as cf will still try to evaluate both sides of it even if false. There are ways round it using nested evaluate and de statements, but frankly it just becomes messy and unreadable, I used to do this myself. You are better off just using cfif or switch statement instead. ~| Order the Adobe Coldfusion Anthology now! http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:360049 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm
Re: Iif() Evaluation Weirdness
cfset variables.nickname = Iif( (arguments.nicknameInd NEQ 0) AND (IsDefined(importData.nickname)), De(importData.Nickname), De()) CF is going to verify that the variable exists, because you're referring to it. That's what's happening here. Just because you're using the DE() function doesn't mean that CF isn't going to evaluate importData.nickname at runtime to verify that there's actually something called importData.nickname in case it needs to reference that value. Thanks for the reply. My misuse of terminology aside, that would mean that the following cannot be converted to using IIF(): cfif IsDefined(importData.nickname) cfset variables.nickname = importData.nickname cfelse cfset variables.nickname = /cfif I thought the whole point of the DE() function was to delay evaluation of its argument until it was actually needed (e.g., when the IIF() function's first argument evaluates to true). If not, then the only way to use a variable, that may not be defined, is by way of the full cfif treatment. Is that right? No, that's not entirely right. But the CFIF is more readable, so you might want to go with that anyway. Honestly, I find IIF to be hard to read a lot of the time, so I try to avoid it. Now that you have a corresponding CFIF, it's a bit clearer to me what you're trying to do. Here's how you'd do that, I think, in IIF: cfset variables.nickname = iif(arguments.nicknameInd neq 0 and isDefined(importData.nickname), importData.nickname, ) (note, I haven't tested this because I'm not in my office) The IIF function will automatically evaluate the last two arguments and return their values, but you'll notice that the arguments themselves are strings. Your second argument doesn't actually refer to the variable until that branch is executed. Does that make sense? Dave Watts, CTO, Fig Leaf Software 1-202-527-9569 http://www.figleaf.com/ http://training.figleaf.com/ Fig Leaf Software is a Service-Disabled Veteran-Owned Small Business (SDVOSB) on GSA Schedule, and provides the highest caliber vendor- authorized instruction at our training centers, online, or onsite. ~| Order the Adobe Coldfusion Anthology now! http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:360050 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm
Re: Iif() Evaluation Weirdness
Well if you want to try it out then try using nested evaluate(de('')) You can also achieve same using nested quotes like de('stuff') to avoid evaluation, But it just gets messy imho Iif() has its uses for simple inline evaluations, such as select lists or dynamic style classes but beyond that I think it just makes for better code not to do it inline. On Wed, Feb 4, 2015 at 0:21 AM, Mosh Teitelbaum mosh.teitelb...@evoch.com wrote: Thanks Russ (and Dave). That's the route I ended up going to. Kind of annoying as I think the Iif() method produces cleaner code (and I had already written it) but it is what it is. -- Mosh Teitelbaum Russ Michaels wrote If you are going to use a variable that doesn't exist then using this method will cause you issues as cf will still try to evaluate both sides of it even if false. There are ways round it using nested evaluate and de statements, but frankly it just becomes messy and unreadable, I used to do this myself. You are better off just using cfif or switch statement instead. ~| Order the Adobe Coldfusion Anthology now! http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:360051 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm