Save yourself from Brittle Strings!

Over the last week, I completed a training course on Enterprise Java Beans (EJB) and the Java Persistence API (JPA). We are hoping to utilize this technology at work, and my employer offered myself and fellow co-workers the opportunity to participate in a week long class that reviews the technology from top to bottom. The class was really interesting, and I am very excited to utilize this technology in development. Our instructor was from an outside company called Webucator, and presented the material very well.

EJB and JPA make many of the necessities of real-time enterprise application development – such as database access, transaction processing, or job scheduling – much easier. Many of the concepts in EJB and JPA require annotations on the code to accomplish tasks. If you’ve never seen an annotation before in Java, a simple example is shown below. The @Override annotation indicates that the method is overriding an implementation in a base class that is being extended:

@Override
public void foo()
{
	//Code removed
}

Many annotations happen to also take parameters to determine the behavior. One such parameter is JPA’s @NamedQuery annotation. This annotation allows us to create an object query using the Java Persistence Query Language (JPQL) and name it. Take a look at one example below, which finds MusicItem objects based on a keyword parameter:

@NamedQuery(name = "MusicItem.findByKeyword", query = "SELECT i "
		+ "FROM MusicItem i "
		+ "WHERE i.title LIKE :keyword "
                + "OR i.artist LIKE :keyword")

Now this statement looks all well and good at first glance. The syntax is correct, and the query is ready to be utilized by an application developer. However, there is something very dangerous!

Put yourself in the shoes of the engineer utilizing this query. You write a line of code like the following in your bean implementation class:

//The variable em is our EntityManager
em.createNamedQuery("MusicItem.findByKeyWord", MusicItem.class)
.setParameter("keyword", wildCardedKeyword).getResultList();

You deploy your newly coded bean to your application server, try to run it, and it blows up! Why is this? Take a close look at that query above again. The first parameter in EntityManager.createNamedQuery() is the name of the query. Notice that it isn’t exactly the same as what we put in our @NamedQuery annotation? In our annotation defining the query, we named it "MusicItem.findByKeyword", but in our bean class, we called it using "MusicItem.findByKeyWord". The case was not the same. This is an error that is tough to track down as it wouldn’t be exposed until the code is actually used. How can we avoid these kinds of problems?

Throughout the week, our instructor in the class kept referring to strings as very “brittle,” and that we need to be very careful when relying on them during development. This is something I never thought about until I got to my job working at Dematic – in all of my previous experiences (academic and professional), using strings was never discouraged. There is a solution to this issue – define your string in a constants class.

In our workplace, we have taken to developing constants classes for our projects. This is a very simple class that contains only public static final values to be utilized elsewhere in the project. Let’s consider the example of the @NamedQuery again, however this time we’ve also put a class called Constants in our project that looks as follows:

package com.prj;

public class Constants { public static final String QUERY_FIND_BY_KEYWORD_NAME= "MusicItem.findByKeyword"; public static final String QUERY_FIND_BY_KEYWORD_CONTENTS= "SELECT i " + "FROM MusicItem i " + "WHERE i.title LIKE :keyword " + "OR i.artist LIKE :keyword"; }

We’ve now defined two constants in our project, QUERY_FIND_BY_KEYWORD_NAME, which is the name of the query, and QUERY_FIND_BY_KEYWORD_CONTENTS, which is the query itself. Let’s try creating our @NamedQuery annotation again, this time with these constants:

//Assume all imports are done correctly
@NamedQuery(name = Constants.QUERY_FIND_BY_KEYWORD_NAME, 
	    query = Constants.QUERY_FIND_BY_KEYWORD_CONTENTS)

Doesn’t that look much cleaner? And now for our call to the EntityManager.createNamedQuery() method in our bean implementation:

em.createNamedQuery(Constants.QUERY_FIND_BY_KEYWORD_NAME,
MusicItem.class).setParameter("keyword", wildCardedKeyword)
.getResultList();

Now, we are guaranteed to avoid the runtime error we faced earlier! With a hard-coded string in a constants file, our engineer utilizing the query can’t possibly mistype the name of the query.

This is just one of the many benefits of utilizing hard-coded strings in your development practice. There are many other benefits as well. Constants are easy to change/adjust if needed, the variable names are easy to refactor, and they are easy to search for all instances of when using an IDE. If you aren’t using hard-coded constants in a central location as part of your development practice, I would highly recommend starting. It will make your life much easier and save you hours of time in the long run.

Mentoring Rocks

A few weeks back, I arrived at the office and started my day by reading my email, as I usually do. The newest message in my inbox was from my boss, and was two sentences long.

Come to my office as soon as you get in. If the door is closed, come in anyways.

I had a brief “uh-oh” moment. I thought that I possibly coded something wrong on a recent project, and maybe I was going to be scolded for my rookie mistake!

Upon walking into my bosses office, I found out that I had not made a warehouse crash and burn (phew!). He informed me that I would have a local high school student shadowing me for the day. His name was Andrew, he was a Senior, and was interested in pursuing a career in engineering. His dad was friends with an employee at my company, who offered him the opportunity to come visit.

Lucky for Andrew, I wasn’t going to be sitting at my desk writing code all day. That definitely would have been the most boring six hours of his life if he just watched me type away. I had plans to test with our conveyor and MultiShuttle teams for an upcoming project – something that would produce very visible, hands on results that could easily be understood by someone outside of my field.

I was fortunate to be able to spend a good deal of time conversing with him about what he likes to do, subjects he enjoys in school, and plans for college. It was fun to be able to look at someone with their entire future ahead of them and still be able to relate, knowing that I was in his shoes not too long ago. Andrew said to me that very interested in math and science, liked creating things he could see work, and did community work in his spare time. I was impressed he was already in an engineering design class in high school – I could have only dreamed of taking a course like that when I was his age! He wanted to attend UW-Platteville, a school that produces great engineers in Wisconsin. He is on the right track to be an engineer one day.

Today I received a letter in the mail from him thanking me for taking the time to show him what I do for a living. I’d never mentored anyone before, and I found it very rewarding to show someone what they could do, and give them some advice. Getting a letter from Andrew knowing that he was thankful for my time made it even better!

Songs to my Hipster Heart

Those who know me well know that I am really into music. I love exploring music and am always interested in finding new artists to check out. I have a particular soft spot for what I like to call “Hipster Rock” – which can be anything from electronic-y artists like Passion Pit, to downtempo stuff like Zero 7, to folksy groups like Of Monsters and Men.

I logged into my Facebook account tonight to find a post from a good friend from college, Kevin, asking me for some music recommendations based on our mutual enjoyment of The Lumineers. Since it’s Friday night, I’m three-quarters of the way into a bottle of Goose Island’s Burbon County Stout, and not doing anything in particular, I figured this would be a nice topic for the blog.

Indie Rock tends to make nice background music for writing software. As far as I’m concerned, writing software without some sort of music in the background is damn near impossible. Indie Rock is perfect, because it (mostly) isn’t jarringly loud, yet it has enough depth that my mind can use it to tune out all the background noise of the office (or, in a previous time of my life, the frat house).

I’ve been listening to music and writing code simultaneously for the better part of a decade now. Much of my discovery (both in writing code and new music) comes while I am programming away. To answer my friend’s Facebook post, here’s what I’ve been discovering and listening to lately:

  • Edward Sharpe and the Magnetic Zeros – A nice, run of the mill folksy band. Their two big songs are “Home” and “40 Day Dream”. I feel like 88.9 has a major love affair with their music.
  • The Shins – I am sure most know who they are as they are one of the most popular hipster bands out there. I felt like I couldn’t leave them off of a list of music I like though. They are on the soundtrack to Garden State twice, which is a great record and movie. Speaking of the Garden State soundtrack…
  • Zero 7 – I am all about their entire collection. After I heard “In the Waiting Line” in Garden State I was totally hooked. They are pretty down-tempo – great music for relaxing. Be sure to also check out the acoustic version of “In the Waiting Line” as well.
  • Cat Power – She’s got really unique sounds. I’m a big fan of “3,6,9″ – it is just one of those songs that seems to make code work. “Ruin” is also a nice track.
  • Bat For Lashes – A spine-chillingly great female vocalist out of England. I was hooked after watching their music video for “What’s a Girl To Do?” a few years back.
  • Sleigh Bells – OK, they are nothing like the Lumineers. I classify them more as Indie Metal, but they are seriously awesome, and I saw them in concert last year, and it was one of the best shows I’ve ever seen. I’m including them because they are one of my major favorites. They totally rock out, and I can not stop playing 2010′s Release Treats or 2012′s Reign of Terror.
  • LCD Soundsystem – I first discovered them (which is really one guy) during my senior year of high school by listening to “Daft Punk is Playing at My House” off their self-titled album. In my opinion, their catalog can be a little hit or miss, but I really enjoy the tracks “Yr City’s a Sucker” and “All My Friends”. Also, the 11-plus minute version of “Yeah (Pretentious Version)” is fun. I can positively say LCD Soundsystem laid the foundation for my interest in Indie Rock. I saw them at Lollapalooza in 2007, but I was really far back and it was 100 degrees, so I don’t think there was any possibility I could have gotten into it.
  • The Civil Wars – So, this post was supposed to be about artists that are like the Lumineers. The Civil Wars are my attempt to get back to that. I’ve just started getting into them. I threw on some of their most played songs on Spotify while I was traveling during Christmas and really, really enjoyed it.
  • Farewell Milwaukee – These guys are fantastic!! Not only do they have my favorite mid-sized city and home-sweet-home in their name, but they’ve got a great acoustic folksy sound. This was another group I discovered while traveling during Christmas that I am just getting into, but I have really liked everything I’ve heard.
  • Of Monsters and Men – I’m only including them for completeness – everyone knows this band. Every song on their album “My Head Is An Animal” is fantastic.
  • Radical Face – I don’t know their entire catalog, but I like what they hear. They do a good job with songs that start quiet and build up to being louder. Great stuff. Be sure to listen with headphones, they do some cool stuff with channels on the track “Welcome Home, Son”.
  • The Kooks – They tend to rock more often than not, but I really like when they have a quieter song. “Ooh La” is a great track.
  • Feist – Though 2007′s The Reminder is the album that got me interested in Feist, I much prefer her latest release, Metals. The Reminder was a much happier, upbeat record compared to the later release. But, Metals sounds so organic and fantastic.
  • Damien Rice – 2003′s O is a great release – one that has really stuck with me throughout the years since I first heard the track “Volcano” off of it. This disc really got me interested in powerful acoustic music.
  • David Gray – 2000′s White Ladder was probably one of the first “adult” albums I ever listened to. To this day “Babylon” is still one of my favorite songs.

I think that’s where I’m going to end my list for now. My beer is gone, and I’ve just spent the last two hours listening to music – which has made a great Friday night. Kevin, I hope you enjoy the list and find something new! I realize some of these are really far from anything like the Lumineers, but I wrote bands as I found them on Spotify, so the genres were a little all over the place.

First World Problems: I have 21,000 unread emails

It’s time that I admit that I miss my clean GMail inbox. I haven’t seen it in years. My inbox has become cluttered with emails from Groupon, LinkedIn, Amazon, and Barnes & Noble, just to name a few. Right now, I get a LOT of email – most of it being worthless. I’ve completely ignored email notifications for a while now – and I’ve decided today that I want to change that. Unlimited storage has fostered a carefree inbox management strategy. Today I’m going to fix that.

The first thing I’m doing is archiving all of my emails that are in my inbox and more than one day old. They aren’t being deleted, just being moved to the all mail box. I used the search filter in:inbox before:2012/11/26 to find these messages. A quick select all and press of the archive button, and I’m already feeling a little bit better. I now am down to only 47 unread emails. This is a nice set of emails that will let me know what websites I need to tell to stop sending me so much crap.

Now I must go through each junk message, one by one, and click “unsubscribe.” For the record, here’s all the junk email I am changing settings on:

  • LIVE 360 Events
  • Groupon – Removed emails for daily Milwaukee and Madison deals, Getaways, Now! (whatever that is), and Occasions
  • LivingSocial deals – Milwaukee South products and escapes
  • LinkedIn group digest emails – I had to select to get “No email” for each group I’m in on LinkedIn as there was no setting to just shut them all off. At least LinkedIn puts all the groups on one page.
  • Old Navy Deal Alerts
  • LinkedIn Today – apparently this is separate from the group digest emails
  • Network Performance Bulletin
  • Barnes & Noble
  • IT Security Bulletin
  • Slickdeals Daily Deals Alerts
  • AmazonLocal Deals – their unsubscribe link in the email doesn’t work. I had to login to my account to manage my subscriptions
  • PUMA
  • Google Calendar – I had to dig for the settings in Google Calendar to figure out how to shut off my daily agenda emails.
  • Amazon.com Marketing Emails
  • Threadless
  • ADTMag.com
  • VMWare
  • Republic Wireless
  • MetroEye
  • Democratic Party of Wisconsin
  • Redmond Channel Partner
  • Gilt
  • SlickDeals newsletters
  • Samsung Electronics America
  • Visual Studio Live!
  • Jetsetter
  • Madison Mallards
  • Application Development Trends
  • Big Data Bulletin
  • 1-800-CONTACTS – This one was particularly scammy. I clicked unsubscribe, and it made me login. Once I logged in, it automatically put the contacts that I had ordered in the past in my cart and took me to a check out page. I had to dig through my account settings to turn off email notifications.
  • Mobile Device Management
  • The Pabst Theater
  • Newegg.com E-Blast
Now that I’ve unsubscribed from all of these newsletters and archived them, I have exactly five email conversations that were not spam from the last 24 hours. I went through and archived them all, as they were all read or responded to by me. For the first time since I got GMail in 2004, I see the following message:

Not a minute after I took that screenshot, I got a new email from Groupon suggesting I should take a belly dancing class in Madison. If they want to know why I unsubscribed from their emails, they should look at this case – why would I, a 25 year old male, be interested in belly dancing, in a city I don’t live in? I click archive, and go back to feeling great about my inbox for the first time in years.

No, you shouldn’t let it crash

I recently read a blog post that suggested that software developers should let their software crash once in a while. The author of the post argued that letting software crash was a good thing. He used the following try-catch block to illustrate his point:

try
{
     // stuff
}
catch (SQLException e)
{
     // something went wrong in the database
     // let's fix it
}
catch (Exception e)
{
     // I'm not sure what went wrong
     // but I don't want my application
     // to crash because of it
}

From the blog post:

The intention is good, you want to prevent crashes. And let’s face it, crashes are annoying! And can be disastrous for a business; a frequently crashing application will drive away customers. But what do you put in the last catch clause, the one that catches everything that has fallen through? The same thing as you always put in a catch, right? The code to fix the problem. Except you don’t know what went wrong.

In my opinion, the author states exactly why you must prevent the crash: a frequently crashing application is bad for business. In an academic setting, it is a good idea to just let software crash. It provides opportunities for students to see problems they may have made in their code, and to correct them. However, the real world isn’t about perfect code – it is about working code – under all conditions. Let me explain with an example I’ve encountered in my career.

I write warehouse control system (WCS) software for a living. Our WCS often interacts with warehouse management system (WMS) software via a messaging protocol. These messages can be sent over a variety of protocols; the details of which are not important to this example. Our software needs to be prepared to receive and process all sorts of data from the WMS. It doesn’t matter if the WMS sends us a complete junk message – we need to be able to handle it.

99% of the time we get good data from the WMS. It is formatted correctly, the data makes sense – so we perform the appropriate action based on what was sent to us. However, sometimes we get a bad message that we can’t do anything with – the data might be bad or it might not be formatted correctly, or it might literally be junk. Who knows? We don’t, and we can’t predict all error scenarios. But we need to handle it gracefully, and we need to let the WMS know something was wrong with what was sent.

If we just “let it crash” when a junk message comes in, we’d log an exception and move on. But when you use the last catch block for that generic exception you didn’t expect, you can do smart things. You can give error messages to the user that say “Hey! That didn’t make ANY sense.” You can send messages back to the host saying that the sent message was junk. Doing these smart things helps everyone from software developers to warehouse operators track down the source of the problem, and solve it.

Letting it crash is useful in the classroom setting. In the real world, we need to handle all problems – expected or not – gracefully. The final catch all for a generic exception lets us do just that.

Why I love my local public radio station

Yesterday morning, I was listening to WUWM 89.7, Milwaukee’s NPR station on my way into work. It was the morning after the presidential debate, and I was eager to hear what the consensus was on the winner. They featured a short piece interviewing some debate viewers on what they thought. The viewers were all from an organization that looked to bridge the divide between Democrats and Republicans in Wisconsin. I really wanted to follow this organization, since I think a huge problem today is that neither side listens to the other. However, by time I got to work, I had forgotten what the organization was. What to do?

I sent a tweet out to @WUWMRadio, WUWM’s official Twitter account asking about it. This morning they got back to me! After some asking around, they were able to figure out that the story I was asking about was this one, and that the group was called Reach Out Wisconsin. You can read my whole Twitter conversation here.

You wouldn’t see assistance like this, let alone a story about bipartisanship, on most major network TV and radio stations. Props to WUWM for being awesome! This is why we need public television and radio – it is one of the few unbiased sources of information that seek to help the public left.

The wheels are finally moving

About two years ago, I started thinking about starting a blog to write about my experiences as an engineer. A lot of software engineers maintain blogs to write about complex programming problems that they encounter – I figured it would be a good idea for me to do so, too.

But, I couldn’t get the motivation to write. I got WordPress setup, and never actually wrote anything. I discovered that writing about programming is hard – almost as hard as programming itself. So, I started a different blog to write about different things: 46 Days of Awesome. My goal was to do something different every day of lent, and write about it. About a week and a half in, I discovered that writing daily is almost as hard as doing something new every day.

I think these past failures have taught me that I should be able to write about what I want, when I want to. So I’ll be writing about whatever strikes my fancy here from now on. I won’t have any sort of posting schedule, which is probably a terrible way to drive readers to my blog. But for me this is mostly a personal exercise to write about what I think is important and interesting and share it with the world.

Stay tuned, I have a lot of ideas in the pipeline that are coming soon.