Why you will never read my blog again...
I was cleaning up my "testingzone" folder today when I ran across some pretty darn old code. (My testingzone folder is where I put all my random tests. My thinking was that it would keep the rest of my web root clean. Of course now my testingzone folder is almost unreadable.) This application was created in 1998, back when IE was the best browser (well in my opinion Netscape had really begun to slip) and ColdFusion was still at version 4.
Oh - and its back when I thought code in all caps was really cool and actually readable.
So first check out the demo:
Check out the browser requirements. Check out the "Team Allaire". Talk about ancient history! The application works on a very simple keyword basis. I set it up so that you would create a group of keywords (dogs,dog,canine,etc) and a list of responses. When a match to a keyword was made, a random response is picked.
Back when I wrote this there was no XML parsing so I made the dubious choice of using line breaks for my data structure. Here is a snippet from the data file:
paranoia
paranoid
You're not paranoid if they really <I>are</I> out to get you.
Who is that behind you?
fear
scared
afraid
You must learn to overcome your fears.
Fear is natural, we must learn to accept our fears if we are to truly enjoy life.
Fear is a four letter word.
you
Let's talk about you, not me.
You would find my life very boring, let's talk about you instead.
Who is the doctor here?
This conversation is about you, not me.
Notice how the logic is - list of keywords, blank line, list of responses. Not what I'd consider best practice. Back in CF4 I believe they had INI file functions. I would have used that if I had thought it out better.
Now for some code. Here is what I wrote to parse in the data file:
<!--- Static Variables --->
<CFSET ELIZADAT = "eliza.dat">
<CFSET OVERRIDE = TRUE>
<CFIF NOT IsDefined("Application.KEYWORDS") OR OVERRIDE>
<CFFILE ACTION="Read" FILE="#ExpandPath(ELIZADAT)#" VARIABLE="BUFFER">
<CFSET Application.KEYWORDS = ArrayNew(1)>
<CFSET Application.RESPONSES = ArrayNew(1)>
<CFSET Application.NULLRESPONSES = "">
<CFSET Application.NOTHINGRESPONSES = "">
<CFSET NL = Chr(10)><CFSET NL2 = Chr(13)>
<CFSET NEWLINE = NL2 & NL>
<CFSET DOUBLE_NEWLINE = NEWLINE & NEWLINE>
<CFSET BUFFER = Replace(BUFFER,DOUBLE_NEWLINE,"@","ALL")>
<CFSET ONKEY = TRUE>
<CFSET LASTKEY = "">
<CFSET CURRX = 0>
<CFLOOP INDEX=CURRLINE LIST="#BUFFER#" DELIMITERS="@">
<CFSET CURRLINE = Replace(CURRLINE,",","&COMMA;","ALL")>
<CFSET CURRLINE = Replace(CURRLINE,NL,",","ALL")>
<CFIF ONKEY>
<CFSET LEN = IncrementValue(ArrayLen(Application.KEYWORDS))>
<CFSET Application.KEYWORDS[LEN] = CURRLINE>
<CFSET CURRX = CURRX + 1>
<CFIF CURRLINE IS "NULL"><CFSET Application.NULLX = CURRX></CFIF>
<CFIF CURRLINE IS "NOTHING"><CFSET Application.NOTHINGX = CURRX></CFIF>
<CFELSE>
<CFSET LEN = IncrementValue(ArrayLen(Application.RESPONSES))>
<CFSET Application.RESPONSES[LEN] = CURRLINE>
</CFIF>
<CFSET ONKEY = NOT ONKEY>
</CFLOOP>
</CFIF>
Wow - I think my eyes are actually bleeding. I'm not sure why I left override on (it's off now). I just noticed - I used caps for tags, normal case for functions. I seriously must have been on crack back then.
Comments
But then again I only started writing CF 15 months ago, and I'm still scared to post code.
You know...it really was readable. Really. Until we started writing everything lowercase to keep up with the standards. Now you're right - it makes the eyes bleed just a little. :-)
<CFSET DOUBLE_NEWLINE = NEWLINE & NEWLINE>
...provides a savings of 3 characters per use. However, you only used it one time, so if you actually used more characters by using the CFSET. But creating the double_newline for the replace method was much easier to read overall, so maybe that's ok. It just struck me as funny that there is no apparent way to shorten "DOUBLE_NEWLINE" without losing readability.
Though, I have to admit, i read some of the stuff I wrote....last NIGHT and I ask myself what in the name of all that is holy was I thinking about, so if you're able to look back at the stuff from 5 YEARS ago and laugh, you're in good shape...
BTW: I LIKE TO WRITE CODE IN CAPS. that is all.
Most of my CF5 apps are still in production, and when it comes time to do tweaks or feature additions, it is painful. I swear I spend 50% of the time adding the new features, and 50% just trying to clean up code a bit, so that I'm not COMPLETELY ashamed of it. :-)


Back in CF 4 I was impressed you didn't surround your variables with #'s