Search

Subscribe

Enter your email address to subscribe to this blog.

Recent Comments

Groovy's null safe operator
diana said: Thanks for the cold fusion example..... [More]

Groovy's null safe operator
avi said: thanks danvega for the information.... [More]

Sneakerhead Git Branches
James Brown said: The successful branching model you referenced can be used even if here is only one developer on a pr... [More]

Learning Groovy & Grails at Harvard
Sam Farmer said: Signed up and ready to go! [More]

Groovy & Grails - List of Learning Resources
Eric Pierce said: Thanks for this, Dan! [More]

Groovy collections vs My Current Thought Process

I have been a ColdFusion developer since before I can remember. What I have always loved about the language is that it made hard things really easy to do. Somewhere along the way though I think it lost some of this magic at the core of the language. Let's take a look at a pretty trivial problem (but one that might come up often) and look at how we can solve it in both ColdFusion and Groovy.

Given a collection of names : print each name to screen and display a comma in between each name. Make sure to ommit the comma after the last name in the collection.
This doesn't seem like that hard of a problem, lets take a stab at it. First we create an array of names. In our first attempt we decide to use a for in loop and loop over each name in the collection. During each iteration we print the name and a comma. The main glaring problem with this approach is that we are going to print a comma after the last element and that won't solve the problem at hand. So we move on to our next attempt. We decide to use a for loop so that we can have the index of item that we are printing out. We will print out the name and a comma except for the last item where will not print out the comma. While this will work it isn't a very clean solution. All this work to solve a pretty simple problem. Finally we do some looking around and remember these a special function in the language to accomplish this. ArrayToList will actually take an array and create a list with a delimiter. I could of actually left out the comma here as it is the default but I just wanted to make sure it was clear what was going on. So I asked another dev to look at the same problem and he pretty much took the same thought process I did. I don't know about you but I feel like I write more loops in ColdFusion than anything so its alway my first instinct. Unless you know ever function the language has to offer the solution may not of been apparent to you right away. Be honest, did arrayToList cross your mind before you got to the solution?

Now let's look at the Groovy solution. I am pretty new to the language but one thing I have noticed is that working with collections is a joy to do. First lets look at the solution and then we will talk through our thought process. The join method will "join" each element in the list and separate them with the delimiter you provide. So what is the difference. The difference here is the join() is a first class member function. This means that I can call it directly on the collection itself. I don't need to start thinking about what functions are available to me and what the arguments are (and what order the come in) I just know that I can call a method to accomplish this. I know that if I want to iterate over the collection of data I can call

names.each()
names.eachWithIndex()

I know that if I am looking for something within the collection I can call

names.find()
names.findAll()

I know that if I need the size of the collection I can call

names.size()

Finally what about a scenario where you want to create a new array that holds the string length of each of our names. Our first thought again is we need to loop over the current collection and create a new one. To do so we need to initialize an empty array, loop over each name and then add a new element using a function that takes the array name and value. In Groovy we again know that the collection has a 1st class function to do this using collect. This will return a new collection by manipulating the existing collection. This to me is a very clean and elegant solution.

I hope nobody gets the wrong impression about ColdFusion here because many of these same approaches are the same in many languages. I just wanted to point out how much I enjoy working with collections in Groovy. They make me think a different way when dealing with data and I am finding myself writing a lot more boring iteration loops these days.

 
 

Groovy's null safe operator

The more years I get under my belt as a programmer the more I appreciate the smaller things a language has to offer. Today I want to look at Groovy's null safe operator. When you want to crawl an object graph you always have to be worried about the dreaded null pointer exception. First I want to take a look at this example in ColdFusion. In this example we are looking at the body of a method where we load up a user object. We want to grab the city of the user object but in each step of the object graph we need to make sure that call is not null before preceding.

In Groovy we can do this shorthand by using the null safe operator (?.). If the variable before the question mark is null it will not proceed and actually returns null for you. We could even shorten this statement more but you get the idea.

The Groovy language has so many little things like this that I am really starting to love. What are some of your favorite features in the language?

 
 

Bootstrap Breadcrumbs for Mura

I am working on a friends site that uses Mura & Slatwall. I needed a way to display some breadcrumbs for the products area. Luckily Mura comes with a built in way to do this.

This uses a method in the default content renderer. This works great but I actually want to use Bootstrap to style it. To do so I need a little bit different markup than what the default content renederer provides. Luckily Mura allows us to override this method in our our own content renderer. Simply open {site}/includes/contentRenderer.cfc and add your own dspCrumbListLinks method. I just copied the method from the default and made a few changes. This will override it for all themes, of course if you need it only for a specific theme just add it to the content renderer inside the themes folder.

I am having some issues with word wrap on that code so I decided to attach a text file with the function inside. I will update this post when I fix that. Click the download attachments link in the article entry bar below.

 
 

I am an Adobe Community Professional, again!

I forgot to share the great news but I received the following email about a week ago.

Hello, I am pleased to announce that your Adobe Community Professional application has been accepted. Out of all the applicants, you exhibited the qualities that would make you an excellent community member. Congratulations! It was an extremely hard decision making process this year and many applicants were exemplary. We were not able to accept as many members as in previous years but we feel that with a smaller group we will be able to refocus the program and give you all the attention you deserve.
It really is a great honor to be selected as an ACP for a 3rd year in a row. Thank You Adobe!

 
 

Removing duplicates from an array of objects

I have a style of programming that generally works well for me. I like to solve a problem and then step back and evaluate my solution. This gives me a chance to see if there is a better way to solve the problem at hand. In the programming world we gave this a fancy name, refactoring. I am also very lucky to have some really smart people at work so when I have the chance to discuss code I take it.

Last week I was working on a problem that I have solved countless times before. I had an array of objects and I wanted to make sure no duplicates of a certain key were returned to the caller. Before I write some code I like to talk the solution out so in plain English this is what I need to do.

  • create an empty array to hold our new array
  • loop over our existing array of objects
  • if the current key doesn't exist in our new array, add it

Seems pretty simple and I am betting a lot of people have taken this approach before. So what does this look in code? First we have our example array of objects. In this array class code is the key we don't want to duplicate. Next we create an empty array that will contain our duplicate free result. Next we are going to loop over our array. During each iteration we loop through our new array and check to see if that value exists, if it does not go ahead and add this object to the new array.

While this works great I was talking with a coworker (Jason Delmore) and he came up with a pretty cool alternative. Instead of creating a new array to hold our result we can simply look ahead to see if this key exists and if it does remove it and decrement the loop index.

We can also do the same thing in JavaScript.

This may seem like a small change or there may be better ways to solve this (I would love to hear them) but I love it. I enjoy looking at problems like this and understanding ways to solve them (in English) and then taking a look at how we can implement them.

 
 

More Entries