Skip to main content
Known Participant
June 4, 2009
Answered

Best Practices for CFC and CFFUNCTION

  • June 4, 2009
  • 2 replies
  • 1378 views

I'm curious what the best place should be for cffunctions.  Should they be in the Application.cfc file if they need to be called from various places throughout a site.

Or, is it a better idea to put the cffunctions onto one page and use cfinclude to bring the page into pages where the functions may be used?

Or is there a better way to deal with them???

    This topic has been closed for replies.
    Correct answer BKBK

    I see ... those are excellent reasons.  I'm not entirely clear on CFCs, but nothing that I am writing is large enough for me to do that with I think. I have a bunch of little code time savers like this:

    <cffunction name="getRandomString" output="no" returntype="string">

        <cfset chars = "0123456789ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" / >

        <cfset randout = "">

        <cfset stringLength = "30">

        <cfloop from="1" to="#stringLength#" index="i">

            <cfset rnum = ceiling(rand() * len(chars))>

            <cfif rnum EQ 0 >

                <cfset rnum = 1 / >

            </cfif>

            <cfset randout = randout & mid(chars, rnum, 1)>

        </cfloop>

        <cfreturn "#randout#">

    </cffunction>

    Perhaps they should all go in a CFC called something like UDF.CFC.  I believe that I can call one of several functions in a CFC.  I'm not clear on if a CFC is ONE process or can be a collection of processes.  I'm also not clear on where to put it so that it can be accessed by all pages in a site.
    I appreciate your comments, they are really helping my understanding of ColdFusion.


    I have a bunch of little code time savers

    You could always use an include, of course. You should be thinking about CFCs when some functions share themes in common, when functions become complex or when the number of functions becomes large.

    Perhaps they should all go in a CFC called something like UDF.CFC. 

    Yes, they could go into a CFC, but not necessarily all in one CFC. You should group functions together only if they express the behaviour of one particular concept. For example, you could make functions that manipulate strings, like getRandomString, part of StringManipulation.cfc.

    I believe that I can call one of several functions in a CFC.

    You can call any number of functions from a CFM page or from a function in another CFC.

    I'm not clear on if a CFC is ONE process or can be a collection of processes.

    A function call by means of an instance of a component, as follows, is one process

    <cfset obj = createobject("component", "dotted.path.to.comp")>
    <cfset theVar = obj.func()>

    However, there are ways to create two or more such processes to run at the same time.

    I'm also not clear on where to put it so that it can be accessed by all pages in a site.

    As Dan has said, you can put it within or outside the wwwroot folder. The important thing is to distribute your CFMs and CFCs into folders in such a way that your application will have a logical, easy-to-follow structure.

    2 replies

    BKBK
    Community Expert
    Community Expert
    June 4, 2009
    Should they[cffunctions] be in the Application.cfc file if they need to be called from various places throughout a site.

    Absolutely not.

    Or, is it a better idea to put the cffunctions onto one page and use cfinclude to bring the page into pages where the functions may be used?

    No.

    Or is there a better way to deal with them???

    Yes. If you wish to call functions from various places throughout your site then you will have to put them in components.

    comp.cfc

    =======

    <cfcomponent>

    <cffunction name="func">

    </cffunction>

    <cfcomponent>

    anyPage.cfm

    ==========

    <cfset obj = createobject("component", "dotted.path.to.comp")>

    <cfset theVar = obj.func()>

    Known Participant
    June 4, 2009

    Thanks guys:

    BKBK: Is there a reason I don't want to use the include file method?  It seems to work just fine.  Is there a security or performance issue that is created by this?

    Inspiring
    June 4, 2009

    Security wise, if you put your cfc's outside your web root they are a bit more secure.  You can't do that with included files.

    Performancewise, there was a thread a while back where someone reported that included files were faster than cfc's.

    Inspiring
    June 4, 2009

    It depends on your situation.

    We self host and have more than one application.  For things like stylesheets, images, javascript, and udfs, we have folders off the web root so the files are available to all applications.  The udfs, are in files with names like stringfunctions.cfm, queryfunctions.cfm, etc.  Two advantages of this are:

    1.  Less data to load if you only want one or two functions.

    2.  Less liklihood of a syntax error in a function you don't want messing you up.

    We also have our custom tags and cfc's outside the web root.  They are available to all applications.

    We also have a webservice folder inside the web root.

    In the few application.cfc files I know about, the only functions are the standard ones, onApplicationStart() etc.

    Finally, some individual cfm pages might contain functions.

    It might not be the best practice, but it works for us.