Wish List
Tags
adobe air ajax cf community cfml coldfusion examples ext flex javascript max2007 misc programming technology ui
Recent Entries
One Bourbon, One Scotch and One Beer
Getting Closures: The Groovy Way
Adobe Photoshop Elements 7 Announced
Getting Started with Flex 3
Adobe Munich Sponsors Girl Geek Dinner
Currently Reading
Search
RSS
Subscribe
Blogroll
An Architect's View
Ben Forta
CFSilence
Coldfusion Jedi
Rey Bango
TalkingTree
Capitalizing Peoples Names
While doing a code review at work I stumbled accross a horrible piece of code that was written in a hurry, The developer did the best he could at the time with the time that he had available to him to complete the task. He never really had time to think it though so just continued to throw worse code after bad until the problem was eventually solved.
The Problem
There has to be a better way to do what he was doing and so when I got home from work today I decided to actually look at the problem in the context of the system we have.Capitalizing names shouldn't be a big deal when a user isn't involved but if you have to rely on your input from a user you can be sure that the input you get can be in any form.
Lets take the simple name "ludwig von beethoven". The proper capitalization of this name is "Ludwig von Beethoven", in Germany names that contain "von", "van", and "der" should remain lower case. While a name like "paris hilton-richie" would be capitalized as "Paris Hilton-Richie".
All relatively simle but the problem is we are talking about users here, where we have a firstName and lastName form field. The two instances above could be any combination of the table below.
| FirstName | LastName |
| ludwig von | beethoven |
| ludwig | von beethoven |
| paris | hilton - richie |
| paris | hilton- richie |
| paris | hilton -richie |
| paris hilton- | richie |
Then of course there is the possibility of having company endings in the last name like GmbH or AG which have their own capitalization rules.
So it's not really as simple as joining the first and last name field together into a string, looping over the string with a space as delimiter and upper-casing the first letter of every word.
The Solution
The first thing I did in my new function "CaptitalizeNames" was to create a list of exceptions.I then removed all the possibilities of problems with hyphens screwing things up, so " -" and " -" were replaced globally with "-" and converted my name string into an array.
I now had a semi-cleaned name I could work with. I then looped over my query and checked for the exceptions, if I found an exception I used the value in the list instead of the value in the string, this guaranteed me that I would have the propper capitalization for the word or company abbreviation without having to do any work.
Next I checked to see if the array entry contained hyphens, if it did I used another loop using the hyphen as the delimiter and upper-cased each name in the "list".
Then at the very end I updated the array with either the exclusion, the capitalized hyphenated part, or the capitalized name.
The function when completed looks like this:
<cfargument name="theName" type="string" required="true">
<cfargument name="excludeList" type="string" required="false" default="von,van,der,AG,KG,OHG,GmbH,GbR">
<!--- convert the names into an array removing any spaces that may be in a hyphenated name--->
<cfset myNameArray = listToArray(replace(replace(theName,"- ","-","ALL")," -","-","ALL")," ")>
<!--- loop over the names array --->
<cfloop from="1" to="#arrayLen(myNameArray)#" index="iNamePart">
<cfif listfindnocase(excludelist,myNameArray[iNamePart])>
<!--- if the myNameArray[iNamePart] is in the exclude list use it from the exclude list to be sure that
the capitalization is correct --->
<cfset myNameArray[iNamePart]=listgetAt(excludelist,listfindnocase(excludelist,myNameArray[iNamePart]))>
<cfelse>
<cfloop from="1" to="#listLen(myNameArray[iNamePart],"-")#" index="i">
<cfset sCurrentName=listgetat(myNameArray[iNamePart],i,"-")>
<cfset myNameArray[iNamePart]=listSetAt(myNameArray[iNamePart],i,"#ucase(left(sCurrentName,1))##mid(sCurrentName,2,len(sCurrentName)-1)#","-")>
</cfloop>
</cfif>
</cfloop>
<cfreturn arraytolist(myNameArray," ")/>
</cffunction>
I hope this is useful to someone out there. I have added the completed function as an enclosure to this post.
Happy Coding...


I guess in mailers it looks better when the mail starts with 'Dear Mr. Gary Gilbert' instead of 'Dear Mr. gary gilbert' purely esthetically of course.
But if you have to write something to handle the situation then at least do a good job of it and not write spaghetti code, that is after all what prompted a rewrite of this function.
pisses me OFF!