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

HPWD
  • 2,232
  • 4
  • 31
  • 61
  • Can you show a screenshot of ` – James A Mohler Feb 09 '18 at 04:04
  • @JamesAMohler Sorry about that, meant to include that with original post.. – HPWD Feb 09 '18 at 04:35
  • Failing how, throws an error or just doesn't increment the counter? – SOS Feb 09 '18 at 10:03
  • @ageax - correct, it isn’t increment it the counter. Later on in the code, there is a conditional statement that checks ‘’ and then displays some options, ‘’ displays no surveys found message which is what I’m getting right now when I know there are 9 available. – HPWD Feb 09 '18 at 14:14
  • 1
    @HPWD - 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. – SOS Feb 09 '18 at 15:09
  • 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 `` or ``. Ahh, the joys of old code... – SOS Feb 09 '18 at 18:26
  • 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. – Dawesi Mar 04 '18 at 23:08

2 Answers2

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.

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
  • 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#` ? – HPWD Feb 09 '18 at 05:09
  • 4
    Based on your cfdump, ID is number. So I would suggest looking for `defaultReport` . Think of this as a struct done wrong. – James A Mohler Feb 09 '18 at 05:12
  • 4
    ... 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 `` :-/ – SOS Feb 09 '18 at 10:07
  • You should write this up as the answer to your question. It solved your problem. – James A Mohler Feb 09 '18 at 18:52
  • @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. – HPWD Feb 20 '18 at 17:33
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

Alex Baban
  • 11,312
  • 4
  • 30
  • 44