UDF best practices
Anyone know what's the best way to set a UDF as a global function for use by all tags (custom, includes, etc.)... or does one in fact have to include the function code locally in each file that's being called during a page request? Thanks, Orlando ~~ Structure your ColdFusion code with Fusebox. Get the official book at http://www.fusionauthority.com/bkinfo.cfm FAQ: http://www.thenetprofits.co.uk/coldfusion/faq Archives: http://www.mail-archive.com/cf-talk@houseoffusion.com/ Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists
Re: UDF best practices
1. In the admin set the server variables to auto-lock 2. run the template containing the UDFs before the application runs or on machine startup. 3. Set a CFLOCK with a scope of server 4. Set each UDF to a variable in the scope server 5. Call the UDF on any page without using a CFINCLUDE of it This is slightly slower than having the UDFs in a CFINCLUDE that's included into the page. It's a better practice to do the following: 1. Create a small library of UDF's in a template (i.e. all data validation in one file and all text manipulation in another). 2. Save these UDF library templates in the CFIDE/UDF directory. 3. Use a in the template you want to use UDFs in. At 08:59 AM 8/8/01, you wrote: >Anyone know what's the best way to set a UDF as a global function for use by >all tags (custom, includes, etc.)... or does one in fact have to include the >function code locally in each file that's being called during a page >request? > >Thanks, >Orlando > > ~~ Structure your ColdFusion code with Fusebox. Get the official book at http://www.fusionauthority.com/bkinfo.cfm FAQ: http://www.thenetprofits.co.uk/coldfusion/faq Archives: http://www.mail-archive.com/cf-talk@houseoffusion.com/ Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists
RE: UDF best practices
Couldn't you just put them in the application.cfm for your application?? -Original Message- From: Michael Dinowitz [mailto:[EMAIL PROTECTED]] Sent: 08 August 2001 15:11 To: CF-Talk Subject: Re: UDF best practices 1. In the admin set the server variables to auto-lock 2. run the template containing the UDFs before the application runs or on machine startup. 3. Set a CFLOCK with a scope of server 4. Set each UDF to a variable in the scope server 5. Call the UDF on any page without using a CFINCLUDE of it This is slightly slower than having the UDFs in a CFINCLUDE that's included into the page. It's a better practice to do the following: 1. Create a small library of UDF's in a template (i.e. all data validation in one file and all text manipulation in another). 2. Save these UDF library templates in the CFIDE/UDF directory. 3. Use a in the template you want to use UDFs in. At 08:59 AM 8/8/01, you wrote: >Anyone know what's the best way to set a UDF as a global function for use by >all tags (custom, includes, etc.)... or does one in fact have to include the >function code locally in each file that's being called during a page >request? > >Thanks, >Orlando > > ~~ Structure your ColdFusion code with Fusebox. Get the official book at http://www.fusionauthority.com/bkinfo.cfm FAQ: http://www.thenetprofits.co.uk/coldfusion/faq Archives: http://www.mail-archive.com/cf-talk@houseoffusion.com/ Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists
RE: UDF best practices
You could but what about re-usability outside the application. This is the reason for the CFINCLUDE of the UDF. There's nothing stopping you from just putting a UDF in the application.cfm or anywhere on a template. Only limitation is that a UDF used as part of a CFINCLUDE must be defined before it's to be used where a page based one can be anywhere on the page (even after the UDF call). At 12:59 PM 8/8/01, you wrote: >Couldn't you just put them in the application.cfm for your application?? > >-Original Message- >From: Michael Dinowitz [mailto:[EMAIL PROTECTED]] >Sent: 08 August 2001 15:11 >To: CF-Talk >Subject: Re: UDF best practices > > >1. In the admin set the server variables to auto-lock >2. run the template containing the UDFs before the application runs or on >machine startup. >3. Set a CFLOCK with a scope of server >4. Set each UDF to a variable in the scope server > >5. Call the UDF on any page without using a CFINCLUDE of it > > >This is slightly slower than having the UDFs in a CFINCLUDE that's included >into the page. It's a better practice to do the following: > >1. Create a small library of UDF's in a template (i.e. all data validation >in one file and all text manipulation in another). >2. Save these UDF library templates in the CFIDE/UDF directory. >3. Use a in the template you >want to use UDFs in. > > >At 08:59 AM 8/8/01, you wrote: > > >Anyone know what's the best way to set a UDF as a global function for use >by > >all tags (custom, includes, etc.)... or does one in fact have to include >the > >function code locally in each file that's being called during a page > >request? > > > >Thanks, > >Orlando > > > > > ~~ Structure your ColdFusion code with Fusebox. Get the official book at http://www.fusionauthority.com/bkinfo.cfm FAQ: http://www.thenetprofits.co.uk/coldfusion/faq Archives: http://www.mail-archive.com/cf-talk@houseoffusion.com/ Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists
RE: UDF best practices
If I have a "page" request that calls several templates during the request... For Example: + application.cfm + myUDF.cfm + index.cfm + cfinclude template1.cfm + cfmodule template2.cfm Should I be able to include myUDF.cfm in the application.cfm file and it will be available to index.cfm, template1.cfm, and template2.cfm as myUDF()? Currently I'm getting "Error resolving parameter myUDF" with this setup when calling it from template2.cfm... If I set server.myUDF = myUDF() in application.cfm I get an "The number of arguments passed to a user-defined function cannot be less than the number of parameters in its definition. This call to myUDF passes 0 arguments, but 2 parameters are defined.". So when I pass in two dummy variables server.myUDF = myUDF('x','x') I get an "The symbol you have provided (server.myUDF) is not the name of a function." in the calling page... Would I need to make the arguments non-required to make this work properly... In this particular case, I'm running this in a hosted environment, so I don't have access to put them in the /cfide/udf/ folder. Any insight would be appreciated... Orlando -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] Sent: Wednesday, August 08, 2001 11:40 AM To: CF-Talk Subject: RE: UDF best practices You could but what about re-usability outside the application. This is the reason for the CFINCLUDE of the UDF. There's nothing stopping you from just putting a UDF in the application.cfm or anywhere on a template. Only limitation is that a UDF used as part of a CFINCLUDE must be defined before it's to be used where a page based one can be anywhere on the page (even after the UDF call). At 12:59 PM 8/8/01, you wrote: >Couldn't you just put them in the application.cfm for your application?? > >-Original Message- >From: Michael Dinowitz [mailto:[EMAIL PROTECTED]] >Sent: 08 August 2001 15:11 >To: CF-Talk >Subject: Re: UDF best practices > > >1. In the admin set the server variables to auto-lock >2. run the template containing the UDFs before the application runs or on >machine startup. >3. Set a CFLOCK with a scope of server >4. Set each UDF to a variable in the scope server > >5. Call the UDF on any page without using a CFINCLUDE of it > > >This is slightly slower than having the UDFs in a CFINCLUDE that's included >into the page. It's a better practice to do the following: > >1. Create a small library of UDF's in a template (i.e. all data validation >in one file and all text manipulation in another). >2. Save these UDF library templates in the CFIDE/UDF directory. >3. Use a in the template you >want to use UDFs in. > > >At 08:59 AM 8/8/01, you wrote: > > >Anyone know what's the best way to set a UDF as a global function for use >by > >all tags (custom, includes, etc.)... or does one in fact have to include >the > >function code locally in each file that's being called during a page > >request? > > > >Thanks, > >Orlando > > > > > ~~ Structure your ColdFusion code with Fusebox. Get the official book at http://www.fusionauthority.com/bkinfo.cfm FAQ: http://www.thenetprofits.co.uk/coldfusion/faq Archives: http://www.mail-archive.com/cf-talk@houseoffusion.com/ Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists
Re: UDF best practices
I am getting pretty good results with saving my udf's in the session scope. see the tutorial at http://www.kcfusion.org/Presentations/cflp.zip Bryan - Original Message - From: "Correa, Orlando (ITSC)" <[EMAIL PROTECTED]> To: "CF-Talk" <[EMAIL PROTECTED]> Sent: Wednesday, August 08, 2001 9:02 PM Subject: RE: UDF best practices | If I have a "page" request that calls several templates during the | request... | | For Example: | | + application.cfm | + myUDF.cfm | + index.cfm | + cfinclude template1.cfm | + cfmodule template2.cfm | | Should I be able to include myUDF.cfm in the application.cfm file and it | will be available to index.cfm, template1.cfm, and template2.cfm as myUDF()? | Currently I'm getting "Error resolving parameter myUDF" with this setup when | calling it from template2.cfm... | | If I set server.myUDF = myUDF() in application.cfm I get an "The number of | arguments passed to a user-defined function cannot be less than the number | of parameters in its definition. This call to myUDF passes 0 arguments, but | 2 parameters are defined.". So when I pass in two dummy variables | server.myUDF = myUDF('x','x') I get an "The symbol you have provided | (server.myUDF) is not the name of a function." in the calling page... Would | I need to make the arguments non-required to make this work properly... | | In this particular case, I'm running this in a hosted environment, so I | don't have access to put them in the /cfide/udf/ folder. | | Any insight would be appreciated... | | Orlando | | -Original Message- | From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] | Sent: Wednesday, August 08, 2001 11:40 AM | To: CF-Talk | Subject: RE: UDF best practices | | | You could but what about re-usability outside the application. This is the | reason for the CFINCLUDE of the UDF. There's nothing stopping you from just | putting a UDF in the application.cfm or anywhere on a template. Only | limitation is that a UDF used as part of a CFINCLUDE must be defined before | it's to be used where a page based one can be anywhere on the page (even | after the UDF call). | | At 12:59 PM 8/8/01, you wrote: | >Couldn't you just put them in the application.cfm for your application?? | > | >-Original Message----- | >From: Michael Dinowitz [mailto:[EMAIL PROTECTED]] | >Sent: 08 August 2001 15:11 | >To: CF-Talk | >Subject: Re: UDF best practices | > | > | >1. In the admin set the server variables to auto-lock | >2. run the template containing the UDFs before the application runs or on | >machine startup. | >3. Set a CFLOCK with a scope of server | >4. Set each UDF to a variable in the scope server | > | >5. Call the UDF on any page without using a CFINCLUDE of it | > | > | >This is slightly slower than having the UDFs in a CFINCLUDE that's included | >into the page. It's a better practice to do the following: | > | >1. Create a small library of UDF's in a template (i.e. all data validation | >in one file and all text manipulation in another). | >2. Save these UDF library templates in the CFIDE/UDF directory. | >3. Use a in the template you | >want to use UDFs in. | > | > | >At 08:59 AM 8/8/01, you wrote: | > | > >Anyone know what's the best way to set a UDF as a global function for use | >by | > >all tags (custom, includes, etc.)... or does one in fact have to include | >the | > >function code locally in each file that's being called during a page | > >request? | > > | > >Thanks, | > >Orlando | > > | > > | > | ~~ Structure your ColdFusion code with Fusebox. Get the official book at http://www.fusionauthority.com/bkinfo.cfm FAQ: http://www.thenetprofits.co.uk/coldfusion/faq Archives: http://www.mail-archive.com/cf-talk@houseoffusion.com/ Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists
RE: UDF best practices
Ok... that did it... It was saving using the and something that is not recommended? Thanks again! Orlando -Original Message- From: Bryan LaPlante [mailto:[EMAIL PROTECTED]] Sent: Thursday, August 09, 2001 1:44 AM To: CF-Talk Subject: Re: UDF best practices I am getting pretty good results with saving my udf's in the session scope. see the tutorial at http://www.kcfusion.org/Presentations/cflp.zip Bryan - Original Message - From: "Correa, Orlando (ITSC)" <[EMAIL PROTECTED]> To: "CF-Talk" <[EMAIL PROTECTED]> Sent: Wednesday, August 08, 2001 9:02 PM Subject: RE: UDF best practices | If I have a "page" request that calls several templates during the | request... | | For Example: | | + application.cfm | + myUDF.cfm | + index.cfm | + cfinclude template1.cfm | + cfmodule template2.cfm | | Should I be able to include myUDF.cfm in the application.cfm file and it | will be available to index.cfm, template1.cfm, and template2.cfm as myUDF()? | Currently I'm getting "Error resolving parameter myUDF" with this setup when | calling it from template2.cfm... | | If I set server.myUDF = myUDF() in application.cfm I get an "The number of | arguments passed to a user-defined function cannot be less than the number | of parameters in its definition. This call to myUDF passes 0 arguments, but | 2 parameters are defined.". So when I pass in two dummy variables | server.myUDF = myUDF('x','x') I get an "The symbol you have provided | (server.myUDF) is not the name of a function." in the calling page... Would | I need to make the arguments non-required to make this work properly... | | In this particular case, I'm running this in a hosted environment, so I | don't have access to put them in the /cfide/udf/ folder. | | Any insight would be appreciated... | | Orlando | | -Original Message- | From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] | Sent: Wednesday, August 08, 2001 11:40 AM | To: CF-Talk | Subject: RE: UDF best practices | | | You could but what about re-usability outside the application. This is the | reason for the CFINCLUDE of the UDF. There's nothing stopping you from just | putting a UDF in the application.cfm or anywhere on a template. Only | limitation is that a UDF used as part of a CFINCLUDE must be defined before | it's to be used where a page based one can be anywhere on the page (even | after the UDF call). | | At 12:59 PM 8/8/01, you wrote: | >Couldn't you just put them in the application.cfm for your application?? | > | >-Original Message----- | >From: Michael Dinowitz [mailto:[EMAIL PROTECTED]] | >Sent: 08 August 2001 15:11 | >To: CF-Talk | >Subject: Re: UDF best practices | > | > | >1. In the admin set the server variables to auto-lock | >2. run the template containing the UDFs before the application runs or on | >machine startup. | >3. Set a CFLOCK with a scope of server | >4. Set each UDF to a variable in the scope server | > | >5. Call the UDF on any page without using a CFINCLUDE of it | > | > | >This is slightly slower than having the UDFs in a CFINCLUDE that's included | >into the page. It's a better practice to do the following: | > | >1. Create a small library of UDF's in a template (i.e. all data validation | >in one file and all text manipulation in another). | >2. Save these UDF library templates in the CFIDE/UDF directory. | >3. Use a in the template you | >want to use UDFs in. | > | > | >At 08:59 AM 8/8/01, you wrote: | > | > >Anyone know what's the best way to set a UDF as a global function for use | >by | > >all tags (custom, includes, etc.)... or does one in fact have to include | >the | > >function code locally in each file that's being called during a page | > >request? | > > | > >Thanks, | > >Orlando | > > | > > | > | ~~ Structure your ColdFusion code with Fusebox. Get the official book at http://www.fusionauthority.com/bkinfo.cfm FAQ: http://www.thenetprofits.co.uk/coldfusion/faq Archives: http://www.mail-archive.com/cf-talk@houseoffusion.com/ Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists
RE: UDF best practices
>It's interesting how ColdFusion 5.0 seems to allow these functions to be >saved in a variable scope by referencing the functions as if they were >variables [scope.func=func]... rather than by referencing them as if they >were functions [scope.func=func() or scope.func()=func()]... well wouldn't scope.func=func() set the new var with the return value of the function? And scope.func()=func() just doesn't make sense. But I wonder whey they didn't allow for us to specify the scope of the function while declaring the function, that makes more sense to me anyhow? something like: function server.myFunction(){} Robert -Original Message- From: Correa, Orlando (ITSC) [mailto:[EMAIL PROTECTED]] Sent: Monday, August 13, 2001 3:02 PM To: CF-Talk Subject: RE: UDF best practices Ok... that did it... It was saving using the and something that is not recommended? Thanks again! Orlando -Original Message- From: Bryan LaPlante [mailto:[EMAIL PROTECTED]] Sent: Thursday, August 09, 2001 1:44 AM To: CF-Talk Subject: Re: UDF best practices I am getting pretty good results with saving my udf's in the session scope. see the tutorial at http://www.kcfusion.org/Presentations/cflp.zip Bryan - Original Message - From: "Correa, Orlando (ITSC)" <[EMAIL PROTECTED]> To: "CF-Talk" <[EMAIL PROTECTED]> Sent: Wednesday, August 08, 2001 9:02 PM Subject: RE: UDF best practices | If I have a "page" request that calls several templates during the | request... | | For Example: | | + application.cfm | + myUDF.cfm | + index.cfm | + cfinclude template1.cfm | + cfmodule template2.cfm | | Should I be able to include myUDF.cfm in the application.cfm file and it | will be available to index.cfm, template1.cfm, and template2.cfm as myUDF()? | Currently I'm getting "Error resolving parameter myUDF" with this setup when | calling it from template2.cfm... | | If I set server.myUDF = myUDF() in application.cfm I get an "The number of | arguments passed to a user-defined function cannot be less than the number | of parameters in its definition. This call to myUDF passes 0 arguments, but | 2 parameters are defined.". So when I pass in two dummy variables | server.myUDF = myUDF('x','x') I get an "The symbol you have provided | (server.myUDF) is not the name of a function." in the calling page... Would | I need to make the arguments non-required to make this work properly... | | In this particular case, I'm running this in a hosted environment, so I | don't have access to put them in the /cfide/udf/ folder. | | Any insight would be appreciated... | | Orlando | | -Original Message- | From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] | Sent: Wednesday, August 08, 2001 11:40 AM | To: CF-Talk | Subject: RE: UDF best practices | | | You could but what about re-usability outside the application. This is the | reason for the CFINCLUDE of the UDF. There's nothing stopping you from just | putting a UDF in the application.cfm or anywhere on a template. Only | limitation is that a UDF used as part of a CFINCLUDE must be defined before | it's to be used where a page based one can be anywhere on the page (even | after the UDF call). | | At 12:59 PM 8/8/01, you wrote: | >Couldn't you just put them in the application.cfm for your application?? | > | >-Original Message- | >From: Michael Dinowitz [mailto:[EMAIL PROTECTED]] | >Sent: 08 August 2001 15:11 | >To: CF-Talk | >Subject: Re: UDF best practices | > | > | >1. In the admin set the server variables to auto-lock | >2. run the template containing the UDFs before the application runs or on | >machine startup. | >3. Set a CFLOCK with a scope of server | >4. Set each UDF to a variable in the scope server | > | >5. Call the UDF on any page without using a CFINCLUDE of it | > | > | >This is slightly slower than having the UDFs in a CFINCLUDE that's included | >into the page. It's a better practice to do the following: | > | >1. Create a small library of UDF's in a template (i.e. all data validation | >in one file and all text manipulation in another). | >2. Save these UDF library templates in the CFIDE/UDF directory. | >3. Use a in the template you | >want to use UDFs in. | > | > | >At 08:59 AM 8/8/01, you wrote: | > | > >Anyone know what's the best way to set a UDF as a global function for use | >by | > >all tags (custom, includes, etc.)... or does one in fact have to include | >the | > >function code locally in each file that's being called during a page | > >request? | > > | > >Thanks, | > >Orlando | > > | > > | > | ~~ Structure your ColdFusion code with Fusebox. Get the official book at http://www.fusionauthority.com/bkinfo.cfm FAQ: http://www.thenetprofits.co.uk/coldfusion/faq Archives: http://www.mail-archive.com/cf-talk@houseoffusion.com/ Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists
RE: UDF best practices
> It's interesting how ColdFusion 5.0 seems to allow these > functions to be saved in a variable scope by referencing > the functions as if they were variables [scope.func=func]... > rather than by referencing them as if they were functions > [scope.func=func() or scope.func()=func()]... Actually, given the behavior of functions in other languages such as JavaScript, this is the behavior I'd expect. For example, if you wanted to assign a function to the window error handler, you might have something like this: function myerrorhandler(msg, url, lno) { ... } window.onerror = myerrorhandler; Of course, JavaScript is an object-oriented language, while CF isn't. > I haven't tested yet what happens when you have a local variable > named "mylocal" and a local function named "mylocal"... then > set server.mylocal=mylocal to see if it saves the variable in > server scope or the function in server scope... I haven't tested this, but as a wagering man, I'll bet that whichever one you declare last will be used. That's the standard behavior in CF for duplicate variables within the same scope. > Does anyone know if this "trick" is an official behavior of CF5.0 > or something along the lines of > and something that is not recommended? If a user-defined function can be treated as a variable, there's no reason why this behavior wouldn't be considered "official" since it would follow the normal behavior of variables. As for the CFSET example you provide, I used to recommend against that, and still personally prefer using SetVariable, but it's mentioned in the CF documentation, so I guess it's official in that sense. (Even though CF documentation tends not to be authoritative in any meaningful sense.) Finally, however, I don't know that it's a good idea to "globalize" functions by storing them in variables so that you can use them within custom tags. After all, when you're building a custom tag, you want it to be essentially self-contained to the greatest extent possible - you pass it parameters, and it returns values. The placement of user-defined functions within persistent memory variables violates this model, although it does make it easier to use UDFs within custom tags. Instead, I'd probably stick with an explicit inclusion of UDF libraries using CFINCLUDE within custom tags, or simply physically including the necessary code within the custom tag. I'd probably go with the first of those two, unless building something for widespread distribution. Dave Watts, CTO, Fig Leaf Software http://www.figleaf.com/ voice: (202) 797-5496 fax: (202) 797-5444 ~~ Structure your ColdFusion code with Fusebox. Get the official book at http://www.fusionauthority.com/bkinfo.cfm FAQ: http://www.thenetprofits.co.uk/coldfusion/faq Archives: http://www.mail-archive.com/cf-talk@houseoffusion.com/ Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists
New UDF Best Practices Article on oreilly.com
Just wanted to let the list know about an article I recently did for O'Reilly's web site oreilly.com. The article highlights best practices for writing UDFs and is called "Top Ten ColdFusion UDF Tips". You can view the article at http://www.oreillynet.com/pub/a/javascript/2002/02/22/udftips.html.; I welcome any questions or comments. Thanks! -Rob __ Dedicated Windows 2000 Server PIII 800 / 256 MB RAM / 40 GB HD / 20 GB MO/XFER Instant Activation · $99/Month · Free Setup http://www.pennyhost.com/redirect.cfm?adcode=coldfusiona FAQ: http://www.thenetprofits.co.uk/coldfusion/faq Archives: http://www.mail-archive.com/cf-talk@houseoffusion.com/ Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists