Yesterday at my presentation, someone asked about CFC and recursion. I had mentioned that normally I shy away from it, and I had been told specifically by MACR engineers that it was a bad idea inside UDFs, especially if the recursion got more than 50 levels deep. I made it clear though that I wasn't 100% sure this applied to CFCs.
Todd Rafferty had to deal with exactly this issue and has two blog entries as well as a code sample. You can check out these entries here:
http://blog.web-rat.com/archives/000144.cfm#000144 http://blog.web-rat.com/archives/000145.cfm#000145
Archived Comments
Ray,
Back when CF 5 was out, I had a fairly lengthy discussion with some of the Macromedia engineers regarding recursionin UDFs. At the time, they told me that the max CF 5 could handle was ~800 levels before the stack overflowed. Depending on the type of operation you are going to do, this may or may not be a problem.
I''ve used recursion in UDF ever since they became available. I''ve found it to work just fine (I don''t think I''ve every gone over 100 levels of recursion, though). It helps to keep your variables local with the var command if at all possible.
Recursion is often the easiest and most elegant way to express a problem solution. I''ve worked in languages that have no assignment / side-effects and they rely on recursion as a substitute for iterating over lists. As with all implementation techniques, if you don''t know what you''re doing, you can end up with a runaway program!
I had a similar problem that I solved with a GREAT SQL query. In ORALCE 8i<, not sure about SQL Server.
SELCT parentID, childID, level
FROM table
CONNECT BY parentID = PRIOR childID
START WITH parentID = 0
this SQL block retuns a tree structure with a built-in column called "level" for the level on the tree (starts with 1).
This eliminated me doing recursion in CF and puts all the work in the DB where it belongs.
See it in work at www.dresser.com (menu applet) on the left hand navigation.
Thanks