Jedi Name Generator Code Review
Posted in ColdFusion |
Jun 22, 2007 11:16:PM |
In my search for understanding of CFC's I created the Star Wars Jedi Name Generator in ColdFusion. Since I have only used CFC's to query databases and have never used them to parse any variables I thought long and hard for a cool yet easy CFC to parse text from a form. In my searching I ran across several Jedi Name Generators in many other languages, but none in Coldfusion. So thank goodness I'm the first, plus I'm posted the code for all to see and use.
So first lets look at the CFC doing all the parsing work.
Code
<cfargument name="firstName" type="string" required="true"> <cfset jediFirstNameTemp = lastNameTemp & "" & firstNameTemp><cfcomponent displayName="generateName" output="false" hint="Star Wars name generator">
<cffunction name="renderName" access="public" returnType="string" output="false" hint="Handles rendering of Jedi name.">
<cfargument name="lastName" type="string" required="true">
<cfargument name="actressLastName" type="string" required="true">
<cfargument name="cityOfBirth" type="string" required="true">
<cfset var jediName = "">
<cfset var firstNameTemp = "">
<cfset var lastNameTemp = "">
<cfset var actressLastNameTemp = "">
<cfset var cityOfBirthTemp = "">
<cfset var jediFirstNameTemp = "">
<cfset var jediLastNameTemp = "">
<cfset firstNameTemp = Left(arguments.firstName, 2)>
<cfset lastNameTemp = Left(arguments.lastName, 3)>
<cfset actressLastNameTemp = Left(arguments.actressLastName, 2)>
<cfset cityOfBirthTemp = Left(arguments.cityOfBirth, 3)>
<cfset jediLastNameTemp = actressLastNameTemp & "" & cityOfBirthTemp>
<cfset jediFirstNameTemp = ucase(left(trim(jediFirstNameTemp),1)) & lcase(mid(trim(jediFirstNameTemp), 2, (len(trim(jediFirstNameTemp)) - 1)))>
<cfset jediLastNameTemp = ucase(left(trim(jediLastNameTemp),1)) & lcase(mid(trim(jediLastNameTemp), 2, (len(trim(jediLastNameTemp)) - 1)))>
<cfset jediName = jediFirstNameTemp & " " & jediLastNameTemp>
<cfreturn jediName>
</cffunction>
</cfcomponent>
I think the code is pretty easy to follow, basically I'm taking 4 variables from the form and using them to create a Jedi name. I also made sure if some one entered all lower case it would capitalize the first character for the first and last Jedi name.
The form which you can download below does all the error checking, once I learn how to use the Application.cfc I want to learn to do error checking through the CFC's. this was a fun project and I learned a few thing's, post you comment let me know what you think.
Print