6

I'm in the process of upgrading a large legacy ColdFusion application that heavily utilizes Application.cfm template files instead of the newer Application.cfc files.

It seems that Application.cfc offer a cleaner more efficient solution to everything that a Application.cfm file can do.

  • An Application.cfm runs every line sequentially for every request, so it would recreate application variables on every subsequent new page query. (Can cause performance hit if many application variables loaded) The Application.cfc allows for certain truely global variables to avoid getting recreated with the onApplicationStart() and onRequestStart() method

Has anyone come across any use cases/examples(apart from the obvious time it takes to port) where Application.cfm pages are preferable to Application.cfc

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Hedge7707
  • 557
  • 1
  • 5
  • 17

1 Answers1

8

IMO, this is not "too broad" a topic. This isn't an opinion, I'd classify it as a Best Practice.

There are vast number of reasons to use the cfc over the cfm. I've been in this exact situation.

Here's a list of the common functions available in Application.cfc (I'm sure you're aware):

  • onApplicationStart()
  • onSessionStart()
  • onRequestStart()
  • onRequest()
  • onRequestEnd()
  • onSessionEnd()
  • onApplicationEnd()
  • onError()

Without getting into the details of each, having the ability to sort your code into contextual buckets like these will allow you to better manage your various variable scopes. Without these contextual triggers, you're just replying on the procedural aspects of the Application.cfm.

While both are run on every page request, only certain functions in the cfc are run. The cfm, you have code running all the time, checking conditions for when it should or should not be run.

Sticking with the cfm is certainly less risky, but if you're upgrading it, you should expect that you'll break things along the way. Adopting best practices should be part of this process.

Kevin B
  • 94,570
  • 16
  • 163
  • 180
Adrian J. Moreno
  • 14,350
  • 1
  • 37
  • 44
  • 1
    I agree with everything on this list, but I feel like adding one more. Because it is a cfc, it can use extends. There are rich frameworks like FW/1 and Taffy which tap into that. Also I think if you want to use ORM, you have to use application.cfc. – James A Mohler Nov 23 '17 at 00:38
  • Thanks for the responses! So I definitely see the benefit of the .CFC over the .CFM, but are there any good reasons someone would ever use a CFM over the CFC in modern application that applies best practices? – Hedge7707 Nov 23 '17 at 15:15
  • 3
    IF I had to take care of 15 year old computer that was running CF 5, AND it was making the company millions per day AND I was told to never touch AND I was allowed to complain about the setup every day AND my kid had cancer AND my wife was having her arms and legs amputated, and the cat wanted tuna, THEN I would leave it alone. The only reason to use `application.cfm` is you just don't want to bother with it. – James A Mohler Nov 24 '17 at 07:17