cfc best practices, tips, and tricks raymond camden [email protected]
TRANSCRIPT
![Page 2: CFC Best Practices, Tips, and Tricks Raymond Camden jedimaster@mindseye.com](https://reader036.vdocuments.mx/reader036/viewer/2022062805/5697bfbd1a28abf838ca1cea/html5/thumbnails/2.jpg)
Agenda
• Basic (but real!) Examples
• Best Practices (Suggestions)
• Tips and Tricks
• Resources
![Page 3: CFC Best Practices, Tips, and Tricks Raymond Camden jedimaster@mindseye.com](https://reader036.vdocuments.mx/reader036/viewer/2022062805/5697bfbd1a28abf838ca1cea/html5/thumbnails/3.jpg)
Examples
• www.cflib.orgCFCs serve as DB abstraction
• ResourceBundle CFCLocalization Utility
• SimpleCMSSimple CMS based on CFCs
![Page 4: CFC Best Practices, Tips, and Tricks Raymond Camden jedimaster@mindseye.com](https://reader036.vdocuments.mx/reader036/viewer/2022062805/5697bfbd1a28abf838ca1cea/html5/thumbnails/4.jpg)
Best Practices
• Control white space generated by CFC
<cfcomponent displayName="Example" output="false">
<cffunction name="test" returnType="string" access="public" output="false"> <cfreturn "This is a boring function."> </cffunction> </cfcomponent>
![Page 5: CFC Best Practices, Tips, and Tricks Raymond Camden jedimaster@mindseye.com](https://reader036.vdocuments.mx/reader036/viewer/2022062805/5697bfbd1a28abf838ca1cea/html5/thumbnails/5.jpg)
Best Practices
• Supply Optional Attributes
<cfcomponent displayName="Example2" output="false">
<cffunction name="test" returnType="string" access="public" output="false" hint="This function does blah using Foo's logic."> <cfargument name="argone" type="string" required="true"> <cfargument name="argtwo" type="numeric" required="false" default="0"> <cfreturn arguments.argone & " with the number " & arguments.argtwo> </cffunction> </cfcomponent>
![Page 6: CFC Best Practices, Tips, and Tricks Raymond Camden jedimaster@mindseye.com](https://reader036.vdocuments.mx/reader036/viewer/2022062805/5697bfbd1a28abf838ca1cea/html5/thumbnails/6.jpg)
Best Practices
• Use the VAR scope!
<cfcomponent displayName="Example3" output="false">
<cffunction name="test" returnType="string" access="public" output="false"> <cfargument name="argone" type="string" required="true"> <cfset var i = 0> <cfloop index="i" from=1 to=10> ..... </cfloop></cffunction> </cfcomponent>
![Page 7: CFC Best Practices, Tips, and Tricks Raymond Camden jedimaster@mindseye.com](https://reader036.vdocuments.mx/reader036/viewer/2022062805/5697bfbd1a28abf838ca1cea/html5/thumbnails/7.jpg)
Understand Data Scopes
• Variables
• This
• Arguments
• Function Local
![Page 8: CFC Best Practices, Tips, and Tricks Raymond Camden jedimaster@mindseye.com](https://reader036.vdocuments.mx/reader036/viewer/2022062805/5697bfbd1a28abf838ca1cea/html5/thumbnails/8.jpg)
Cache CFC Instances
• CFC creation can be slow.
• Use createObject/cfobject to create an instance.
• <cfinvoke> (typically) works with new instances.
![Page 9: CFC Best Practices, Tips, and Tricks Raymond Camden jedimaster@mindseye.com](https://reader036.vdocuments.mx/reader036/viewer/2022062805/5697bfbd1a28abf838ca1cea/html5/thumbnails/9.jpg)
Pseudo-Constructor
• Any code not inside <cffunction>.
• Useful to initialize variables.
• BlueDragon has <cfconstructor>
![Page 10: CFC Best Practices, Tips, and Tricks Raymond Camden jedimaster@mindseye.com](https://reader036.vdocuments.mx/reader036/viewer/2022062805/5697bfbd1a28abf838ca1cea/html5/thumbnails/10.jpg)
Dealing with Application Variables
• Application.cfm and when it's run:Flash Remoting/WS callsSame folder
• Don't rely on it!
• Application CFC
![Page 11: CFC Best Practices, Tips, and Tricks Raymond Camden jedimaster@mindseye.com](https://reader036.vdocuments.mx/reader036/viewer/2022062805/5697bfbd1a28abf838ca1cea/html5/thumbnails/11.jpg)
Inheritance/Super
• Use for Is-A, not Has-AIn other words, inheritance is used when a
CFC defines as "is a" relationship with it's parent. It should not be used for simple code re-use.
• Super allows for a child method to extend a parent method.
![Page 12: CFC Best Practices, Tips, and Tricks Raymond Camden jedimaster@mindseye.com](https://reader036.vdocuments.mx/reader036/viewer/2022062805/5697bfbd1a28abf838ca1cea/html5/thumbnails/12.jpg)
Pass by Ref/by Value
• Values passed to CFC methods follow the same rules as values passed to UDFs.
• The following are passed by reference:StructuresQueriesOther "comlpex" objects (including CFCs)
![Page 13: CFC Best Practices, Tips, and Tricks Raymond Camden jedimaster@mindseye.com](https://reader036.vdocuments.mx/reader036/viewer/2022062805/5697bfbd1a28abf838ca1cea/html5/thumbnails/13.jpg)
Resources
• www.cfczone.org
• cfcdev mailing list
• Top Ten Tips article by Rob Brooks-Bilsonhttp://www.oreillynet.com/pub/a/javascript/
2003/09/24/coldfusion_tips.html?page=1
• CFC Best Practices by Nathan Dintenfasshttp://www.dintenfass.com/cfcbestpractices/