Monday, February 14, 2011

What I Learned at the Milwaukee Hack-a-thon

This past weekend I participated in my first hack-a-thon put on by Spreenkler Talent Labs.  My team consisted of myself and 3 other developers: my good friend Ryan Nanney who is a php developer, Joe Steinbring who is a fellow ColdFusion developer, and Russel Stetzer who is a networking guy looking to break into the development side of things.


As this was my first hack-a-thon event I wasn't sure what to expect going in and I was very careful to make sure that my team didn't break any rules by doing any development work ahead of time.  Looking back I think I may have been too careful.  Prior to the event our team only had two short meetings to discuss our idea and go over some of the frameworks and technologies we were planning on using.   So going into the event we knew that we were going to build an application that would help non profits looking for volunteers connect to professionals that wanted to volunteer their time and that we were going to use ColdFusion 9, ColdFusion ORM, FW/1 as our MVC framework, github as our collaboration hub, and jQuery as our JavaScript framework.


The event itself was held in a computer lab at the University of Wisconsin Milwaukee, which proved to be a bit small for the amount of participants that were in attendance.   I'm glad I decided to play it safe and pack up my keyboard, external monitor, and mousepad since I arrived at the about 5 minutes late and all of the spots with monitors already there were gone.  Lucikly two of my team members arrived early and I was able to get a spot right by them.

Once the event kicked off our team got off to a slow start, I think that it was around 12:00 before we actually wrote a single line of code.  The morning was mostly spent getting everybody's development environment up and running and working on the getting the look and feel of our application sorted out.  When we finally started writing code things became quite hectic in a hurry. While we had our team roles pretty clearly defined ahead of time, we didn't have a very good plan on how we were going to actually build this application.  I was to be the guy person in charge of the master branch of the project and wiring up the different layers of the MVC framework,  Ryan N was to work on the CSS and jQuery, Joe was to work on the data model and database, while Russ helped work on the view pages.   



One of the biggest strategical flaws that I helped push was to use a few technologies that every team member wasn't familiar with, specifically ColdFusion ORM and FW/1.  While I felt that these technologies are extremely simply to use and learn, 8 hours just isn't enough to teach 3 people to use them and be productive myself. 

By the end of the day we had written a lot of code, however our application was quite disjointed and wasn't quite what we had envisioned for the application.   Even though I wasn't completely thrilled with our final product I can say that I had so much fun at this event that it didn't matter.  I also learned a lot of things that I'll put in practice at the next event because I definitely plan on doing this again.  After a little reflection this is what I learned for the next hack-a-thon:

Have all software installed before the event.
First and foremost make sure that every team member has all the software needed before the event, time is of the utmost importance and you don't want to waste it downloading and installing software.  

Don't be over ambitions with your application.
Just as important as being prepared make sure that you have an idea in mind that can be executed in such a short time frame.  The application you develop for an event like this should be extremely simple, I'd say no more than 2-3 screens max with an equally simple data model.  

While we felt that the application was simple enough before hand, we quickly realized that this application would need 4 or 5 distinct areas, each with its own set of screens and database tables.  This is far too many when you are trying to deliver a complete application in less than 8 hours.

A hack-a-thon is not a time to teach and learn new technologies
While my goal going into this even was to show people how great ColdFusion, ORM, and FW/1 were there just isn't enough time to teach, learn, and execute.  While I did succeed in wowing my team members with the capabilities of ORM and FW/1 too much valuable time was spent teaching and learning when we needed to be executing. 


Break free of your "enterprise" or "corporate" practices. 
When I first sat down to start coding I envisioned writing the elegant API with a very clean and nice separation of concerns in the MVC layer. I think at one point I even was going to thrown in a unit test or two???  What was I thinking??  This is a HACK-a-thon, hack the thing together and make the code pretty later.  Heck it ended up a mess anyway, I might as well have just started out throwing code together saved the time putting together a nice folder structure and object structure.  Since there is no shame in my game if you want to see what we pumped out in 8 hours the code is all up at github here: https://github.com/bittersweetryan/Waternoose.

While I wish our team could have represented ColdFusion a bit better I came away learning some valuable lessons that I will apply in the next event.  On top of all of that I also got a $10 iTunes gift card, the Xbox 360 game "Allen Wake", and a "Make Web not War" t-shirt.  All-in-all not too shabby for a Saturday.  I can't wait for the next one!

Wednesday, February 9, 2011

Using Metadata To Add Static Variables to ColdFusion Components

Let me start out by saying that everything in this this post I learned from Elliot Sprehn's fantastic CFMeetup presentation "I bet you didn't know you could do that with ColdFusion".  If you haven't already seen this presentation, you should.  Even if you have seen it you should watch it again, and show it to anyone you know who loves to bash ColdFusion.   Anyways, I wanted to play around with this concept on my own to see how I could get it to work for me.  

First, if you are unfamiliar with the concept of metadata in ColdFusion read "Using introspection to get  metadata for components" at adobe.com on for some background.  Also should check out the getMetaData and getComponentMetaData functions over at CFQuickDocs.  The most important thing to know for this post is that these functions return a structure that describes an object.  These properties are generated the first time an object is instantiated and will persists until the object is recompiled by the ColdFusion server.

So what exactly am I talking about when it comes to adding static variables to ColdFusion components?  Well if anybody has worked with a strongly typed language such as .NET or Java you'll be familiar with the static keyword.  Static variables are variables that are variables that exist in only one place and are accessible without having to instantiate a instance of a object.  Basically you can access a property on an object by calling ObjectName.staticVar without having to create a new ObjectName().

Lets take a look at a bare bones component called Employee:

<cfscript>
component name="Employee"
{
 public Employee function Init(){
  var metadata = getComponentMetaData("Employee"); 
  
  if(!structKeyExists(metadata,"myStaticVar")){
   
   lock name="metadata.myStaticVar" timeout="10"{
    metadata.myStaticVar = "Hello Static Variable."; 
   }
  }
  
  return this;
 }
}

As you can see when this component's Init() method is called I get the components metadata and look for a key called "myStaticVar" and if it doesn't exist I create it.  Note: it's important to lock the creation of this key since you are manipulating a shared scope.  Its important to note here that until this component is compiled by ColdFusion this key does not exist.  Dumping the output of getComponentMetaData("Employee")at this point will produce the following output:






















As you can see there is no key for "myStatcVar" which was defined in the init() method of the component.   So let's create an instance of the component and dump its metadata.

<cfscript>
Employee = new Employee();
writeDump(getComponentMetaData("Employee");
</cfscript>
 






















Now you can see that my  the component metadata structure has a key "MYSTATICVAR".  The real beauty of this is that this metadata persists server wide for this component until it is recompiled by the ColdFusion server.   I tested this by creating  a new directory with a new application.cfc and with a new application name and dumped the metadata on the Employee component as shown below:

<cfscript>
writeDump(getComponentMetaData("Employee");
</cfscript>

Sure enough, the MYSTATICVAR key was there.  I even created a completely new website and mapped the component directory to the directory that holds my Employee component and ran the code above again and the metadata was there.

Now anytime I want to access the value of my static variable all I have to do is write code like this to retrieve the value of the static variable:

<cfscript>
 staticVariableValue = getComponentMetaData("Employee").myStaticVar;
 
 writedump(staticVariableValue);
</cfscript>

It's important to note that when the server is restarted this metadata will once again disappear until the component is created for the first time.

Monday, January 31, 2011

Getting the Generated Key From A Query in ColdFusion (Including Script Based Queries)

Are you are still writing things like SELECT SCOPE_IDENTITY()  or LAST_INSERT_ID() in your ColdFusion Queries?  One of the more powerful and perhaps lesser known features of the cfquery tag is the ability to return the generated ID from an insert statement.

Prior to ColdFusion 9 the value returned in the query struct would change depending on the type of database the value was being inserted into (see cfquickdocs for more information).   For Instance Oracle would return a key called RowID  and SQL Server would return a key called identitycol in the result structure of a query (provided you supplied a result argument in the query itself).

<cfquery name="insertMe" datasource="mysql_db" result="queryResult">
 INSERT INTO
   Contact(firstname, lastname) 
 VALUES('ryan','anklam');
</cfquery>

In ColdFusion 9 Adobe simplified this by always returning a value called generatedkey in the result structure as shown below:



 






Things get a bit more interesting when leveraging ColdFusion's new script based queries.  Lets use the following code as an example:

<cfscript>
 insertQuery = new query();
 
 insertQuery.SetDatasource("facets_sql");
 
 insertQuery.SetSql("insert into contact(firstname, lastname) values('ryan','anklam')");
 
 result = insertQuery.Execute();
</cfscript>

One might expect that generated key would be a property that could be accessed through the result variable.  However, looking at the dump of the result variable we can see that ColdFusion actually wraps all the result goodness in a property called prefix:














So, in order to get the generated key from a script based query the following code is required:

<cfscript>
theKey = result.getPrefix().generatedkey;
</cfscript>

Wednesday, January 26, 2011

The Books / Classes I Wish to complete in 2011

Since I've purchased my nook color last year I've been addicted to reading tech-ebooks and with that has come the motivitation to learn everything that I can, including webinars and online classes. 

Tuesday, January 25, 2011

Book Review: Sitepoint's Simply JavaScript by Kevin Yank & Cameron Adams

My life has finally slowed down over the last few weeks which has allowed me to start to tackle my huge list of books to read.  The first book I decided to read was SitePoint's Simply JavaScript written by Kevin Yank and Cameron Adams.  This was also the first book I read on my new Nook Color (I previously had a e-paper Nook) and I have to say that I waas very impressed with the quality of SitePoint's ePub format.  The code was fairly easy to read in within the constraints of the ePub format and the illustrations generally looked really good.   The splashes of color were also a nice touch when reading, its amazing much a subtle detail like a blue line can make your reading experience a little more enjoyable.  

So why did I choose this book to read first from the list of about 15 books I want to read?  I've been a software developer for over 10 years but I never really learned the basics of JavaScript.  I've spent a good part of  my career behind a firewall where I only had to target my company's standard browser so I was always able to hack away a solution that worked.  Once I moved on to a company that had more public facing sites I picked up jQuery and pretty much learned how to write jQuery code and not worry too much about the JavaScript behind what I was writing.  This always somewhat bothered me and when Site Point (http://www.sitepoint.com) offered this book as one of their 25 days of Christmas deals this year I jumped on it.

I immediately felt comfortable with the way the authors spoke to me in this book.  Some technical books are so dry they could suck the mositure out of the desert air, however this book has a plesent delivery that made reading it that much more easy.  Another caviat I have about a lot of technical books is that they typically spend too much time delving into a topic and bore me into glazing over the pages and not really absorbing what the author is trying to convey.  Kevin and Cameron do a great job of pacing the concepts they cover in this book.

The first chapter is all about one of the fundamential building blocks of JavaScript in the browser: the DOM.  They start by explaining the concepts of the DOM and some of its idiosyncrasies. A good example of this is their explanation of how whitespace can actually create new text nodes in the DOM structure. The next chapter dives into the event model, and goes quite in-depth about all of the cross browser issues that developers used to complain about before frameworks became popular and took care of most of these issues automatically.  After events are covered the authors then delve into the world of animation, another thing that the popular libraries do a great job of abstracting out for modern JavaScript developers.  This book did a very good job of explaining a lot of the details qne techniques behind animation, again providing some very good examples along the way.  After animation is covered the authors build upon the lessons learned in previous chapters and introduce the conepts of form enhancement using JavaScript.   In this chapter the authors will walk the reader through creating some common form enhancements, which after you'll begin to really appreciate how much work goes into plugin development for your favorite JS library.  Next the authors walk you through the black art of debugging JavaScript code and finish up with a brief introduction to Ajax.

What I really liked about this book is that the sample code in this book is easy to follow and really helps strengthen the concepts that are discussed throughout. The code builds upon the concpepts that are introduced throughout the book and by the end you feel very comfortable using the techniques described througout.   Kevin and Cameron also do a great job of breaking down the code after it is presented and give an in-depth description important steps throughout the code.  Other important JavaScript concepts are sprinkled throughout text as well, for example closures and object literal syntax are both covered  in this book.  In addition basics like variable scoping, array creation, looping concepts and conditional statments are also introduced and explained in this book.  One of the biggest compliments I can give this book is that after reading it I actually understood the concept of closures, something I had been trying to grasp for sometime.

Since this book is aimed at a beginner to JavaScript I feel like it could have done a better job of explaining some of the built in functions of the JavaScript language and perhaps touched some more on a few of the shorthand methods that one will see in many scripts such as ternary operators.  

I'd definately reccomend this book to anyone that wants to learn a little bit more about JavaScript and whats behind the magic of some of today's popular JavaScript frameworks.  An advanced JavaScript developer will probabaly find this book a boring, but thats not the intended audience for this book anyway.
Fork me on GitHub