ColdFusion MX Coding Guidelines - Good Practice

Good Practice

This section provides some hints and tips that are considered "good practice".

Booleans

Booleans are always true or false, conditions are also true or false - do not test a boolean expression against a value, just test the expression:

view plain print about
1<cfif boolExpr is "true"> <!--- BAD! --->
2<cfif boolExpr is "false"> <!--- BAD! --->
3<cfif boolExpr> <!--- good --->
4<cfif not boolExpr> <!--- good --->

On the other hand, don't rely on implicit conversions from numeric types to boolean. Functions like compareNoCase() and listFind() should be explicitly tested against zero for clarity instead of relying on the "0 is false" conversion:

view plain print about
1<cfif compareNoCase(x,y)> <!--- BAD! --->
2<cfif compareNoCase(x,y) neq 0> <!--- good --->

cfswitch/cfcase

Instead of cascading cfelseif blocks, when you are branching on the value of a single expression, use cfswitch and cfcase instead:

view plain print about
1<!--- bad: --->
2<cfif expr eq "Value A">
3        ...
4    <cfelseif expr eq "Value B">
5        ...
6    <cfelse>
7        ...
8    </cfif>
9<!--- good: --->
10<cfswitch expression="expr">
11    <cfcase value="Value A">
12        ...
13    <cfcase>
14    <cfcase value="Value B">
15        ...
16    </cfcase>
17    <cfdefaultcase>
18        ...
19    </cfdefaultcase>
20</cfswitch>

Components & cfargument

ColdFusion MX does not require that you use cfargument tags but they provide validation (type safety) and act as additional documentation - always provide a cfargument tag for each named argument your function expects and follow these rules:

  • Always specify the type= attribute in your cfargument tags. Try to avoid using type="any".
  • If an argument is required, specify required="true" but do not specify a default= attribute.
  • If an argument is not required, either specify a default= attribute if appropriate or specify required="false" but do not provide both.
  • If you need to detect whether a non-required argument was provided, do not specify default= but instead use structKeyExists(arguments,"argName") in the function body (remember that when you specify default=, you cannot tell the difference between the caller omitting that argument and the caller providing that same default value as an argument).
view plain print about
1<cfargument name="badArg" type="string" required="false" default="foo" />         <!--- BAD! --->
2<cfargument name="veryBadArg" type="string" required="true" default="silly" />     <!--- BAD! --->
3<cfargument name="iAmRequired" type="string" required="true" />     <!--- good --->
4<cfargument name="iAmOptional" type="string" required="false" />     <!--- good --->
5<cfargument name="iHaveADefault" type="string" default="foo" />     <!--- good --->

Components & cfproperty

Be aware that the cfproperty tag is of limited use - it does only two things:

  1. It provides additional type validation for Web Service return types.
  2. It provides a way to add metadata to components.

In general, cfproperty should not be used - it does not provide type checking for instance data (in general) and it does not provide default values for instance data so its presence can be misleading rather than helpful.

If you really are returning a component type from a Web Service and want the public data members of that component to be type-checked at runtime, then cfproperty can be useful.

If you are creating a metadata-driven system, then cfproperty can be useful (although dynamic systems driven by component metadata tend not to scale well!).

Components & Constructors

All components should define a method called init() that initializes the instance, even if the body of the method is empty, i.e., the initialization doesn't do anything. This makes usage of components consistent since you are guaranteed to be able to call init() on any component instance, just as you can for Java objects created within ColdFusion. The method should have a return type that matches the component and it should return this so that the following construct is always legal:

view plain print about
1<cfset obj = createObject("component","util.lib.thing").init() />

This matches the way that createObject().init() behaves for Java objects - init() returns the fully initialized Java object.

The util.lib.thing CFC would have a method that looks like:

view plain print about
1<cffunction name="init" access="public" returntype="thing">
2        ...
3        <cfreturn this />
4</cffunction>

If your component extends another component, your init() method should begin with a call to super.init() to initialize the base component.

Note: There is a notable exception to this guideline - when you are extending components in Mach II (listeners, plugins, event filters), instead of defining a method called init(), you define a method called configure() (which does not return anything - configure() has returnType="void"). This is because the framework itself uses init() for its own initialization and provides configure(), which it automatically calls for you once the framework has been initialized, so that you do not have to worry about any initialization arguments that the framework requires.

Cookies

Persistent cookies are intrusive - use them sparingly. Note that by default CFID / CFTOKEN are persistent cookies which is one of the reasons why we enable J2EE Session Variables (in the ColdFusion Administrator): jsessionid is in-memory so it's not as intrusive and it goes away when the browser closes (along with the user session, so it is more secure than using persistent CFID / CFTOKEN cookies for session management).

If your application requires cookies in order to work correctly, such as for session management or login, then handle the situation where a user has cookies disabled and do it gracefully - tell the user they need cookies enabled.

Modifying every link and form in your application to append the appropriate tokens (jsessionid or CFID / CFTOKEN) by using URLSessionFormat() might be an option but that opens up the security risk of users sharing links and inadvertently sharing session data (this is not an option for macromedia.com because the security risks are too high).

Custom Tags

Custom tags should be organized into related groups by using a meaningful directory tree under the Custom Tag Path (defined in the ColdFusion Administrator). This makes it easier to use cfimport to access specific groups of custom tags.

All custom tags should anticipate being executed in both start mode and end mode, i.e., they should be structured as follows:

view plain print about
1<cfif thisTag.executionMode is "start">
2 ...
3<cfelse> <!--- thisTag.executionMode is "end" --->
4 ...
5</cfif>

If the custom tag is a simple one, i.e., it doesn't process a tag body, then the cfelse section should be empty (and cfelse can be omitted). Do not put spurious complaints in the cfelse section about being called twice!

Do not hardcode a result variable in a custom tag, e.g., caller.result. Instead, use either a result= or a returnVariable= attribute and return results by setting the specified variable, i.e.,caller[attributes.returnVariable]. This is more flexible and also reduces coupling between a custom tag and its calling page.


Source : Adobe.com©

Related Blog Entries

TweetBacks
Comments
timberland shoe company's Gravatar We are traveling by train. Out timberland hiking boots windows, we drink in the passing scene of cars on nearby highways, of children timberland shoe company waving at a crossing, of cattle grazing on a distant timberland boots hillside, of smoke pouring from a power plant, of row upon row of corn and wheat, of flatlands and timberland wheat shoes valleys, of mountains and rolling classic 3 eye timberland boat hillsides, of city skylines and village halls. But uppermost in our black timberland boots minds is the final destination.
# Posted By timberland shoe company | 28/08/10 10:25
Apad's Gravatar Willkommen auf unserer Internetseite, Efox-Shop, dass Sie bietet alle Arten von bevorzugten Produkte wie <a href="http://www.efox-shop.com"> Tablet PC </ a>
<a href="http://www.efox-shop.com/tablet-pc-notebook-apad-c-129_235.html"> Epad </a>, Billige Notebook , <a href="http://www.efox-shop.com/tablet-pc-notebook-apad-c-129_235.html"> APAD </ a>
android 2.1 Tablet PC und <a href="http://www.efox-shop.com/mp3mp4mp5-player-c-148.html">" target="_blank">http://www.efox-shop.com/mp3mp4mp5-player-c-148.ht...; mp3 player</ a>
mit hoher Qualität, Mode-Stil und konkurrenzfähigen Preis in http://www.efox-shop.com.
# Posted By Apad | 31/08/10 13:43
BlogCFC was created by Raymond Camden. This blog is running version 5.9.5.005. Contact Blog Owner