Wednesday, August 10, 2011

Learn JavaScript completely On the Cloud With the JavaScript Koans and Cloud9 IDE

Lately I've been investing a lot of time into learning JavaScript and one of the neatest things I've come across is the JavaScript Koans project.  If you haven't heard of Koans before the basic concept is to learn a programming language through the act completing unit tests in that language.   The most popular implementation of this concept is the Ruby Koans project (http://rubykoans.com/).  Liam McLennan took this concept and ported it over to JavaScript and his implementation can be found over on github (https://github.com/liammclennan/JavaScript-Koans).

When you pair this repository with the Cloud9 IDE you can work on them completely on the cloud without having to download a single piece of code locally.  Cloud9 (http://cloud9ide.com/) is an completely online and very richly featured IDE.  One of the great things about Cloud 9 is that it can access your github repositories and clone them into its environment for you to edit.  Cloud9 also provides you with a interactive console with git installed so you can push the changes back into your repository. The console will even give you intellisence on the git commands as shown to the right!

Assuming you already have a github account the first step is to fork a copy of the JavaScript Koans repository.  Once you have a forked copy of the Koans head over to the Cloud9 website and log into your dashboard.  Your dashboard will have a "Add On Services" section that will allow you to connect Cloud9 with your github account and pull in your public repositories.  One thing I've noticed is that if you've added a new repository since you last connected with github you'll have to connect to github again for Cloud9 to pull in the newly created repository.   Once the repository is pulled into Cloud9 next you'll have to select the project from the github menu and click on the "Clone to Edit" in order to be able to edit the files.

Once your github repository is cloned it will show up as a new project in the Projects area.  First click on the Koans project and click on the "Start Editing" button which will open up the project in a new tab.   Once your koans project is open double click on the "jskoans.htm" file to open it up in the editor.  The editor will detect that this is a HTML file and a "preview" icon will appear in the editor.  Click on this button to run the Koans.  When you do the jskoans.htm file will open in a new tab and run the koans unit tests, which will fail on the first test like so:



Whats important to note here is which file the error occurred on. The Koans are organized by topics, and each topic will have its own JavaScript file with qUnit tests that pertain to that topic.  Find that file that caused the error in the editor and open it.  You'll now see all the qUnit tests for that file.  Below you'll see the code from the first topic which is a brief overview of assertions.


Inside the assertion code there will always be two underscores "__" that denote an area where you are to fill in a value to make the test pass.  As you go through the exersizes save the unit test file and refresh the jskoans.htm preview page and you'll start to fill the page with passed tests.  The test runner will stop at the first failed test it finds. As more and more of the tests pass the page will begin to fill up with passed tests as shown in the image to the right.

Whats great about the koans is that its an open source project so community members can add more tests and actively improve the tests that are there, which are very good right now.  Some of the topics covered are: arrays, reflection, prototypical inheritance, objects, and functions and closures. Going through these you'll get to learn JavaScript the best way, by writing JavaScript.




Monday, August 8, 2011

The ColdFusion Geeks Fantasy Football League Needs Two More Teams!

This is the second year of the ColdFusion Geeks league (http://football.fantasysports.yahoo.com/f1/256516), and I've decided to expand it to 12 teams instead of 10.  I'm looking to fill those two spots with ColdFusion lovers who also love to play fantasy football. The winner is promised everlasting glory and lifelong bragging rights within the community.  

This is a free league all that I ask is you play till the end, even if you are 0 and 12 in week 13.  If you are interested you can send me a message on twitter or use the contact form of this blog.

Monday, August 1, 2011

How I Got Started In ColdFusion

The year was 1997.  Puff Daddy's "I'll Be Missing You" and Hanson's "Mmm Bop" ruled the airwaves and I was a skinny 19 year old stuck in an Architectural Technician program that I had zero passion for.  One day while sitting in the Milwaukee Area Technical College computer lab researching the Empire State Building I inadvertently right clicked on a web page trying to copy some text and noticed this option called "View Source" in the menu.  Being the inquisitive type that I am I decided to click on it to see what it did.  Notepad opened up and with it opened a whole new world to me.

At first I wasn't sure what all of these and
things meant but I was able to quickly develop a hypothesis in my head.  Within minutes I created a geocities account and was busy writing "Ryan's Awesome Car Picture Page" which featured a bunch of pictures of cars I found browsing the net.  Tables with a border size of 10 were a common site on this page.  My mind quickly jumped to the prospect of writing my Empire State Building paper as a website.  Architecture had become fun again, or at least writing a webpage about architecture was.  The page featured a repeating green marble background, white text, and some images of greek-style columns, was littered with markup errors, but was I ever proud of it.  I couldn't believe how much fun I had creating it either.  Grown up I was always interested in computers, but the only thing I had ever done with them was load games off a floppy disk and occasionally browse Prodigy.

I'll never forget the look on my professors face when I handed in my paper that had just one line on it: http://www.geocities.com/.... The very next day he pulled me into his office and told me that one of his former students was starting a company creating websites and that I should get in contact with her.  I'm a huge procrastinator, but this was one opportunity that I immediately jumped on.  The very next day I had an interview scheduled.  Apparently that interview went well because a week later I was busy scanning images out of an Oldsmobile car brochure for a car dealership website.

Fast forward a few months and a few static websites later.  The company I was working for, IDL Solutions, had been maintaining the Wisconsin Lottery website.  This was by far their most important client and they weren't about to let some greenhorn loose on it, however the owners were leaving town on vacation and I was called upon to enter the winning numbers for the lottery that night.  Tony, the companies co-owner and author of the lottery website sat me down and began to walk me through the process of updating numbers.  I immediately noticed that the pages he was showing me all ended in .cfm instead of the .html or .html links I was used to.  When I asked him about it he told me of this magical product by a company called Allaire called ColdFusion.  He continued to tell me how easy it made creating database driven websites and showed me some of the source code behind the lottery website.  I couldn't believe what I was seeing.  Up until this point all I had ever heard of was this language called CGI to create dynamic web pages.  CGI was way above my head at the time so ColdFusion seemed like one of the greatest things ever to me.  

It wasn't long before I had the "Ryan's World Tour" webpage up.  It was basically a very early implementation of a blog.  Every time I'd go out and party I'd write up a short story of the nights shenanigans and post it on the website and allowed my friends to comment on the stories.  I couldn't believe how much fun I was writing this website, I was hooked.   After the semester was over I stopped taking Architecture classes and switched all of my classes to computer classes.  For the next 4 years I worked with exclusively with ColdFusion doing pretty basic stuff: event sign up forms, parsing CSV files, and creating job websites...pretty much the same thing everybody was doing in the late 90's.

It wasn't until 2003 and the Macromedia MAX event in Utah that my eyes were opened to a whole new programming paradigm and I started taking my development seriously.  ColdFusion MX 6 had just come out and a lot of the ColdFusion sessions were on this new construct in the language called ColdFusion Components.  A particular session by Simon Horwith changed the way I looked at writing applications forever.  He showed us how, using CFC's, ColdFusion could be written using an object-oriented approach.  This was the first time I felt like I could become a "legitimate" programmer doing ColdFusion.  From that day on I haven't stopped trying to learn how to craft my applications better.

This is the point where I can say that I REALLY got started working with ColdFusion.  I stopped writing procedural spaghetti code and started to write reusable maintainable code.  I also started challenging myself to learn new things.  The more I learned the more I realized that I didn't know as much as I thought I knew.
To this day, that pattern continues.  Everyday I learn something new which makes me realized exactly how much I don't know.  This is a message that I try to pass on to ColdFusion developers that I talk to that seem to be in a rut.   It's so easy to be satisfied with what you know and keep the status quo.  It's easy to write code the same way you've always written code.  Its easy to follow the developers before you and do exactly what they did. In contrast it's hard to always feel like you have a mountain of things to learn.  It's hard to look at the code you wrote two months ago and want to rewrite every single line because you can't believe you were so stupid back then.

This is also what makes being a programmer so dam fun.  I'm never bored and I love the fact that there's always something new and exciting to learn.  ColdFusion started me down this path and for that I'll always remain a loyal ColdFusion programmer.  Of course I'll continue learn new languages along the way but when the rubber meets the road and I know I have to get a project done, ColdFusion is always the language I'll fall back to because I know it can do the job and do it well.

Monday, July 25, 2011

Technical Podcasts That'll Make You A Better Developer

I have a rather lengthy drive to work each day (~1 hour each way) while listening to the nonsensical ramblings of my local sports radio one day it hit me like a ton of bricks: why was I listening to this dribble when I could be spending this time making myself a better developer.  Thus began my search for some good technical podcasts to fill up my iPhone with.

I'm primarily a ColdFusion/Java developer and you'll see that none of these focus on either of those.  I'm a big believer that no matter what language you primarily use there are tons of things to learn from other languages.   I've used many things I've learned from listening to these in my day to day development.

While this list is far from complete its based on what I've discovered so far.  If you have a favorite podcast that you think I missed please comment and let me know.  I'm always looking for more good ones to listen to.

Software Engineering Radio (http://www.se-radio.net/)
I don't recommend listening to this one before you've had your coffee.  This podcast can be quite dry at times but it is packed with excellent technical information.  If you can get past the sometimes monotone delivery there is a lot to be learned from every episode.  The topics covered here cover an extremely wide range of topics, I'd highly suggest looking through all their back episodes for topics that may be interesting to you.

Herding Code (http://herdingcode.com/)
Herding code is a .NET focused podcast that contains mostly interviews with other developers.  The guests on Herding Code are usually top notch, and I frequently find myself going to the show notes on the website and researching a lot of the topics that come up.  The hosts are also extremely knowledgeable and usually ask their guests great thought provoking questions.  The topics discussed cover both open source and Microsoft specific topics which is a bonus in my book.

Pragmatic Podcast (http://pragprog.com/podcasts)
This podcast is another interview style podcast from the publishers of the Pragmatic Bookshelf books.  This podcast is a great change of pace from the strictly programming related podcasts I usually listen to.  Most of the time the interviews are for development specific topics, but I find the real gems are the ones that don't focus on development.  For instance, Andy Hunt's chat on Pragmatic Wetwear (http://pragprog.com/podcasts/show/13) or Chad Fowler's chat on "The Passionate Programmer (http://pragprog.com/podcasts/show/26) were some of the best overall podcasts I've listened to.

Polymorphic Podcast (http://polymorphicpodcast.com/)
The Polymorphic Podcast is another .NET focused podcast.  The host, Craig, does an excellent job of talking about things at an architectural level that will benefit developers from any language.  This podcast also does a good job at providing its listeners with enough high level programming best practices and techniques that its worth listening to even if you are not a .NET developer.

On Software (http://www.informit.com/podcasts/channel.aspx?c=dadf92ca-3bdc-484e-9cd8-cbfe0cfc0de6)
This is a short video podcast that covers a very wide variety of topics.   From JavaScript to Rails to Ubuntu this podcast covers it all.  The episodes are all on the shortish side ~10 minutes or so and since they are video podcasts I don't recommend listening to them on your commute, but they are great for watching while you are eating your lunch.

Pluralcast formarly Elegant Code (http://www.pluralsight-training.net/microsoft/pluralcast/default.aspx)
Admittedly I haven't listened to nearly as many episodes of this one as the others, however, the ones I've listened to have had excellent technical content.  Again, its another .NET podcast but David does an excellent job in breaking down the technical aspects of what he's talking about and seems very detail oriented which helps when explaining something in an audio format.

Railscasts (http://railscasts.com/)
This Ruby-On-Rails specific videocast may be unmatched in its technical content. Like the prior sentence implies, this is another video cast that is a great watch during your lunch hour.   Even if you never have any intention to write a line of Ruby you'll be hard pressed not to take some of the ideas Ryan presents and try to implement them in your day to day development.  Ryan also does an excellent job preparing for his shows and the delivery of the content is second to none on this list.

The Changelog Show (http://thechangelog.com/)
This is the latest podcast I've started to listen to.  Wynn Netherland and Adam Stacoviak host this weekly show that focuses on Open Source projects and culture.  Each week features an Open Source developer talking about their project and the open source landscape.  This podcast does have a pretty Ruby-ish feel to it as both of the hosts are Ruby developers and most of the interviewees (at least through the episodes I've listened to but I still have about 12 to go before I'm current) have a Ruby background.  Regardless I find this show informative and entertaining and I almost always find myself watching at least one project or developer on GitHub after the show.

News Based Podcasts
While learning new things is always one of the more important things a developer can do its also important to stay up on the current news and trends.  While the following podcasts don't give you the amount of technical information as the ones I mentioned above these focus on keeping you up to date on their platforms of focus.  While I downloaded every episode of the others the ones below I usually only download the latest shows so I can hear whats new and exciting.

The Java Possee (http://javaposse.com/) does a great job at keeping me up to date on what the Java community is doing (this podcast does also have a fair share of great technical information but It's just not on the level of the others). The JavaScript show (http://javascriptshow.com/) is a newer show which is brought to you by the same hosts as The Ruby Show (http://rubyshow.com/) and is formatted the exact same way where the hosts just go over a list of technology specific news and projects.  Both of these do a superb job in giving you the latest and greatest news as well as point out projects that are doing exciting things.  These shows are nice because they give you their content in a direct and concise format and leave it up to you to get further details.

For jQuery you can't go wrong with The Official jQuery Podcast (http://podcast.jquery.com/).  I also wish I could speak intelligently on the yayQuery Podcast (http://yayquery.com/) becuase it seems like it will be full of excellent information as well. I do have episodes 0 - 22 queued up in my play list bug I just haven't gotten around to listening to them yet.  Once I do I'll update this entry with more information.

While this list doesn't cover all of the podcasts I listen to regularly, it does cover the ones I tend to glean the most information from.  Like I mentioned earlier, if there are others you know of that you think deserve mention in this post please leave me a comment and I'll give them a listen, and I'll try to keep this post up to date as I discover more good podcasts.

Monday, July 18, 2011

Mocking Java Objects In ColdFusion Unit Tests

I've been working on a project lately that has a few CFC's that rely heavily on a java object for much of their functionality.  This was making my unit tests act more like functional tests since I was using the return values from the Java object to make my assertions.  This didn't feel very good to me so I went to StackOverflow for some help. Edward Smith (Edward if you are reading this and would like me to link to something other than your stack overflow profile send me a comment and I'll update it) knocked it out of the park with his answer.  I used what he said to implement the solution that this post is based off of.   He mentioned that his company had used the Java mocking framework Mockito to mock Java objects in their ColdFusion unit tests.  Brilliant!  I already use Mockito for all of my Java unit tests, I just had never put it together that I could actually use it in my ColdFusion unit tests as well.

The first step in implementing this approach is to download the mockito jar file from http://www.mockito.org.  For my implementation I'm using Mark Mandel's excellent JavaLoader project to create instances of java objects so I copy the jar into a lib folder in my application's root folder.  Another approach would be to add the .jar file right to my ColdFusion server's instance, however I don't have any experience with this approach so I stuck with what I knew.  This article on the Adobe blogs by Christian Cantrell might help you out if you'd rather use this approach.

Now that I have the .jar file in my project its time to make them available to my mxunit test. To do this I need to make them available to my test by loading them using JavaLoader and then to create an instance of the Mockto object so I can start mocking the Java objects.  Its important to note that when using the JavaLoader approach you must also inititalize the .jar file of the object you mocking and ALL of the dependencies.  Once the JavaLoader object is initialized with all of .jar files our objects need we need to create an instance of the mockito class for our test to use:


The next step is to create our mock Java objects.  Since the component I'm trying to test is more or less a ColdFusion implementation of the java object I decided to recreate the mock before every test so I knew I was working with a clean copy of the mock.  This means that I placed my mock code in my unit test's "setUp" method like so:


The syntax for creating a mock object in Mockito is pretty simple:  variables.mockito.mock(variables.classLoader.create("com.sudios714.cfevernote.CFEvernote").init("123","S1","232","sandbox.evernote.com","mock").getClass()); Here I am calling the "mock" method on my Mockito object.  The first parameter to the mock method is a Java class.  In order to get a class I'm  using the JavaLoader object's create method to create a new instance of a Java class and calling the getClass() method on that class to get the actual Java class to pass into Mockito.  The last step is to inject it into my component. Now we have a working mock object injected into my component lets take a look at a test that puts my mock to use:


There are a few important things to note here.  First is that the data that I'm creating for my mock is using a native Java object, in this case java.util.ArrayList.  Since this is a part of the core Java language I don't need to use the JavaLoader to create an object for this data type.   The next step is to do the actual mocking behavior.  The statement variables.mockito.when(mockCFEvernote.listNotebooks(12)).thenReturn(retArray); is telling Mockito WHEN the listNotebooks method is called with a parameter of 12 to RETURN retArray which is the java.util.ArrayList object we created above.

Mockito can also be used to verify behavior, however this comes with a large caveat.  Even if the verify statement is surrounded a try/catch statement (at least in my Win64/IIS7/CF9/Java 6 environment) if the verify statement fails, meaning that the method was not called with the expected parameter, the server will throw a 500 error.  If the behavior verifies OK the test will run as expected.  Knowing this if you'd still like to verify some behavior it can be accomplished by writing a verify statement like so: variables.mockito.verify(mockCFEvernote).listNotebooks(12); Here we are telling Mockito to verify that in the mockEvernote object the listNotebooks() method was called with a parameter of 12.

Fork me on GitHub