CF9: What is this evaluate statement evaluating?
Asked Answered
L

2

5

I'm stuck and need a fresh set of eyes on this, please.

I'm working with someone else's spaghetti code who is no longer around and having a heck of a time figuring out what they were evaluating.

<cfset surveyCount = 0>
<cfloop query="surveys">
    <cfif evaluate("defaultReport" & ID)>
        <cfset surveyCount = surveyCount + 1>
    </cfif>
</cfloop>  

In the query dump, I see 9 records which is what I am expecting but because because the evaluate is failing, the surveyCount isn't being incremented. I do not see any columns for defaultReport. In my 15 years of working with CF, I've always avoided evaluate() and now when I need to analyze it, I'm at a complete loss. Can someone offer any guidance?

Added CFDump image (some columns names have been removed for privacy and security): enter image description here

UPDATE I: This file has numerous cfinclude statements and very little code formatting. As a result, I overlooked some cfinclude statements. I found the following. I'm still looking but wanted to document this as I dig.

<cfloop query="surveys"> <cfscript> variables["defaultReport" & ID] = evaluate(thisAssociation & "Price"); </cfscript> </cfloop>

UPDATE II: Dumping the variable scope, I did confirm the variable I am looking for (finding the query I posted in UPDATE I helped, too). :)
enter image description here

Lyre answered 9/2, 2018 at 3:41 Comment(7)
Can you show a screenshot of <cfdump var="#surveys#"?Suburb
@JamesAMohler Sorry about that, meant to include that with original post..Lyre
Failing how, throws an error or just doesn't increment the counter?Daze
@ageax - correct, it isn’t increment it the counter. Later on in the code, there is a conditional statement that checks ‘<cfif surveyCount>’ and then displays some options, ‘<cfelse>’ displays no surveys found message which is what I’m getting right now when I know there are 9 available.Lyre
@Lyre - Ok, makes sense. If you haven't already, I'd start with a search on "defaultReport" like James suggested. Hopefully, that variable prefix is hard coded somewhere. If not, may have to do it the hard way, dump the scopes to see if that variable exists anywhere for the current request.Daze
Glad you finally found it! That explains why the surveyCount isn't incremented. Since the values of all the "defaultReportX" variables are all 0, this equates to false <cfif evaluate("defaultReport" & ID)> or <cfif 0 neq 0>. Ahh, the joys of old code...Daze
BTW nothing wrong with evaluate() as far as performance anymore (since cf8+), it's more of a 'style guideline' thing these days. In fact I've found some edge cases where evaluate is far quicker than using variables[variable&23] or similar... just test both formats and move on, there's little to do difference in most cases (unless you're doing 10k iterations (firstly why?).. and no definite case for either being faster either in all cases. test it, and if no different, leave it.Zoogeography
R
5

What they wanted to do is to increase surveyCount but only if this thing: evaluate("defaultReport" & ID) evaluates to true.

From your query dump picture it looks like the IDs are numbers like 144, 145, etc...

In this context, you can think at evaluate("defaultReport" & ID) as something like defaultReport144, defaultReport145, etc... (these are variables set somewhere in the code).

So the code:

<cfif evaluate("defaultReport" & ID)>
    <cfset surveyCount = surveyCount + 1>
</cfif>

becomes (for an ID of 144, the first one on your query loop)

<cfif defaultReport144>
    <cfset surveyCount = surveyCount + 1>
</cfif>

and so on... for the other IDs

So, search your code for where variables like defaultReport144, defaultReport145, etc... are set to either true or false (0 or 1).

Something like:

<cfset defaultReport144 = true />

or maybe they use some expression that evaluates to true or false, like:

<cfset defaultReport144 = [some expression] />

If you can't find, then maybe the code was changed or removed in the place where these defaultReport... variables were set.

ColdFusion evaluate() documentation:
https://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7f4e.html

Rhizotomy answered 9/2, 2018 at 5:30 Comment(6)
" are set to either true or false (0 or 1)." ... it could also be any value that evaluates to true, like any number <> 0.Daze
"any number <> 0" >> Or <>"false" or <>"No", too. ;-)Centrifugal
@Alex It would become <cfif evaluate(defaultReport144)>... which would boil down to <cfif Yes... or <cfif No....Centrifugal
@Centrifugal - Well .. if you want to be accurate ;-)Daze
@Ageax Accuracy is cool, but I was going more for Pedantic. :-)Centrifugal
@Centrifugal - A fellow pedant! Huzzah! *waits for pitchforks and torches* ;-)Daze
S
5

You need to look for a variable outside of your query. This variable has a name of default#ID# . It may be called.

variables.default#ID#
form.default#ID#
url.default#ID#
request.default#ID#
attributes.default#ID#

etc.

Basically ColdFusion is going to go through every scope until it finds something. (No this is not a good approach)

If you have to clean this up, I would recommend not using such an ambiguous approach. In short, there is no real way to know what it is evaluating.

Suburb answered 9/2, 2018 at 4:42 Comment(5)
It's a very old application. When I say not around any more, I mean literally. Why wouldn't I be looking for defaultReport#id# ?Lyre
Based on your cfdump, ID is number. So I would suggest looking for defaultReport . Think of this as a struct done wrong.Suburb
... and if you can't find a hard coded name starting with "defaultReport", might resort to dumping the various scopes (variables, url, etc..) to at least find out what scope it's in. Sure hope it's not defined dynamically, like in some old apps ie <cfset "#something##OtherID#" = "blah"> :-/Daze
You should write this up as the answer to your question. It solved your problem.Suburb
@JamesAMohler while your answer did help, Alex's answer was a bit more precise which led me to have an "ah ha" moment. I did upvote your answer though and appreciate you assistance pointing me in the right direction.Lyre
R
5

What they wanted to do is to increase surveyCount but only if this thing: evaluate("defaultReport" & ID) evaluates to true.

From your query dump picture it looks like the IDs are numbers like 144, 145, etc...

In this context, you can think at evaluate("defaultReport" & ID) as something like defaultReport144, defaultReport145, etc... (these are variables set somewhere in the code).

So the code:

<cfif evaluate("defaultReport" & ID)>
    <cfset surveyCount = surveyCount + 1>
</cfif>

becomes (for an ID of 144, the first one on your query loop)

<cfif defaultReport144>
    <cfset surveyCount = surveyCount + 1>
</cfif>

and so on... for the other IDs

So, search your code for where variables like defaultReport144, defaultReport145, etc... are set to either true or false (0 or 1).

Something like:

<cfset defaultReport144 = true />

or maybe they use some expression that evaluates to true or false, like:

<cfset defaultReport144 = [some expression] />

If you can't find, then maybe the code was changed or removed in the place where these defaultReport... variables were set.

ColdFusion evaluate() documentation:
https://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7f4e.html

Rhizotomy answered 9/2, 2018 at 5:30 Comment(6)
" are set to either true or false (0 or 1)." ... it could also be any value that evaluates to true, like any number <> 0.Daze
"any number <> 0" >> Or <>"false" or <>"No", too. ;-)Centrifugal
@Alex It would become <cfif evaluate(defaultReport144)>... which would boil down to <cfif Yes... or <cfif No....Centrifugal
@Centrifugal - Well .. if you want to be accurate ;-)Daze
@Ageax Accuracy is cool, but I was going more for Pedantic. :-)Centrifugal
@Centrifugal - A fellow pedant! Huzzah! *waits for pitchforks and torches* ;-)Daze

© 2022 - 2024 — McMap. All rights reserved.