5 Ways To Rubify Your ColdFusion Functions

"A sentence should contain no unnecessary words, a paragraph no unnecessary sentences, for the same reason that a drawing should have no unnecessary lines and a machine no unnecessary parts." - William Strunk, Jr.

Over the last few years of object oriented programming using ColdFusion, I have shifted away from a development style that I would consider to be in line with ColdFusion best practice, towards something more Ruby-like. I wanted to document the changes I have made and why I have personally found them beneficial. Let me display the transition.


<!—- This function will return the artist by its name  —->

<cffunction name=”getArtist” output=”false” returntype=”artist” access=”public” hint=”this method will return the artist by its name”>

<cfargument name=”name” required=”true” type=”string” hint=”the artists name” />

    <cfset variables.artist = entityLoad(“artist”,{name=arguments.name},true) />

   <cfreturn variables.artist />



function getArtist(name){

   artist = entityLoad(“artist”,{name=arguments.name},true);

   return artist;



I no longer specify the access type if the function is public. The access type is set to public by default, which makes access attribute redundant in such cases. This means less code to type without any significant loss in terms of expressiveness. Yes!


I now write all of my functions in cfscript, which does not output anything by default, meaning that the output attribute is also redundant! BoooYah! I feel that my code is a lot cleaner in cfscript, as there is less code required to achieve the same thing. Scripted code also resembles other languages more closely, which is beneficial when learning from and coding in other programming languages.


I no longer use the returntype attribute for type checking, instead I move any type based validation into my unit tests. BAM! Moving these checks into the unit tests also means a seperation of concerns, leaving the method to focus solely on behaviour rather than validation.


I no longer add hints to my functions, I aim to make my code so expressive that hints and comments arent required. I find that hints and comments tend to duplicate what should be expressed through the code in the first place, in essence, its a way of compensating for the code not being expressive enough. Comments and hints also require more unnecessary writing, which again making coding less joyous for me.

Variables Scope

"I want to get to the #variables.nitty_gritty#"

I no longer declare the scope of any variables in the variables scope, as all unscoped variables are placed there automatically. I find that scoped variables add noise to my code, but in the case of local variables the safety benefits outweigh the costs of readability. With variables in the variable scope however, this isnt the case, here I prefer to get straight to the nitty gritty - the variable name itself., rather than polluting the readability of my code.


To conclude, I favour conciseness over upfront type checks and extensive documentation. I guess my argument is that the benefits of type-checking and documentation can be achieved more successfully through unit-testing and expressive code, without the downside of having to write more to achieve the same thing. This is the greatest benefit for me, especially when writing a large application, it gives me more time to focus on the design, which I believe is the real fun/skill in software development. Less is definitely more.

OOP - explained using music

One of the biggest issues when learning object oriented programming is getting your head around some of the concepts. The main problems I have found is that most explanations are really complex and use uninspiring examples, mainly involving a car. I have decided to explain the main principles and concepts of object oriented development in a more succinct, invigorating way - using song titles.

  • Class   - “Blueprint” - Jay Z  
  • Method - “Human behaviour” - Bjork
  • Inheritance - “Father to son” - Queen
  • Encapsulation - “The lost art of keeping a secret” - Queens of The Stone Age
  • Coupling - “Where I end and you begin” - Radiohead
  • Polymorphism - “Fake plastic trees” - Radiohead

This tasks proved more difficult then I presumed, so most song titles became obscure descriptions of the OOP terms. I would love to know what you think, and if you have any suggestions of your own. 

Please leave this code clean for the next user.

How many times have you navigated through your code base and felt completely bamboozled. What is this function doing? what does this variable refer to? why does this class exist, why am I still a programmer? The list goes on and on. Not only is this a frustrating experience, its also a massive waste of programmers time and energy.     

Having recently taken on a legacy project, I know this feeling all too well.  Functions with misleading names, classes so large its intimidating, and so strongly coupled you’d think they where planning their honeymoon together. So what can we do about it? Well as the sign in the toilet clearly states, “Please leave this clean for the next user”. We all need to maintain a healthy codebase by cleaning and clarifying our code as part of the development schedule.

I will be writing a number of short blog posts demonstrating how to apply the ”5S Method” - (Sort, Set in Order, Shine, Standardize and Sustain) to our development process. There will be several tips on how to  make our code base easier to understand, for ourselves, and for those lucky enough to adopt our projects. 

For now, lets take one positive action. Quickly skim through your code base and find the one function that is  most obscure or unclear. Change its name so there is absolutely no confusion towards its purpose. Please leave a comment on the function you changed and the reasons for the change.

Written by Forever development - 

Ever encountered a problem that you have solved before, but cant remember how you did it? Re-use your experience and effort better by using a solutions log for web developers.

Speedy unit tests using transactions

Whilst learning ruby on rails I stumbled upon a method for speeding up unit tests using database transactions. I wanted to adapt this method to my coldfusion 9 ORM unit tests to make them as speedy as….speedy gonzales! 

This method, as documented here, loads a transaction before each test, then performs a transaction rollback to restore the database back to its original state. This seemed like a much more efficient technique than those I had tried before - Calling a function to manually delete all objects from the database following each test (very time consuming and unreliable) - Firing ormReload() in the setUp function (extremely sloowwww). Anyway, heres what I did. 

First, I trigger a transaction in the setUp function of my test component. 


public void function setUp(){

         artistService= new model.artistService();

         VARIABLES.transaction = ormGetSession().beginTransaction();


Then perform a rollback in the tearDown.


void function teardown(){



This means I can run my unit tests knowing that the database will revert back to its original state following each test. Job done? Errr….not quite! Although this method works brilliantly in most cases, it falls down if there are any transactions in the code we are unit testing. For example, if I run one of my tests.


public void function test_should_create_artist_with_transaction(){

  local.data= {

       firstname = ‘james’,

surname = ‘blake’,                                                                                              

style = ‘dubstep’


        VARIABLES.artist= artistService.create(data);




I recieve the error org.hibernate.SessionException: Session is closed at File Path\artistTest.cfc: 10. Which is where VARIABLES.trans.rollback() is called in my teardown function.

This is because I am calling a transaction in the save method of the artist service I am unit testing, which is prematurely closing the original transaction triggered in the setUp method. Here is the code for that save method.


public void function save(required artist){





To get round this problem, I use MxUnits funky injectMethod to replace the save method in my component under test with an exact replica - minus the transaction. So my setup function now looks like this…


public void function setUp(){

     artistService = new model.artistService();

     injectMethod(artistService, this, “saveOverride”, “save”);

     VARIABLES.transaction = ormGetSession().beginTransaction();


And I have an override method for my component under test.


private void function saveOverride(required artist){



With this in place the unit tests run on my component under test with no problems , and are much faster then when I was calling ormReload() in the setUp.

Any views on the techniques described here? Or any suggestions for improvement would be appreciated.

Following the recent article in smashing magazine about music and productivity,  I wanted to share my thoughts on music and its varying effects on my own productivity when developing.

I personally find that certain types of music help me feel motivated, enthuse me and keep me mentally focussed, whilst others completely distract me from the task at hand, especially when it requires a little bit of brain power.

Much like the smashing magazine article, I believe rhythm to the be the most important factor in determining which songs to listen to when coding, it is the rhythm and flow of a piece which induces that hypnotic state that I love - where I feel focussed and energised, where I forget the music is actually playing, I get lost in the task at hand.

I also feel that it is important to select music which is not distracting. I find music with lyrics to turn my attention away from the complex refactoring I am working on.  As does the excitement of hearing the latest chemical brothers release, I leave any new music that I have been anticipating till my own leisure time.

The video above is a piece by steve reich which I have been listening to a lot recently. There is quite a strong driving rhythm throughout the piece, and its repetitive nature is quite hypnotic and calming, perfect for getting in that ‘zone’.

I like to also suggest the hype machine for building a playlist of songs to work to. I have a list of favourite songs on the site which I just let run. This prevents me from breaking my pomodoros with internal interruptions.

So, what songs do you like to listen to whilst coding?