4
\$\begingroup\$

I created a few forms for faculty members to nominate colleagues for awards. Many of the nominations require documentation to be submitted in order to validate the nomination. I created one form then modified it to fit the needs of many different award forms. I created it rather hastily and would like input as to how I may trim the fat and make the code more efficient.

One of the forms can be found here. Here's the code relevant to file uploading (starting at line 376 of the paste):

<!---Set file upload destination path to nominationUploads/firstname_lastname--->
<cfset destination = expandPath("./nominationUploads/#fname#_#lname#/")>

<!---If the destination directory does not exist, create it.  This will be unique for each nominee.--->
<cfif not directoryExists(destination)>
    <cfdirectory action="create" directory="#destination#">
</cfif>

<!---Upload  document to the destination.  Accept only MSWord, PDF, RTF and plain text files.--->
<cffile action="upload"
        filefield="nominationLetter"
        accept="application/msword, application/pdf, application/rtf, text/plain,  application/vnd.ms-word.document.12, application/vnd.openxmlformats-officedocument.wordprocessingml.document"
        destination="#destination#"
        nameconflict="makeunique">

<!--- Create variable to reference the original document name and extension uploaded from client.--->
<cfset clientNominationLetter = #file.ClientFile#>
<!---Create variable to reference renamed document.  Retain orignal file extension.--->
<cfset renameNomination = "nominationLetter"&"."&#cffile.clientFileExt#>

<!---Rename uploaded document using variable.  Save renamed document to original destination.--->
<cffile action="rename"
        source="#destination##File.ServerFile#"
        destination="#destination##Trim(renameNomination)#">


<!---Upload document to the destination.  Accept only MSWord, PDF, RTF and plain text files.--->
<cffile action="upload"
        filefield="curriculumVita"
        accept="application/msword, application/pdf, application/rtf, text/plain,  application/vnd.ms-word.document.12, application/vnd.openxmlformats-officedocument.wordprocessingml.document"
        destination="#destination#"
        nameconflict="makeunique">

<!--- Create variable to reference the original document name and extension uploaded from client.--->
<cfset clientCurriculumVita = #file.ClientFile#>
<!---Create variable to reference renamed document.  Retain orignal file extension.--->
<cfset renameCurriculumVita = "curriculumVita"&"."&#cffile.clientFileExt#>

<!---Rename uploaded document using variable.  Save renamed document to original destination.--->
<cffile action="rename"
        source="#destination##File.ServerFile#"
        destination="#destination##Trim(renameCurriculumVita)#">

<!---Upload  document to the destination.  Accept only MSWord, PDF, RTF and plain text files.--->
<cffile action="upload"
        filefield="recommendation"
        accept="application/msword, application/pdf, application/rtf, text/plain,  application/vnd.ms-word.document.12, application/vnd.openxmlformats-officedocument.wordprocessingml.document"
        destination="#destination#"
        nameconflict="makeunique">

<!--- Create variable to reference the original document name and extension uploaded from client.--->
<cfset clientRecommendation = #file.ClientFile#>
<!---Create variable to reference renamed document.  Retain orignal file extension.--->
<cfset renameRecommendation = "recommendation"&"."&#cffile.clientFileExt#>

<!---Rename uploaded document using variable.  Save renamed document to original destination.--->
<cffile action="rename"
        source="#destination##File.ServerFile#"
        destination="#destination##Trim(renameRecommendation)#">


    <cfif Len(form.recommendation2)>
        <!---Upload  document to the destination.  Accept only MSWord, PDF, RTF and plain text files.--->
        <cffile action="upload"
                filefield="recommendation2"
                accept="application/msword, application/pdf, application/rtf, text/plain,  application/vnd.ms-word.document.12, application/vnd.openxmlformats-officedocument.wordprocessingml.document"
                destination="#destination#"
                nameconflict="makeunique">

        <!--- Create variable to reference the original document name and extension uploaded from client.--->
        <cfset clientRecommendation2 = #file.ClientFile#>
        <!---Create variable to reference renamed document.  Retain orignal file extension.--->
        <cfset renameRecommendation2 = "recommendation2"&"."&#cffile.clientFileExt#>

        <!---Rename uploaded document using variable.  Save renamed document to original destination.--->
        <cffile action="rename"
                source="#destination##File.ServerFile#"
                destination="#destination##Trim(renameRecommendation2)#">
    </cfif>

    <cfif Len(form.recommendation3)>
        <!---Upload  document to the destination.  Accept only MSWord, PDF, RTF and plain text files.--->
        <cffile action="upload"
                filefield="recommendation3"
                accept="application/msword, application/pdf, application/rtf, text/plain,  application/vnd.ms-word.document.12, application/vnd.openxmlformats-officedocument.wordprocessingml.document"
                destination="#destination#"
                nameconflict="makeunique">

        <!--- Create variable to reference the original document name and extension uploaded from client.--->
        <cfset clientRecommendation3 = #file.ClientFile#>
        <!---Create variable to reference renamed document.  Retain orignal file extension.--->
        <cfset renameRecommendation3 = "recommendation3"&"."&#cffile.clientFileExt#>

        <!---Rename uploaded document using variable.  Save renamed document to original destination.--->
        <cffile action="rename"
                source="#destination##File.ServerFile#"
                destination="#destination##Trim(renameRecommendation3)#">
    </cfif>

    <cfif Len(form.recommendation4)>
        <!---Upload  document to the destination.  Accept only MSWord, PDF, RTF and plain text files.--->
        <cffile action="upload"
                filefield="recommendation4"
                accept="application/msword, application/pdf, application/rtf, text/plain,  application/vnd.ms-word.document.12, application/vnd.openxmlformats-officedocument.wordprocessingml.document"
                destination="#destination#"
                nameconflict="makeunique">

        <!--- Create variable to reference the original document name and extension uploaded from client.--->
        <cfset clientRecommendation4 = #file.ClientFile#>
        <!---Create variable to reference renamed document.  Retain orignal file extension.--->
        <cfset renameRecommendation4 = "recommendation4"&"."&#cffile.clientFileExt#>

        <!---Rename uploaded document using variable.  Save renamed document to original destination.--->
        <cffile action="rename"
                source="#destination##File.ServerFile#"
                destination="#destination##Trim(renameRecommendation4)#">
    </cfif>

    <cfif Len(form.recommendation5)>
        <!---Upload  document to the destination.  Accept only MSWord, PDF, RTF and plain text files.--->
        <cffile action="upload"
                filefield="recommendation5"
                accept="application/msword, application/pdf, application/rtf, text/plain,  application/vnd.ms-word.document.12, application/vnd.openxmlformats-officedocument.wordprocessingml.document"
                destination="#destination#"
                nameconflict="makeunique">

        <!--- Create variable to reference the original document name and extension uploaded from client.--->
        <cfset clientRecommendation5 = #file.ClientFile#>
        <!---Create variable to reference renamed document.  Retain orignal file extension.--->
        <cfset renameRecommendation5 = "recommendation5"&"."&#cffile.clientFileExt#>

        <!---Rename uploaded document using variable.  Save renamed document to original destination.--->
        <cffile action="rename"
                source="#destination##File.ServerFile#"
                destination="#destination##Trim(renameRecommendation5)#">
    </cfif>

    <cfif Len(form.recommendation6)>
        <!---Upload  document to the destination.  Accept only MSWord, PDF, RTF and plain text files.--->
        <cffile action="upload"
                filefield="recommendation6"
                accept="application/msword, application/pdf, application/rtf, text/plain,  application/vnd.ms-word.document.12, application/vnd.openxmlformats-officedocument.wordprocessingml.document"
                destination="#destination#"
                nameconflict="makeunique">

        <!--- Create variable to reference the original document name and extension uploaded from client.--->
        <cfset clientRecommendation6 = #file.ClientFile#>
        <!---Create variable to reference renamed document.  Retain orignal file extension.--->
        <cfset renameRecommendation6 = "recommendation6"&"."&#cffile.clientFileExt#>

        <!---Rename uploaded document using variable.  Save renamed document to original destination.--->
        <cffile action="rename"
                source="#destination##File.ServerFile#"
                destination="#destination##Trim(renameRecommendation6)#">
    </cfif>

</cfif>
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

Considering that you're doing this:

    <!---Upload  document to the destination.  Accept only MSWord, PDF, RTF and plain text files.--->
    <cffile action="upload"
            filefield="recommendation4"
            accept="application/msword, application/pdf, application/rtf, text/plain,  application/vnd.ms-word.document.12, application/vnd.openxmlformats-officedocument.wordprocessingml.document"
            destination="#destination#"
            nameconflict="makeunique">

    <!--- Create variable to reference the original document name and extension uploaded from client.--->
    <cfset clientRecommendation4 = #file.ClientFile#>
    <!---Create variable to reference renamed document.  Retain orignal file extension.--->
    <cfset renameRecommendation4 = "recommendation4"&"."&#cffile.clientFileExt#>

    <!---Rename uploaded document using variable.  Save renamed document to original destination.--->
    <cffile action="rename"
            source="#destination##File.ServerFile#"
            destination="#destination##Trim(renameRecommendation4)#">

over and over and over again. I'd create a custom function to handle these steps for you. Any place you have something that needs to be dynamic, create an Argument for that value. Then you could replace the above code with something like this:

uploadDocument(destination = field = "recommendation1");
uploadDocument(field = "recommendation2");
uploadDocument(field = "recommendation3");
uploadDocument(field = "recommendation4");

Then if you ever need to change how your file uploads are handled, you can just change the one function to make that happen.

Good rule of thumb: any time you copy and paste a block of code, and then replace one or two strings, make it a function.

\$\endgroup\$
2
  • \$\begingroup\$ Excuse my ignorance, but would your suggestion by considered a user defined function? I have never accomplished this before so I am trying to figure out where to start. \$\endgroup\$
    – aparker81
    Jun 9, 2011 at 19:03
  • \$\begingroup\$ The official documentation on <cffunction> is confusing. This page gives a simplified intro: communitymx.com/content/article.cfm?cid=AD0AD43C1BF789E6 \$\endgroup\$
    – krubo
    Jun 11, 2011 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.