My Secret Life as a Spaghetti Coder
home | about | contact | privacy statement
This morning, Ed Griffiths asked on the CFCDev mailing list if it was "possible to interrogate some method or metadata associated with [an object] in order to find out what variable name it has been stored within."

I first pointed out that Per Djurner (contributor to CF on Wheels) and myself were chatting over IM about something similar - but with functions. His solution, which I thought was "awesomely creative" (to quote myself on the CFCDev list), was basically to do this: Throw an exception and catch it. Find the line number you are looking for, read the line number, and figure out the variable name (or in this case, function).

Of course, we both hope to find a better solution. Anyway, back to Ed's problem. From my understanding of his question, he just wanted to know the name of a variable who was holding a reference to a CFC. Responses ranged from things like "why would you want to do that?" to "there is probably a way," to "it's not possible because it may have many references" (these aren't true quotes, but paraphrases really). Even Hal Helms chimed in (and I am a fan of his, albeit just recently).

After I posted a function which I thought solves the problem, Teddy Payne mentioned "this looks academic," as in he couldn't find a use for it. I have to agree with Teddy, in that I can't find a use for it either. But, when some one has a tough problem that I can't see immediately how to find an answer to, I like to find an answer. That compulsion gets stronger as the number of "this can't be done" replies increases. =) (I said). For Per, the problem is more than academic (and this doesn't quite solve it -- I'll investigate that tomorrow). We were discussing ways to dynamically decide what a function should do based on its name.

I started out searching through all the Java stuff to find a solution. When I didn't find one, I thought, why not just search through each scope and compare if the memory address is the same? Well, that didn't work well, but the function below did. I got the idea to insert a UUID key into the CFC and compare the variables in the scope to this from looking through Paul Kenney's cfcUnit (brilliant Paul!).

In any case, if you ever have the need, here's how you can find all the references to an object, but you need to provide the scope to search. This was done because I didn't take the time to figure out if there was a way to reference the "global" variables scope from within a CFC. Now, of course you may want to make it return a list of all the names, or to search all the scopes available, but you get the idea from this.

<cffunction name="getTheNameOfTheVariableThatContainsMe" output="true">
   <cfargument name="variablesScope" required="true">
   <cfset this.testkey = createUUID()>
   <cfloop list="#structkeylist(variablesScope)#" index="name">
       <cfif isStruct(variablesScope[name]) and structKeyExists(variablesScope[name],"testKey") and variablesScope[name].testKey eq this.testKey>
   <cfset structDelete(this,"testKey")>

Hey! Why don't you make your life easier and subscribe to the full post or short blurb RSS feed? I'm so confident you'll love my smelly pasta plate wisdom that I'm offering a no-strings-attached, lifetime money back guarantee!

Leave a comment

There are no comments for this entry yet.

Leave a comment

Leave this field empty
Your Name
Email (not displayed, more info?)


Subcribe to this comment thread
Remember my details

Picture of me

.NET (19)
AI/Machine Learning (14)
Answers To 100 Interview Questions (10)
Bioinformatics (2)
Business (1)
C and Cplusplus (6)
cfrails (22)
ColdFusion (78)
Customer Relations (15)
Databases (3)
DRY (18)
DSLs (11)
Future Tech (5)
Games (5)
Groovy/Grails (8)
Hardware (1)
IDEs (9)
Java (38)
JavaScript (4)
Linux (2)
Lisp (1)
Mac OS (4)
Management (15)
MediaServerX (1)
Miscellany (76)
OOAD (37)
Productivity (11)
Programming (168)
Programming Quotables (9)
Rails (31)
Ruby (67)
Save Your Job (58)
scriptaGulous (4)
Software Development Process (23)
TDD (41)
TDDing xorblog (6)
Tools (5)
Web Development (8)
Windows (1)
With (1)
YAGNI (10)

Agile Manifesto & Principles
Principles Of OOD
Ruby on Rails

RSS 2.0: Full Post | Short Blurb
Subscribe by email:

Delivered by FeedBurner