Copy link to clipboard
Copied
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???
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
...Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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()>
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Not particularly. Includes work just fine. It is just that calling a function via a component is the better solution in this case. I can immediately think of three advantages:
1) Components enable you to separate business code from presentation code;
2) Maintenance costs account for more than half the total costs that software will incur over its lifetime. Components, more than includes, make code easier to maintain. For example, a component may have several related functions, and may also define the variables that the functions share. To achieve the same functionality with includes would generally involve repetition, thereby increasing the maintenance burden.
3) Components raise the level of abstraction of your code. As a result, you can use them to model things or concepts. The functions then represent the behaviour of the modelled objects.
Copy link to clipboard
Copied
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>
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
While we're on the subject, here's an idea
<cffunction name="getRandomString" output="no" returntype="string">
<cfargument name="chars" required="yes" type="string">
<cfargument name="stringLength" required="yes" type="string">
<cfset var chrs = arguments.chars>
<cfset var length = arguments.stringLength>
<cfset var randout = "">
<cfset var rnum = "">
<cfloop from="1" to="#length#" index="i">
<cfset rnum = ceiling(rand() * len(chrs))>
<cfif rnum EQ 0 >
<cfset rnum = 1 >
</cfif>
<cfset randout = randout & mid(chrs, rnum, 1)>
</cfloop>
<cfreturn randout>
</cffunction>
Copy link to clipboard
Copied
Excellent, thanks so much for your posts.
I know that I have opportunities in my work for implementing CFCs. I'm going to have to find a good tutorial and start using them more. Thanks again
Ciao!