Last posts:
Web Design » Tutorials » What's with variables-dot-underscore in my CFC's?
What's with variables-dot-underscore in my CFC's?
Views: 1815 | 1-11-2010, 08:54 | Category : Tutorials, Other

I've had a few people ask me why I often have variables named things like "variables._foo" inside of my CFC's. Specifically, they're wondering about the underscore. In short, it's all about protecting data in the variables scope.

Expanded version of that one-sentence explanation:

If I've got something used by multiple methods in the variable, like a reference to a bean representing datasource information in a DAO, it gets two things: an underscore and a private getter method. Like this:

<cfcomponent displayname="SomeDAO">

<cffunction name="init">
<cfargument name="datasource" />
<cfset variables._datasource = arguments.datasource />

<cffunction name="getDatasource" access="private">
<cfreturn variables._datasource />



It means that if I accidently to this in some function, forgetting to var scope...

<cfset datasource = "wallaWallaBingBang" />

...I'll have only overwritten variables.datasource, not the variables._datasource that everything else uses.

In other words, it's just a quick way to prevent me from harming myself or others with a sloppy error that'd be hard to trace down.


Read also:
  • CF Queries in .NET via Web Services: A Better Approach
  • CFCUnit + FlexUnit How-To: No excuses now!
  • Passing parameter into functions – by value and reference.
  • I Don't Like Rails
  • Polymorphism in C++

  • Vote
    What do you need?

    Website design
    Website development
    PHP Tutorials

    Hire Desk
    Website Design & Development, Php Tutorials
    This is Web developer Blog TheDesignMag
    Only high quality web developming service