Key Takeaways from CFML Functions

Whenever we are discussing about Rapid Development on script base technologies then CFML seats on a top row. CFML Technologies are becoming more powerful day by days and have better functions to improve the code and performance. There are many functions that make your job very easy, here I am comparing query functions over array functions. Both functions are stunning but sometime mix and match will play a major role in performance. let’s see quick review about CFQUERY

Technology should have to reduce our work where CFML plays a key role in it. In order to write SQL queries, we use CFQUERY tag. How easy to write queries in CFML since no need to worry about open connection and close connection. Isn’t it?

There is another feature called DBTYPE QUERY.

A bad thing I notice in DBTYPE is kill the performance specially when put in a loop so I would suggest to replace DBTYPE method with QUERYFILTER function. It returns a query object, nothing change in your code just a method that you wrote. There are more useful functions, you may like to look on it.

Now, let’s think ahead to achieve best performance for the same output. As we know there are couple of ways to accomplish the tasks so here I came up with some of techniques so code can be improve and enhance quality as well as performance. You may not find something magical, but you can find scale able solution.

Why do we need to move from Query to Array?

On the era of Front-end Technologies, we often write the rest APIs and mostly return the JSON and it should be based on the standard like {KEY: VALUE}. Why I mentioned the standard format because I want to draw your attention on a method that we often used that is serializeJSON(queryname). It converts query data into COLUMNS & ROWS format but whole universe works on a standard format.  Also, there are couples of ways to convert query data into JSON format. you can check out here.

Henceforth,  why do not we start practice to write a code base array style instead of query? I hope you are feeling more like we are moving towards  Array Fuctions so instead of returning query object from CFQUERY, we return an array object. It is very easy to get data into array from CFQUERY. Use RETURNTYPE attribute. checkout below video tutorial to understand deeply about CFQUERY RETURNTYPE.

Don’t forget to checkout this. I know you are thinking it’s look buggy but it can fix by adding two more lines so I don’t think to set back. To explain, convert NULL value to empty string. The issue has been fixed and it is in RC as on date.

Once you get data into Array then there is no DBTYPE comes in a role. It replaces with ARRAYFILTER. Below are the techniques where we can achieve a good performance score.

Why am I suggesting the array style code? Here are the keys.

  • No need to write additional single line of code if you are deal with APIs and no alteration.
  • Instead dbtype, we can use ArrayFilter. That is more efficient when you need filtration in a loop.
  • Since ORDER BY cost a lot to retrieve data from SQL. When we are going to use <cfoutput group="">, we have to apply ORDER BY whether it requires or not.
    • Instead use ArrayReduce to make groups of data.
    • Even no need to ordered data in a query. Instead use ArraySort

I believe above techniques can be game changer. Nowadays, I used a lot and believe me once you start, you will realize instantly.

Note that there is no technique declare officially. I made based on my experience.

I hope you takeaways some key from this article.

(Thanks to Debby Hudson for an image.)