Using ColdFusion Functions to clean up code
By Steven Durette

Well, another co-worker hit me with another question the other day. I really got to love my co-workers; if it weren?t for them I probably wouldn?t have anything to write about. After all, most of them are quite intelligent and have a pretty good handle on ColdFusion and SQL (T-SQL to be specific).

This time it was a pretty simple one, ?Is there a ColdFusion function that will check if a date is between two other dates?? He told me that he was writing a lot of cfif statements that were getting pretty large comparing dates and he would like to clean up the code. First I checked my handy-dandy cheat sheet of CF Functions, so that I could be reasonably sure that I wouldn?t give bad information, and when I didn?t find one I asked him why he didn?t just make one. For that matter, why not create functions for any of the large repetitive combinations that he does.

First, lets start with an example of the code we are going to clean up.
<cfif (myStartDate gte ?1/1/2003?
            and myStartDate lte
?3/31/2003?
            and myEndDate gte
?10/01/2003?
            and myEndDate lte
?12/31/2003?)
      OR (myStartDate gte
?4/1/2003?
            and myStartDate lte
?6/30/2003?
            and myEndDate gte
?1/1/2004?
            and myEndDate lte
?3/31/2004?)>
  
     <strong>It is in one of the right date ranges!</strong>

<cfelse>

    <em>It isn?t in one of the right date ranges!</em>

</cfif>

Now, first I would like to say that there is nothing wrong with the code above, but as we all know, code can get messy and sometimes it needs to be cleaned up.

Now for our function.
<cfscript>
    //by creating the function inside of a cfscript, it should be able to work in CF5.
    
    function isBetween(compVal, loVal, hiVal) {
       var myRetVal = false; 
       //I prefer to set a return variable. It?s part of one of the old coding
       //practices I learned years ago, always have one entry and one exit.
       
       //Here we check to see if our range was properly created i.e. 1..5 or
       //?10/01/2003? to ?10/31/2003? 

       if(loVal LTE hiVal) {
           //If it is, compare our value with the range and set the return
           //value if it is in the range.

           if(compVal GTE loVal AND compVal LTE hiVal)
                myRetVal = true; 
           } 
           else {
                //If loVal is greater than hiVal, reverse the compare.
                //Then set the return variable if our value is in the reversed
                //range. Sometimes we have to protect ourselves from common
                //mistakes.

                if(compVal GTE hiVal AND compVal LTE loVal)
                    myRetVal = true; 
            }

        //And finally our single exit point.
        return myRetVal;
     }
</cfscript>

All of that may seem like a lot of code, but if you consider that you can reuse it whenever and wherever you want and that it will make the previous code much easier to read, it doesn?t look so large.

Now for our corrected code from above.
<cfif (isBetween(myStartDate, ?1/1/2003?, ?3/31/2003?) and isBetween(myEndDate, ?10/01/2003?, ?12/31/2003?))
  OR (isBetween(myStartDate,
?4/1/2003?, ?6/30/2003?) And isBetween(myEndDate, ?1/1/2004?, ?3/31/2004?))>
    
     <strong>It is in one of the right date ranges!</strong>

<cfelse>

    <em>It isn?t in one of the right date ranges!</em>

</cfif>

Fewer lines and easier (in my opinion, your mileage may vary) to read. While this doesn?t apply to every situation, if you find that you are doing repetitive comparisons it might just help.

Oh, one last thing about the isBetween function created above? It works with other things besides dates. Numbers, characters, and strings are all fair game. Want to see some examples, then try the following code. (Attach the function of course)

<cfoutput>

     5 between 1 and 8: #isBetween(5, 1, 8)#<br />
     5 between 1 and 4: #isBetween(5, 1, 4)#<br />
     5 between 8 and 1: #isBetween(5, 8, 1)#<br />
     a between 1 and 8: #isBetween("a", 1, 8)#<br />
     10/21/2003 between 10/01/2003 and 10/31/2003: #isBetween("10/21/2003", "10/01/2003", "10/31/2003")#<br />
     10/21/2003 between 12/01/2003 and 12/20/2003: #isBetween("10/21/2003", "12/01/2003", "12/20/2003")#<br />
     12/21/2003 between 12/01/2003 and 12/20/2003: #isBetween("12/21/2003", "12/01/2003", "12/20/2003")#<br />
     c between a and f: #isBetween("c", "a", "f")#<br />
     g between a and c: #isBetween("g", "a", "f")#<br />
     "A test of Wills" between "1 more thing" and "zoo": #isBetween("A test of Wills", "1 more thing", "zoo")#<br />
     "A test of Wills" between "A quarter" and "zoo visits": #isBetween("A test of Wills", "A quarter", "zoo visits")#<br />
     "A test of Wills" between "Time to go bowling" and "zoo visits are great": #isBetween("A test of Wills", "Time to go bowling", "zoo visits are great")#<br />

</cfoutput>

Happy coding and enjoy the function.

About This Tutorial
Author: Steven Durette
Skill Level: Beginner 
 
 
 
Platforms Tested: CF5,CFMX
Total Views: 98,401
Submission Date: November 28, 2003
Last Update Date: June 05, 2009
All Tutorials By This Autor: 4
Discuss This Tutorial
Advertisement


Website Designed and Developed by Pablo Varando.