Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at cf.Objective() 2010 (Minneapolis, MN) with: Doug Hughes and Ezra Parker and Dan Wilson and John Mason and Jason Dean and Luis Majano and Mark Mandel and Brian Kotek and Wil Genovese and Rob Brooks-Bilson and Andy Matthews and Simeon Bateman and Ray Camden and Chris Rockett and Joe Bernard and Dan Skaggs and Byron Raines and Barney Boisvert and Simon Free and Steve 'Cutter' Blades and Seth Bienek and Katie Bienek

StructAppend() the Forgotten Function

By Ben Nadel on
Tags: ColdFusion

In my applications, I create an Attributes object (REQUEST.Attributes), which is a unbinding of the URL and the FORM scope. I was first introduced to this idea in FuseBox and I really like how it can simplify a good amount of code.

I was going over the code I use to create this last week. I thought my code was very simple, and yet some how, my boss, Jim Curran, was able to make a suggestion to simplify it even further. StructAppend(). Admittedly, I had to look it up because I wasn't exactly sure what it did - I am not sure I have ever used it.

Well, it does exactly what I wanted it to do. Of course, the code was very simple before, but now, it is even more so. I was able to take this:

  • // Copy the URL into the Attributes sturct. Unless we want to explicitly get
  • // information from the FORM variables (as we do in add/edit screens), we are
  • // just going to get it from the Attributes object to make things simpler and
  • // the scoping more consistent.
  • REQUEST.Attributes = Duplicate(URL);
  •  
  • // Now, go over the form variables and copy those into attributes.
  • for (LOCAL.Key in FORM){
  • REQUEST.Attributes[ LOCAL.Key ] = FORM[ LOCAL.Key ];
  • }

and make it this:

  • // Copy the URL into the Attributes sturct. Unless we want to explicitly get
  • // information from the FORM variables (as we do in add/edit screens), we are
  • // just going to get it from the Attributes object to make things simpler and
  • // the scoping more consistent.
  • REQUEST.Attributes = Duplicate(URL);
  •  
  • // Now, move the form values into the Attributes object.
  • StructAppend( REQUEST.Attributes, FORM );

As you can see, the difference is small, but we have removed a coded Loop and let ColdFusion handle the process with a built-in, hopefully more efficient method. All to say, sometimes, I try to come up with a solution that ends up being more complicated than a solution that is built into ColdFusion. This only goes to emphasize the point that people really need to understand the basics of the language.

Tweet This Great article by @BenNadel - StructAppend() the Forgotten Function Thanks my man — you rock the party that rocks the body!


Reader Comments

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.