Thursday, April 12, 2012

MAD About Code! :)

On a new consulting gig, and wanting to improve the legacy code...

We can all agree this is awful!!! Right???

And that it will run slow as a nutrirat with two broken legs up hill in the rain

*nutrirat is a cajun delicacy, ask Ray Camden! :)

No, what you remember is that you used to drive that old busted joint. See, I drive... the new hotness.


<cfset descrabbrev_var="ABBREV">

<select name="states">

<OPTION VALUE=""> </OPTION>

<OPTION VALUE="AL" ><cfif descrabbrev_var EQ "ABBREV">AL<cfelse>Alabama</cfif></OPTION>

<OPTION VALUE="AK" ><cfif descrabbrev_var EQ "ABBREV">AK<cfelse>Alaska</cfif></OPTION>

<OPTION VALUE="AZ" ><cfif descrabbrev_var EQ "ABBREV">AZ<cfelse>Arizona</cfif></OPTION>

<OPTION VALUE="AR" ><cfif descrabbrev_var EQ "ABBREV">AR<cfelse>Arkansas</cfif></OPTION>

<OPTION VALUE="CA" ><cfif descrabbrev_var EQ "ABBREV">CA<cfelse>California</cfif></OPTION>

<OPTION VALUE="CO" ><cfif descrabbrev_var EQ "ABBREV">CO<cfelse>Colorado</cfif></OPTION>

<OPTION VALUE="CT" ><cfif descrabbrev_var EQ "ABBREV">CT<cfelse>Connecticut</cfif></OPTION>

<OPTION VALUE="DE" ><cfif descrabbrev_var EQ "ABBREV">DE<cfelse>Delaware</cfif></OPTION>

<OPTION VALUE="DC" ><cfif descrabbrev_var EQ "ABBREV">DC<cfelse>Dist Of Col</cfif></OPTION>

<OPTION VALUE="FL" ><cfif descrabbrev_var EQ "ABBREV">FL<cfelse>Florida</cfif></OPTION>

<OPTION VALUE="GA" ><cfif descrabbrev_var EQ "ABBREV">GA<cfelse>Georgia</cfif></OPTION>

<OPTION VALUE="HI" ><cfif descrabbrev_var EQ "ABBREV">HI<cfelse>Hawaii</cfif></OPTION>

<OPTION VALUE="ID" ><cfif descrabbrev_var EQ "ABBREV">ID<cfelse>Idaho</cfif></OPTION>

<OPTION VALUE="IL" ><cfif descrabbrev_var EQ "ABBREV">IL<cfelse>Illinois</cfif></OPTION>

<OPTION VALUE="IN" ><cfif descrabbrev_var EQ "ABBREV">IN<cfelse>Indiana</cfif></OPTION>

<OPTION VALUE="IA" ><cfif descrabbrev_var EQ "ABBREV">IA<cfelse>Iowa</cfif></OPTION>

<OPTION VALUE="KS" ><cfif descrabbrev_var EQ "ABBREV">KS<cfelse>Kansas</cfif></OPTION>

<OPTION VALUE="KY" ><cfif descrabbrev_var EQ "ABBREV">KY<cfelse>Kentucky</cfif></OPTION>

<OPTION VALUE="LA" ><cfif descrabbrev_var EQ "ABBREV">LA<cfelse>Louisiana</cfif></OPTION>

<OPTION VALUE="ME" ><cfif descrabbrev_var EQ "ABBREV">ME<cfelse>Maine</cfif></OPTION>

<OPTION VALUE="MD" ><cfif descrabbrev_var EQ "ABBREV">MD<cfelse>Maryland</cfif></OPTION>

<OPTION VALUE="MA" ><cfif descrabbrev_var EQ "ABBREV">MA<cfelse>Massachusetts</cfif></OPTION>

<OPTION VALUE="MI" ><cfif descrabbrev_var EQ "ABBREV">MI<cfelse>Michigan</cfif></OPTION>

<OPTION VALUE="MN" ><cfif descrabbrev_var EQ "ABBREV">MN<cfelse>Minnesota</cfif></OPTION>

<OPTION VALUE="MS" ><cfif descrabbrev_var EQ "ABBREV">MS<cfelse>Mississippi</cfif></OPTION>

<OPTION VALUE="MO" ><cfif descrabbrev_var EQ "ABBREV">MO<cfelse>Missouri</cfif></OPTION>

<OPTION VALUE="MT" ><cfif descrabbrev_var EQ "ABBREV">MT<cfelse>Montana</cfif></OPTION>

<OPTION VALUE="NE" ><cfif descrabbrev_var EQ "ABBREV">NE<cfelse>Nebraska</cfif></OPTION>

<OPTION VALUE="NV" ><cfif descrabbrev_var EQ "ABBREV">NV<cfelse>Nevada</cfif></OPTION>

<OPTION VALUE="NH" ><cfif descrabbrev_var EQ "ABBREV">NH<cfelse>New Hampshire</cfif></OPTION>

<OPTION VALUE="NJ" ><cfif descrabbrev_var EQ "ABBREV">NJ<cfelse>New Jersey</cfif></OPTION>

<OPTION VALUE="NM" ><cfif descrabbrev_var EQ "ABBREV">NM<cfelse>New Mexico</cfif></OPTION>

<OPTION VALUE="NY" ><cfif descrabbrev_var EQ "ABBREV">NY<cfelse>New York</cfif></OPTION>

<OPTION VALUE="NC" ><cfif descrabbrev_var EQ "ABBREV">NC<cfelse>North Carolina</cfif></OPTION>

<OPTION VALUE="ND" ><cfif descrabbrev_var EQ "ABBREV">ND<cfelse>North Dakota</cfif></OPTION>

<OPTION VALUE="OH" ><cfif descrabbrev_var EQ "ABBREV">OH<cfelse>Ohio</cfif></OPTION>

<OPTION VALUE="OK" ><cfif descrabbrev_var EQ "ABBREV">OK<cfelse>Oklahoma</cfif></OPTION>

<OPTION VALUE="OR" ><cfif descrabbrev_var EQ "ABBREV">OR<cfelse>Oregon</cfif></OPTION>

<OPTION VALUE="PA" ><cfif descrabbrev_var EQ "ABBREV">PA<cfelse>Pennsylvania</cfif></OPTION>

<OPTION VALUE="RI" ><cfif descrabbrev_var EQ "ABBREV">RI<cfelse>Rhode Island</cfif></OPTION>

<OPTION VALUE="SC" ><cfif descrabbrev_var EQ "ABBREV">SC<cfelse>South Carolina</cfif></OPTION>

<OPTION VALUE="SD" ><cfif descrabbrev_var EQ "ABBREV">SD<cfelse>South Dakota</cfif></OPTION>

<OPTION VALUE="TN" ><cfif descrabbrev_var EQ "ABBREV">TN<cfelse>Tennessee</cfif></OPTION>

<OPTION VALUE="TX" ><cfif descrabbrev_var EQ "ABBREV">TX<cfelse>Texas</cfif></OPTION>

<OPTION VALUE="UT" ><cfif descrabbrev_var EQ "ABBREV">UT<cfelse>Utah</cfif></OPTION>

<OPTION VALUE="VT" ><cfif descrabbrev_var EQ "ABBREV">VT<cfelse>Vermont</cfif></OPTION>

<OPTION VALUE="VA" ><cfif descrabbrev_var EQ "ABBREV">VA<cfelse>Virginia</cfif></OPTION>

<OPTION VALUE="WA" ><cfif descrabbrev_var EQ "ABBREV">WA<cfelse>Washington</cfif></OPTION>

<OPTION VALUE="WV" ><cfif descrabbrev_var EQ "ABBREV">WV<cfelse>West Virginia</cfif></OPTION>

<OPTION VALUE="WI" ><cfif descrabbrev_var EQ "ABBREV">WI<cfelse>Wisconsin</cfif></OPTION>

<OPTION VALUE="WY" ><cfif descrabbrev_var EQ "ABBREV">WY<cfelse>Wyoming</cfif></OPTION>

</SELECT>


So my slick new code bellow will be much faster right?

<cfscript>

 

variables.aryStates=[       

          {twoLetter='AL', fullName='Alabama'}

          ,{twoLetter='AK', fullName='Alaska'}

          ,{twoLetter='AZ', fullName='Arizona'}

          ,{twoLetter='AR', fullName='Arkansas'}

          ,{twoLetter='CA', fullName='California'}

          ,{twoLetter='CO', fullName='Colorado'}

          ,{twoLetter='CT', fullName='Connecticut'}

          ,{twoLetter='DE', fullName='Delaware'}

          ,{twoLetter='DC', fullName='Dist Of Col'}

          ,{twoLetter='FL', fullName='Florida'}

          ,{twoLetter='GA', fullName='Georgia'}

          ,{twoLetter='HI', fullName='Hawaii'}

          ,{twoLetter='ID', fullName='Idaho'}

          ,{twoLetter='IL', fullName='Illinois'}

          ,{twoLetter='IN', fullName='Indiana'}

          ,{twoLetter='IA', fullName='Iowa'}

          ,{twoLetter='KS', fullName='Kansas'}

          ,{twoLetter='KY', fullName='Kentucky'}

          ,{twoLetter='LA', fullName='Louisiana'}

          ,{twoLetter='ME', fullName='Maine'}

          ,{twoLetter='MD', fullName='Maryland'}

          ,{twoLetter='MA', fullName='Massachusetts'}

          ,{twoLetter='MI', fullName='Michigan'}

          ,{twoLetter='MN', fullName='Minnesota'}

          ,{twoLetter='MS', fullName='Mississippi'}

          ,{twoLetter='MO', fullName='Missouri'}

          ,{twoLetter='MT', fullName='Montana'}

          ,{twoLetter='NE', fullName='Nebraska'}

          ,{twoLetter='NV', fullName='Nevada'}

          ,{twoLetter='NH', fullName='New Hampshire'}

          ,{twoLetter='NJ', fullName='New Jersey'}

          ,{twoLetter='NM', fullName='New Mexico'}

          ,{twoLetter='NY', fullName='New York'}

          ,{twoLetter='NC', fullName='North Carolina'}

          ,{twoLetter='ND', fullName='North Dakota'}

          ,{twoLetter='OH', fullName='Ohio'}

          ,{twoLetter='OK', fullName='Oklahoma'}

          ,{twoLetter='OR', fullName='Oregon'}

          ,{twoLetter='PA', fullName='Pennsylvania'}

          ,{twoLetter='RI', fullName='Rhode Island'}

          ,{twoLetter='SC', fullName='South Carolina'}

          ,{twoLetter='SD', fullName='South Dakota'}

          ,{twoLetter='TN', fullName='Tennessee'}

          ,{twoLetter='TX', fullName='Texas'}

          ,{twoLetter='UT', fullName='Utah'}

          ,{twoLetter='VT', fullName='Vermont'}

          ,{twoLetter='VA', fullName='Virginia'}

          ,{twoLetter='WA', fullName='Washington'}

          ,{twoLetter='WV', fullName='West Virginia'}

          ,{twoLetter='WI', fullName='Wisconsin'}

          ,{twoLetter='WY', fullName='Wyoming'}

];

         

         

          //variables.dspType='fullName';                

          variables.dspType='twoLetter';                 

</cfscript>

 

<cfoutput>

          <select id="state" name="state">

              <cfloop array="#variables.aryStates#" index="variables.i">

                  <option value="#variables.i['twoLetter']#">#variables.i[variables.dspType]#</option>

              </cfloop>

          </select>

</cfoutput>

 


To put the hurt on respectively:
old busted joint:

new hotness:

This is running on a CF9.01 CHF2 installed.
I am just not happy!

For those of you that know there is more than one country in North America...

<cfscript>

      

       //This selects the language to be displayed

//     variables.language='enSubject';  

       variables.language='frSubject';  

      

       variables.aryStates=[

              {twoLetter='AL',     fullName='Alabama'}

              ,{twoLetter='AK',    fullName='Alaska'}  

              ,{twoLetter='AZ',    fullName='Arizona'} 

              ,{twoLetter='AR',    fullName='Arkansas'}

              ];       

             

       variables.aryEnStates=[

              {twoLetter='ND',     fullName='North Dakota'}

              ,{twoLetter='SD',    fullName='South Dakota'}  

              ]; 

             

       variables.aryFrStates=[

              {twoLetter='ND',     fullName='Dakota du Nord'}

              ,{twoLetter='SD',    fullName='Dakota du Sud'} 

              ]; 

 

      

       if (variables.language eq 'frSubject'){

              variables.aryStates.addAll( variables.aryFrStates );

              //addAll is a java function

       };

       if (variables.language eq 'enSubject'){

              variables.aryStates.addAll( variables.aryEnStates );

              //addAll is a java function

       };

                                                      

       //This selects the language to be displayed

//     variables.dspType='twoLetter';   

       variables.dspType='fullName';           

</cfscript>

 

<cfoutput>

       <select id="state" name="state">

           <cfloop array="#variables.aryStates#" index="variables.i">

               <option value="#variables.i['twoLetter']#">#variables.i[variables.dspType]#</option>

           </cfloop>

       </select>

</cfoutput>

3 comments:

Tony Nelson said...

You're worried about .016 seconds?

Nathan Strutz said...

Rich, you're asking something dynamic to be faster than something static... let me put this another way. I have an HTML page that has the word "hello" on it. Compare that to a CFM page that has #hello#. There is no way that your cfm page is going to be faster than your html. Make sense? So when you make your form field more dynamic, you should, by all means, be making it slower. That's the nature of the beast.

Now let me point out a couple things. First, the speed difference is negligible. Also, with debugging on, it is undoubtedly slower than it will be in production. There is no way any user will realize the difference between the two pages. Second, your form is now data-driven, and the data is reusable. You can tuck that aryStates (the data) away in a CFC or an include and re-use it anywhere you need. This is a very good thing. Third, your form field is now dynamic, so say one day you have to add Canadian provinces, you are already halfway there. Fourth, you have reduced complexity - what used to take 54 lines, is now 5 lines, not including a call to get the data from somewhere. This means that if you want to modify the way the select works, like style each option element or something, you don't have to change it 50 times, you just have to change it once.

This is the magic of programming, it costs CPU cycles but you've made your application better. No doubt.

Rich said...

Hi Nathan, I was figuring that eliminating 50 cfif statements would put the new code in the fast lane. Yes, we do Canadian provinces & French, I just kept this example simple. I kbow how to justify the code, and speed is not one of them. :(