Here's something cool that you can do with ListFind() or ListFindNoCase() that could save you loads of typing. It's something I've been doing for years in ColdFusion. And who knows -- maybe everyone else has been too! But I'll just put it out there for your consumption. :o)
Have you ever written a piece of code that looks like this?
<CFIf ThisReportType EQ "OrdersPlaced"
OR ThisReportType EQ "DailyNeeds"
OR ThisReportType EQ "TravelExpenses"
OR ThisReportType EQ "SpendRecap"
OR ThisReportType EQ "FillRate"
OR ThisReportType EQ "FillRateException"
OR ThisReportType EQ "ShiftsFilled"
OR ThisReportType EQ "OrderPlacementMethod"
OR ThisReportType EQ "ResponseTime">
<!--- then do something that pertains to all these different report types --->
</CFIf>
Yuck. That's a lot of typing (to me). If you're anything like me, I typically have a bunch of lists in my code. In this case I'm almost sure to have a list of report types called something clever like "ReportTypeList". Well, lets say that you've got twenty or thirty reports in your application, and just the above mentioned subset need some special code added.
Why not use ListFind() or ListFindNoCase()? Like this:
...
<CFSet ReportSubTypeList = "OrdersPlaced,DailyNeeds,TravelExpenses,SpendRecap,FillRate,
FillRateException,ShiftsFilled,OrderPlacementMethod,ResponseTime">
<CFIf ListFindNoCase(ReportSubTypeList, ThisReportType)>
<!--- then do something that pertains to the given report type --->
</CFIf>
...
I like to use ListFindNoCase() just so I don't have to worry about case when performing an operation like this.
I have not run any tests to see how the different methods perform, but I imagine that they're not much different from each other. It just saved me a lot of repetitive typing.
Now, I stole that first snippet of code from a friend of mine, and in looking at the rest of the code I found that he had that same opening if statement repeated two or three times on the page!
Ick!
Yes. He probably did use cut and paste, but what happens when he has another report come along that needs the same special code? He's got to go add "OR ThisReportType EQ..." in perhaps several spots. I've only got to add a single word to a single list (probably somewhere at the top of my code), and I'm done! Finished! Movin' on to the next programming task for the day. My change took two seconds, and it affected the entire program, while his change probably took him three to five minutes and with all that typing (or cutting and pasting) he could have made several typos, and introduced a new bug to track down.
Also, I just find this little trick more elegant somehow.:o)
I'd also like to add that because both the ListFind() and ListFindNoCase() functions are available in the CFJS library, you can now employ this same little time-saving trick in your JavaScript too. :o)
Well, there's another neat little trick that you may want to tuck into your toolkit. ;o)
Cheers!