Title: CFC Best Practices, Tips, and Tricks
1CFC Best Practices, Tips, and Tricks
- Raymond Camden
- jedimaster_at_mindseye.com
2Agenda
- Basic (but real!) Examples
- Best Practices (Suggestions)
- Tips and Tricks
- Resources
3Examples
- www.cflib.org
- CFCs serve as DB abstraction
- ResourceBundle CFC
- Localization Utility
- SimpleCMS
- Simple CMS based on CFCs
4Best Practices
- Control white space generated by CFC
ltcfcomponent displayName"Example"
output"false"gt ltcffunction name"test"
returnType"string" access"public"
output"false"gt ltcfreturn "This is a boring
function."gt lt/cffunctiongt
lt/cfcomponentgt
5Best Practices
- Supply Optional Attributes
ltcfcomponent displayName"Example2"
output"false"gt ltcffunction name"test"
returnType"string" access"public"
output"false" hint"This function
does blah using Foo's logic."gt ltcfargument
name"argone" type"string" required"true"gt
ltcfargument name"argtwo" type"numeric"
required"false" default"0"gt ltcfreturn
arguments.argone " with the number "
arguments.argtwogt lt/cffunctiongt
lt/cfcomponentgt
6Best Practices
ltcfcomponent displayName"Example3"
output"false"gt ltcffunction name"test"
returnType"string" access"public"
output"false"gt ltcfargument name"argone"
type"string" required"true"gt ltcfset var i
0gt ltcfloop index"i" from1 to10gt
..... lt/cfloopgt lt/cffunctiongt
lt/cfcomponentgt
7Understand Data Scopes
- Variables
- This
- Arguments
- Function Local
8Cache CFC Instances
- CFC creation can be slow.
- Use createObject/cfobject to create an instance.
- ltcfinvokegt (typically) works with new instances.
9Pseudo-Constructor
- Any code not inside ltcffunctiongt.
- Useful to initialize variables.
- BlueDragon has ltcfconstructorgt
10Dealing with Application Variables
- Application.cfm and when it's run
- Flash Remoting/WS calls
- Same folder
- Don't rely on it!
- Application CFC
11Inheritance/Super
- Use for Is-A, not Has-A
- In 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.
12Pass by Ref/by Value
- Values passed to CFC methods follow the same
rules as values passed to UDFs. - The following are passed by reference
- Structures
- Queries
- Other "comlpex" objects (including CFCs)
13Resources
- www.cfczone.org
- cfcdev mailing list
- Top Ten Tips article by Rob Brooks-Bilson
- http//www.oreillynet.com/pub/a/javascript/2003/09
/24/coldfusion_tips.html?page1 - CFC Best Practices by Nathan Dintenfass
- http//www.dintenfass.com/cfcbestpractices/