The Joy of CFCs

A few years ago I started a project that I knew would likely be the largest I had yet attempted. The project would also be my first on ColdFusion MX, so CFCs seemed like a good tool to keep my code organized.
As it was my first project with CFCs, I had no experience with how best to utilize them. I read about objects and design patterns. Reading, of course, doesn't substitute for experience. Still, I have been happy with the result overall.
This week, as I was changing some functionality per some new requirements, I discovered that I had written one piece of functionality three different times in three different CFCs (talk about violating DRY!).
I changed the constructor (well, the init method) of two of the CFCs to pass in a third. I was then able to call the functionality from one place. This change in code in the CFCs didn't change the methods or arguments in the CFC except the init method. The result is that I didn't have change any of the code throughout the application that relied on this functionality.
Basicaly, I kept the API the same and so I was able to change the guts of how my logic worked without needed to change any of the code that used that logic (of which I had a lot). I could do this because I had used CFCs in a well-encapsulated way and had maintainted the API of the CFCs.
The point (if there is one) is that if you are thinking about using CFCs but you are worried that you don't know enough to take full advantage, I would encourage you to go ahead and give it a try. Even if you don't use them correctly, you will probably be ahead by using them. I have been.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Awesome stuff! CFC's are awesome. I dont know if you are a subscriber to the CFC Zone mailing list: www.cfczone.org. They have a lot of really great discussions about CFCs and general architecture using CFCs. I have been slacking on my reading of it, but certainly it's worth a read.

Keep up the good work.
# Posted By Ben Nadel | 8/7/06 4:09 PM
Sound advice, Steve. Up until a year ago I was still using CF5 and I debated about learning to use CFCs when I started using CF7 on a new project. I took the plunge and I haven't looked back. They're one of those things that you don't realise you need until you actually use them! I found spending an extra few minutes crafting a good CFC can save a lot of unecessary code duplication. Using optional arguments so various parts of your application can make use of the CFC in different ways is a good way of minimising code re-use, much like what you describe. And if you need to adjust or upgrade the function you only need to edit it in one place.
# Posted By Gary Fenton | 8/7/06 4:15 PM
Ben,

Thanks. I was. I haven't subscribed again after the mail server issues, but I plan to soon. I have really enjoyed that list and I would recommend it to others as well.

Gary,

I agree completely. I have been asked about the advantages of CFCs many times and I have often had a hard time enumerating their benefits clearly. So this seemed like a perfect example of their benefits.
# Posted By | 8/7/06 5:33 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.