The Dopefly Tech Blog

« The Dopefly Tech Blog Main page

Fun with Functions and CFCs VIII - Functions as Variables and Functional Programming

posted under category: ColdFusion on December 19, 2007 by Nathan

CFML acts a lot like Javascript, really. In this series, I've been throwing around functions like they were candy. Indeed, ColdFusion and CFML let you do that. So let's have some fun with it.

function add(n1,n2) {return n1+n2;}
function subtract(n1,n2) {return n1-n2;}

function doSomethingWith2Numbers(thingToDo, n1,n2) { return thingToDo(n1,n2);}

writeOutput( doSomethingWith2Numbers( add, 5, 5) );
writeOutput( doSomethingWith2Numbers( subtract, 5, 5) );


Just the fact that you can is awesome.

The best benefit I can see right now for tossing around functions as arguments is for late-binding actions. You compile a handful of things to do, then execute them at the last possible moment. I like this approach for generating UI elements from OO systems in particular. We set a bunch of preferences, then, when you call render(), the system spits it out based on different code generation methods that were previously defined. That's one of my favorite things: instead of asking if a switch is on or off, just execute the method resting in a function reference.

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.