The Dopefly Tech Blog

« The Dopefly Tech Blog Main page

Cheating with the 'ol dynamic language function alias trick

posted under category: ColdFusion on May 8, 2009 by Nathan

One of my favorite little tricks to play with CF, and with other dynamic languages like it, is aliasing function names.

Let’s say you have a component, User, and a method, getIsUserNameValid(). It’s a “getter” in order to satisfy some other kind of style requirement, but writing it out is verbose and feels wrong:

if (user.getIsUserNameValid()) {...}

You have to leave it as is so you don’t break any existing method calls, and to keep the basic style guidelines, but what if you could cheat a little? Consider this:

<cfset variables.isNameValid = variables.getIsUserNameValid />
<cfset this.isNameValid = this.getIsUserNameValid />

In ColdFusion, this creates an alias in the private "variables" scope as well as the public "this" scope to the old method. Now you can change your call to:

if (user.isNameValid()) {...}
Nathan is a software developer at The Boeing Company in Charleston, SC. He is essentially a big programming nerd. Really, you could say that makes him a nerd among nerds. Aside from making software for the web, he plays with tech toys and likes to think about programming's big picture while speaking at conferences and generally impressing people with massive nerdiness and straight-faced sarcastic humor. Nathan got his programming start writing batch files in DOS. It should go without saying, but these thought and opinions have nothing to do with Boeing in any way.
This blog is also available as an RSS 2.0 feed. Click your heels together and click here to contact Nathan.