Search:
Calendar:
| Sun | Mon | Tue | Wed | Thu | Fri | Sat |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
Subscribe:
Archives By Subject:
Adobe (33) [RSS]
AIR (7) [RSS]
Ajax (3) [RSS]
Buzzword (1) [RSS]
CFUG (42) [RSS]
ColdFusion (70) [RSS]
Flash (6) [RSS]
Flex (40) [RSS]
Frameworks (3) [RSS]
Free Software (2) [RSS]
FXUG (22) [RSS]
General (35) [RSS]
JpgMetadataReader (3) [RSS]
jQuery (1) [RSS]
Mac (2) [RSS]
Off Topic (13) [RSS]
OpenSource (14) [RSS]
PaperVision (10) [RSS]
Spry (2) [RSS]
Transfer (1) [RSS]
Tags:
Recent Entries:
Top Posts:
- [19131] Paper Vision 3D Sphere - Carousels on Steroids!
- [11223] Simple Dynamic Cursor Example in Flex2: With source code
- [10710] Simple PaperVision3D Blurring example : source code
- [10699] Simple PaperVision3D Blurring example
- [10535] PaperVision3D/Flex2 Carousel v0.2
- [10320] Carousel v0.2 - source code
- [10320] Saving images from Flex using ColdFusion: Simple Example with source code
- [9850] PaperVision Carousel0.3 - one step closer to the master plan!
- [8471] PaperVision3D is good for cubes! - a "geek" gift
- [7862] Simple Effects on a Form - Flex Example
Recent Comments:
- PaperVision3D/Flex2 Carousel v0.2
Arindam said: http://www.citroen.co.uk/... see the above links how can I create this kind of slider? If ... [More] - Paper Vision 3D Sphere - Carousels on Steroids!
Reflexion Graphic said: Nice job ! [More] - Getting Red from my color
David said: @Promethe: Thanks for the comment. Jason uses that way too in his example. working with number as n... [More] - Getting Red from my color
Promethe said: guys... seriously: var myColor : uint = 0xef556ef; var myRed : uint = (myColor >> 16) & ... [More] - Getting Red from my color
David said: @Jason: Thanks for the example! I never like working with numbers-to-strings-to-numbers. I'll pull y... [More]
Top Commenters:
- [8] Campbell
- [8] Ben Nadel
- [6] Mark Flewellen
- [6] Steve Bryant
- [4] Raymond Camden
- [4] barry.b
- [3] John Whish
- [3] Peter Bell
- [2] Cory
- [2] carlos
My Links:
RSS:
ColdFusion and constants - how do you do them?
I've been working on a project a bit lately and found I've had the need to use some constants.
This has gotten me thinking how to implement them.
When work in Flex, I use the Extensions for Adobe Cairngorm and when creating your custom events involves you declaring constants in the event. The consts in the event are related to the Event type.
The idea behind constants is that this is information that *will never change*, hence the name "constants" :-)
In the CF application I've been working on, UUIDs are used, and there is no way you can call a UUID "human readable", so this can make code harder to understand when you see something like:
If you want the person who reads this code later understand which page that is with out having to pull the application apart (and hair out too sometimes), you might to this:
<cflocation url="/index.cfm?page=#uInfoPage#">
That way the name of the variable will indicate *what* that UUID value indicates.
With me so far?
"But Dave, my application has more than one template and I don't want to have to do that every time I want to send a user to the info page, and what happens if the UUID of the info page changes?" you cry.
Well, why not have a single "thing" that stores all your constants that you can store in one place and use everywhere?
What I started doing here is I created a "Const.cfc" that had getter functions on them to return the constants. EG:
<cffunction name="getInfoPageUUID">
<cfreturn "70688909-1676-D9C3-F2C5B9F163AEBDCF">
</cffunction>
</cfcomponent>
(Another note: I'd usually declare/call the function in all CAPS too, as this is the "standard" way of naming constants)
The above relies on "oConst" being created, which could be stored in application scope for example.
What I didn't like about this is that the actual value is hidden from my old friend "cfdump", and if I have alot of constants I can't see their values in one hit.
This lead me to the "this" scope of my CFC(s).
Something like this:
<cffunction name="init">
<cfset this.INFOPAGEUUID = "70688909-1676-D9C3-F2C5B9F163AEBDCF">
<cfreturn this>
</cffunction>
</cfcomponent>
...which them means the example code can be written like this:
Before you cry "But people can change the values in the 'this' scope and break stuff!", I've always thought if people want to do that, it's up to them, but don't come running to me if it breaks! (Also, CFCs are not secure at all anyway! see related entry)
The two things I like about using the "this" scope for constants are:
- 1: You can see all the values if you cfdump it
- 2: If you CFC has functions, you don't have lots of functions to work out that don't really do anything
Let me explain the second point a bit further.
Imagine I have a DB table that stores information of pages on my site. My CFC has functions to work with this data. In my application I have 1meeeeeeelllion UUIDs to do with various parts of the application, that are used in various ways, that would help the readability of my code if they are stored as constants. This would render the "One Constant.cfc to rule them all" a bit hard to manage. What I have been doing is storing the UUIDs to do with that part of the application in the CFC that deals with that part of the application.
EG:
<cffunction name="init">
<cfset this.INFOPAGEUUID = "70688909-1676-D9C3-F2C5B9F163AEBDCF">
<cfset this.HOMEPAGEUUID = "70826CD8-1676-D9C3-F256954C82CAF104">
<cfreturn this>
</cffunction>
<cffunction name="doSomething">
</cffunction>
<cffunction name="doSomethingElse>
</cffunction>
...lots more functions here...
</cfcomponent>
If you create that cfc and dump it as below:
<cfdump var="#oMyObject#">
Given the above code uses no frameworks and if any of you managed to read all my brain dump, what thoughts do you have on the above and/or how have you approached ColdFusion constants?
Disclaimer: all the above code is psedo code and hasn't actually been tested, but feel free to point out syntax errors etc. I may change it, I may not...

We have a developer console (dashboard) that will allow you to restart the app, display constant values, etc.
StructKeyInsert( "Application.Constants.HOMEPAGEUUID", "70826CD8-1676-D9C3-F256954C82CAF104", false );
You can also use ColdSpring with a config bean. ModelGlue has one, that write all the getters for you. see: http://www.dougboude.com/blog/1/2007/06/Global-Con...
<cfcomponent name="catalog">
<cfset this.IMAGE_SMALL = "small"/>
</cfcomponent>
<cfset product = catalog.GetProductBySku(data.sku)/>
<cfset image = product.GetImage(catalog.IMAGE_SMALL)/>
So in this case, we have a component that stores constants that are used by a number of other smaller classes, but isn't global to the entire application. The product component in the example above could have its own constants if appropriate, and the catalog component does more than just store constants.
Other than providing readability, using constants can be a good way to abstract data, such as in the example above where the constant maps to a database key value. There is no guarantee that the data will be consistent from project to project, especially when the data is being imported from a client's legacy system.
catalog = CreateObject('component', 'catalog');
catalog.IMAGE_SMALL = "new value";
David already pointed that out in his post, and I share his sentiment. A rogue developer who wanted to sabotage the app would be better off with a statement like:
<cfset StructClear(application)/>
I believe that coding for ease of use, and more importantly, ease of reuse, outweighs the minimal concern for "rogue developers", at least in our environment.
When I said a rogue developer I didn't mean a malicious developer who actually wants to cause damage. I meant one who might change one of the constant values in their code to maybe test something and then either forget to delete the code that sets the value or not realise they have updated an application scoped variable and leave it as the wrong value, rather than a malicious developer who actually wants to cause damage.
Totally agree that for most people, ease of use outweighs the rogue developer concern. As long as people know the pros and cons of each approach then using whichever one suits them best is the way to go!
If you create a getConstants() method where you just set up a local var STRUCT with the constants in it then you can call
this.constants = duplicate(getConstants());
in your init() method. If users changed the object.constants struct nothing would happen. They'd require access to the file. And if they complained about nothing happening you could tell them to look up the the term "constant" on dictionary.com.
But they couldn't break your code at least (don't worry they'll find other ways) .
I must have been taking crazy pills for that last comment (ages ago). Of course the user can change the object.constants scope in their use of the object. What I really meant was that inside the cfc you could continue to use getconstants() and always be sure to obtain the value you intended. Outside, well that's another matter, I would say if you don't want the developers to change values then you make them use getconstants() as a public method if they needed to read a value.