Wednesday, May 17, 2006

Bad cfc form?

Is the following code considered bad form?


<cffunction name="init" access="public" output="false" returntype="templateSectionName">
<cfargument name="dsn" required="false" default="#session.managedApp.getDsn()#" type="string" />


How big of a problem is it to violate encapsulation by pulling values in from persistent scopes? Are there varying degrees of acceptability, for example, is it okay to do this hear where I'm just supplying a default value as opposed to using a value in the rest of the component.

Are different scopes more acceptable than others for doing something like this. I would guess that pulling in a value from the application scope is probably more reliable than pulling something in from the session scope.

The problem I see with the code above is that code that uses this component may be written without defining a datasource directly. In that case the component may still work as long as the user's session is maintained and the ManagedApp component is in good shape in the session scope. The problem that I think this approach introduces is that where this code


<cfargument name="dsn" required="true" type="string" />


fails the first time it's run and fails consistently until corrected, the other method may run fine for quite a while before it fails because a session has timed out. Had the value existed in the application scope, I'd be more inclined to let it slide.

Any thoughts?

No comments:

Post a Comment