Archive for June, 2008

Game Over, Man!

Monday, June 23rd, 2008

I recently went to my local GameStop in an attempt to purchase the game: “Zach & Wiki” for the Wii to play with my 4 year old daughter.  I enjoy going to GameStop despite it’s name which puts two words together that conjure up memories of all the times my original Nintendo crashed on me.  They might as well be named “GameLockUp”, or “GameFreeze”.

Anyway, it shouldn’t have surprised me when the store clerk said that they didn’t have the game in stock.  Indeed, if the game that you’re looking for is not Grand Theft Auto IV, chances are slim that GameStop will carry it.

What did surprise me however, was when I went to order it on-line, through GameStop, I was met with this:

gamestop.PNG

Now, I’m baffled.  This game that I’m looking for is not old:  it was just released a few months ago.  It’s produced by Capcom (of Megaman fame), one of the largest video game publishers.  Something to note here is that not only did it not find the title, but it didn’t even take a stab at guessing what I might have meant. I mean, this is a game store, I at least expected a Guitar Hero or Halo recommendation.

So I turned to Amazon:

Amazon to the rescue!

Amazon had no trouble handling my search request.  I even misspelled the title for maximum contrast with GameStop, and it was still the first hit.  Not only that, but the second hit listed, “Mercury Meltdown”is actually an excellent recommendation.  Once Amazon realized that I was looking for a Wii game, it could have easily just recommended the top five Wii titles, but it actually gave me a very similar, age appropriate title.  I felt compelled to buy that game too, if only to give its Recommender positive reinforcement.

GameStop sells games.  Amazon sells books.  This should have been GameStop’s sale, not Amazon’s.  GameStop seems to be part of the dying breed of company that Chris Anderson talks about in his book The Long Tail.

GameStop stocks only the very few popular games in the “Short Head”.  Amazon wins because of its ability to tap into The Long Tail with Amazon Marketplace, and its superior search and recommendation systems.

Speaking of recommendations, here’s one for you: People who enjoyed this blog post may also enjoy the talk that I’m going to give in two days on Recommendation Systems, Project Aura, and Search Inside the Music.

The talk is only open to City Tech employees, but it’s not too late to apply for a job.

Groovy Builders: A Pair Programming Experience

Tuesday, June 17th, 2008

So I’ve been meaning to write up a post on my experience with Groovy’s Builder support.  Rather than actually do this though, I thought it would be more insightful to enclose the transcript from my latest Pair Programming session, which revolved around writing a custom Groovy Builder and integrating it into our application.

Let’s have a look, shall we?

—————————————–

me: Ready for another day of Pair Programming, Doug?

doug: Ugh. Yeah, sure. Did you think about how to address our DSL design issue since our last session?

me: Um.  Yeah, I totally did…  What is the design issue again?

doug: Good grief, we go over this every morning!  It’s our job to take all of the line item budget data, organize it by department and produce a budget book.  The budget book is made up of Book Sections.  Book Sections contain other Book Sections and Page Elements. Book Sections map to Departments, while Page Elements map to budget line items.  So it’s our task to map from one structure, a list of departments with budget data, to another structure, a list of book sections with page elements.

me: Oh right, of course.  I remember now.

doug: So,  I’m afraid to ask, did you come up with any ideas?

me: Sure did!

doug: Great, let’s hear them.

me:  Um. You first.

doug: That’s a big surprise.  Ok, I’ve been reading up on Groovy Builders and-

me: I was thinking Groovy Builders too–I just forgot that I had thought of that until you just said it now.

doug:  I’m sure.  Anyway, thanks to Groovy’s closure and dynamic method support, notably pretended methods, it has this very useful Builder pattern.

me: Right, I know all of this already.

doug:  Really?  By all means, please take it from here.

me: …um…interface…implements…er…class…abstract–

doug: Are you just stuttering keywords again?  Honestly, how did you even get this job? Ok, so let’s take the use case where we already have the organizational hierarchy loaded up.  Remember, Departments are made up of Organizations, which are made up of Bureaus, and bureaus have budget line items. So it looks like this (takes the keyboard):

class Department {
  List<Organization> orgs;
}
class Organization {
  List<Bureau> bureaus;
}
class Bureau {
  List<BudgetLine> budgetLines;
}
class BudgetLine {
  long amountRequested;
  long amountApproved;
 ...
}

So we need to map this hierarchical structure to the BookSection and PageElement world.

class BookSection {
  String name;
  Style style;
  List<BookSection> subsections;
  List<PageElement> pageElements;
}
class PageElement {
  String content;
}

So each Department/Org/Bureau needs to map to a BookSection, and each BudgetLine maps to a PageElement, and we need to preserve the structure.  Using Java we could do something like:

BookSection rootSection = new BookSection("root");
for (Department dept : departments) {
  BookSection deptSection = new BookSection(dept.getName());
  rootSection.add(deptSection);
  for (Organization org : dept.getOrganizations()) {
    BookSection orgSection = new BookSection(org.getName());   

    deptSection.add(orgSection);
    for (Bureau b : org.getBureaus()) {
      BookSection bureauSection = new BookSection(bureau.getName());   

      orgSection.add(bureauSection);
      for (BudgetLine line : b.getBudgetLines()) {
        PageElement element = new PageElement(line);
        bureauSection.addPageElement(element);
      }
    }
  }
}

me: Wow, that’s really verbose!

doug: Finally a relevant, however uninsightful, comment.  But using a custom Groovy builder, we can turn it into–

me: I think I know where you’re going, here let me take a crack at it. (grabs keyboard)

class Doug extends Nerd

doug:  Give me the keyboard!  It wasn’t funny yesterday either.

me: Yesterday “Nerd” was an interface.  I refactored it.

doug: Anyway, using a custom Groovy Builder, the code now looks like this:

BookSection root = builder.bookSection(name:"root", style:default) {
  departments.each { dept ->
    bookSection(name:dept.getName(), style:deptStyle) {
      dept.getOrganizations().each { org ->
        bookSection(name:org.getName()) {
          org.getBureaus().each { bureau ->
            bookSection(name:bureau.getName()) {
              bureau.getBudgetLines().each { line ->
                pageElement(line)
              }
            }
          }
        }
      }
    }
  }
}

me: Well, that’s slightly more readable, I guess.

doug:  What are you talking about?!  It’s much better!  It much more closely captures the budget domain as a DSL.  The “builder” object that kicks that all off is a subclass of FactoryBuilderSupport, the same superclass of the popular SwingBuilder.  We’ve just adapted the idea from making graphical widgets to making BookSections and PageElements. The FactoryBuilderSupport handles  the associations, so we don’t have to worry about wiring the BookSections up to each other like we did in the Java version.  The structure is implicit in the code.

me: Oh!  I think I have the hang of this now.  Oh wait.  What version of Mozilla do we have to support for this application?

doug:  What?  That doesn’t make any sense.  First of all this is desktop application.

me: IE 3 then?  No IE 4, I think.  Yeah, IE 4.  I mean, if you haven’t upgraded by now, you’re out of luck, right?  Can I have the keyboard to update my Facebook profile now?

doug:  Sure…I’ve had enough for now anyway.

Groovy Pretending

Sunday, June 8th, 2008

A Humorous Setup

groovy source file: Uh, hi compiler, I need to invoke a method on an object — the thing is, I’m not totally sure that the object has this method.

groovyc: Relax, man.  No worries, I’ll just go ahead and generate some byte code for you.

groovy source file:  Really?  You’re not going to check to see if that method exists first?

groovyc:  Static checking is for squares, man!  We’ll just let the Runtime handle it.

groovy source file:  Isn’t that dangerous?

groovyc: You’re starting to sound like my old man, Java, and you’re totally killing my buzz.

javac: I heard that!  You kids with your dynamic method invocation! You’re going to get RuntimeExceptions all over yourselves!

The Comparatively Boring, but Necessary Background

Groovy supports Dynamic Method Invocation, that is, the ability to call a method dynamically at run time.  So if we have an object like:

class SomeObject {
  def someMethod() {
    // do stuff
  }
}

We can then call that method at run time dynamically like so:

String methodName = "someMethod"
new SomeObject()."$methodName"()

And that will all work out fine.  Of course you can do the same thing in Java through reflection:

SomeObject obj = new SomeObject();
obj.getClass().getMethod(methodName).invoke(obj);

Provided you wrap that in a “try” block to catch the six or seven exceptions that could occur, you’re basically at the Groovy version.

It turns out however, that Groovy can also go the other way — that is, you can call a method on an object which that object does not even define.  Apparently this feature is called “pretending”, and it’s kind of neat, so pull up a chair and read on.

The Meat of it All

I’m developing a Java application that will ultimately be maintained by the client’s internal IT staff.  These guys are very strong database programmers and very knowledgeable of the details in their domain (the city’s budget).  With six billion and change to be accounted for, there are organizations, bureaus, departments, and divisions flying all over the place with custom rules aplenty.

This situation seems to cry out for a DSL (Domain Specific Language) and that’s where Groovy comes in.  These guys are already experts in the city’s complex organizational structure, it would be nice if that knowledge could translate to the code as seamlessly as possible.

So now that the stage is set, let’s look at a small example where Groovy Pretending can be useful:

In one section of this application, summaries need to be generated based on different parts of the budget.  So budget line items are being sucked in with various categories and need to be totaled.  No two summaries are the same, so some categories may have “contractual”, “land”, “other” (and my personal favorite “other 2″), while a different summary could have “land”, “equipment”, and “commodities.”

So one of the ways to handle this could be to have a “summary” object that keeps a running total of each category in a map.  Something like this:

class Summary {
  private Map<String, Long> map = new HashMap<String, Long>();
  public void addTo(String category, long amount) {
    if (!map.containsKey(category)) {
       map.put(category, 0);
    }
    map.put(category, map.get(category) + amount);
  }
}

So then to use this class:

summary.addTo("Contractual", contAmt);
summary.addTo("Equipment", equipAmt);

This is fine, it all works and doesn’t ask too much of someone not completely familiar with Java.  However, in Groovy, will can turn it into this:

summary.addToContractual contAmt
summary.addToEquipment equipAmt

Now it looks slightly more like a DSL, but the kicker here is that the Summary class doesn’t have the methods “addtoContractual” or “addToEquipment”.

In Groovy, methods are dispatched via the object’s invokeMethod method.  So if we override that method we get:

Object invokeMethod(String name, Object args) {
  if (name.startsWith("addTo")) {
    String methodName = name.substring("addTo".length())
    if (!map.containsKey(methodName)) {
      map.put(methodName, 0)
    }
    map[methodName] += args[0]
       return
    }
    else if (name.startsWith("get")) {
      def ret = map[name.substring("get".length())]
      return ret != null ? ret : 0
    }
  return InvokerHelper.getMetaClass(this).invokeMethod(this, name, args)
}

In the above example, if the method to be invoked starts with “addTo”, it will hash the rest of the name (Contractual, or Equipment in this case), and add the argument to the mapped value.  If the method name starts with “get” it will retrieve the associated value, again, using the remainder of the method name as the key.  If neither case applies, the method will get invoked as normal.

So in a nutshell, we have this class where a client could call any sort of “addToX” and “getX” methods on this object, and the object will pretend that it actually has methods of that name.

Thanks for Reading This Far! (I wouldn’t have.  Seriously, I have Attention Issue–Ugh, I’m bored with this Sentence.)

Now, I’m not necessarily advocating this as good design practice.  It’s no more type safe than the Java version (using Strings), and what’s potentially worse is that it gives the illusion of type safety since developers are used to compilers resolving method names.

I do, however, think it is an interesting feature of the language.  Plus it paves the way for Groovy Builders, which I think are a good design practice.  So stayed tuned for the Builder post…

Also, my apologies if after reading the title you were expecting a tutorial on building forts out of couch cushions while listening to Hendrix.  That post will follow later in the week under the title “Dynamic Method Invocation with Dynamic Languages”.

Google Reject

Thursday, June 5th, 2008

A while back I scored an in-person interview with Google, Inc.  I prepared as best I could for it beforehand (by watching the “Interviewing at Google” video).  I was intimidated and nervous for sure; here was the company that had transformed not only the landscape of the internet, but also the global economy.

So I was surprised when I felt really good walking away from the interview.  I had met with three different engineers for about an hour each, answering various engineering and algorithm problems on a whiteboard, and honestly I felt like it went really well.  You can imagine my disappointment then, when my recruiter contacted me several days later to notify me that I was a Google Reject.

Since I had felt like I rocked the interview, I started to get worried, and wondered if I even knew how to rock at all, or if I was capable of recognizing rock even if I was in its presence.  Here was a company that the instructional video assured me valued “creativity and not necessarily the correct answers”, and I certainly had that in spades (incorrect answers).  So what happened, I wondered?

I asked my recruiter for detail about where I went wrong.  After a bit of pleading she finally forwarded me the notes that everyone who had met with me had taken.  I’ve included the notes below so that others may learn from my folly.  Even though I felt great about my interview, and still don’t know what I would have done differently, something must have triggered a red flag.

==================================================

Google, Inc.

Location: Chicago, IL

Interview Candidate: Sten K. Anderson

(Please return these notes to the HR representative once the candidate leaves)

Notes from HR:

Mr. Anderson arrived 5 minutes early for his scheduled interview.  However, he spent 10 minutes trying to push open the front door, clearly marked “pull”.  He eventually followed another employee in, and then commented to himself about his resourcefulness, and muttered something to the effect of “so the game is afoot” in a Sean Connery-esque accent.

Notes from Engineer #1:

Before I had a chance to ask him his first question, Mr. Anderson spent the first 20 minutes of our time talking about how good he was with dogs.  He went on at length about how he loved to come home to his chocolate lab “Hershey”, and hoped to breed her someday.  I was finally able to squeeze in a question about sorting elements in an array.

Ten minutes into solving this problem, he burst into tears and admitted that he had lied, and in fact was not very good with dogs at all, that he was, in fact, allergic to dogs.  He then asked me if I still liked him.  When I suggested that perhaps he just focus on the problem at hand, he muttered to himself “you’re the problem at hand”, crossed his arms, and stared at the floor.  We spent the remainder of our time in silence.

Notes from Engineer #2:

When I asked Mr. Anderson his first question, he said that it was a question for nerds, and that he was in a rock band and one of the “cool kids”.  He asked if I thought if any of the other nerds here would be intimidated by his coolness when he started working here.  He never did get around to answering any of my questions.

Notes from Engineer #3:

After I introduced myself, Mr. Anderson said that since he had worked for Sun Microsystems in the past that he understood himself to be a “shoo-in” here and understood this all to be a formality, and that if we wanted to spend our time talking about cool cars or Monster Trucks, that would be ok with him.  When I asked him what he did for Sun, he said it was “classified”, and then asked me who I thought would win in a fight: Truckasaurus or a 1970 Camaro.

Overall Assessment:

While we believe that Mr. Anderson interviewed better than most of our candidates, we feel that the lack of Spring and Hibernate experience disqualifies him from any open positions.  We invite Mr. Anderson to reapply in the future after correcting his experience deficiencies.

======================================================

Groovy Without Grails

Tuesday, June 3rd, 2008

me: So I’ve integrated Groovy quite nicely into my Java project.

coworker: It must be a Grails project.

me:  Nope!  It’s a Swing desktop application.

coworker: Oh sure, makes sense, you’re probably using the excellent Groovy SwingBuilder.

me: Wrong again, coworker!

coworker:  Whaa?!  Well then what could you possibly be using Groovy for?

me: Great question!  One that will be answered in a future post (despite the bait and switch title of this post).  For now though, it’s:

Groovy IDE Showdown!

When I started using Groovy about two months ago, I scoured the landscape of Java IDEs in search of the one with the best Groovy support.  I searched high and low, but in the end only had the attention span to try out three.

So here we go:

Netbeans:

I had started using Netbeans at the beginning of this project (about two months prior to the time in question).  Version 6.0 had just been released and spirits were high in the Netbeans community.  And why shouldn’t they be?  Netbeans was a veritable Rags to Riches story, rising from the horribleness of Forte, wading through the slow start up times of every version before version 6, to arrive at actually a very decent IDE.

The winds of change seemed to be blowing in Netbeans’ direction, and I wanted to blow in that direction as well.  So I abandoned Eclipse, my IDE of five years running, for greener pastures and faster build times.

I took to using Netbeans for my Java development very quickly.  However, this was less Netbeans doing, and mostly because I’m a very fast learner.  When it came time for Groovy integration, I thought, surely Sun will have a stellar plugin for the premiere scripting language that sits on Java.  It turns out that, no, they don’t, at least not yet.  They do have a plugin “under development”, due out in full sometime by the end of the year.

You can get early access to the plugin though if you’re willing to install the development version of the IDE.  I went ahead and did this, and then attempted to install the plugin, which I couldn’t get to work.  Given my previously established ability to pick things up quickly, you can imagine the surprise and frustration that I felt.

At some point in my tinkering, Netbeans refused to startup at all, mostly likely out of frustration with me over installing the nightly development build every morning for two weeks.

The writing was on the wall, I needed to leave Netbeans, and make a clean break of it.  After my messy breakup with Eclipse, I wasn’t keen to go back across that burned bridge, so I decided to see what all the fuss was about with IntelliJ.

 IntelliJ IDEA

I approached this IDE with suspicion, mostly because I didn’t trust its CamelCase spelling.  However, I figured that they were trying to spell “Intelligent”, which I could certainly relate to, but made a typo with the “J”, got flustered, stopped, and tried to distract from this blunder by shouting the second word.

I had heard that this IDE boasted the best Groovy support in the industry, so I forgave the non-zero price tag, downloaded the trial and fired it up.  The interface was a complete mystery to me.  All of the windows, widgets, gears, levers, and knobs seem to work in concert to confuse me.  After five minutes of aimlessly wandering around with my mouse (as in literally wandering the halls), I was starting to doubt what my mom had told me about being a Fast Learner.

Doubtless there was Groovy support hiding behind one of IDEA’s rickshaws, but I didn’t have the necessary IQ to derive where that might be.  So I was left having to go back, mouse tail between my legs, to my old friend, Eclipse.

Eclipse

It turns out that the Eclipse plugin for straight Groovy (no Grails, thank you very much) is quite good.  It has its issues, but it does syntax highlighting, incremental compiles, some static type checking, and even code completion.  It’s not perfect for sure, but better than Netbeans’ “Doesn’t work”, and IntelliJ’s “Whaa???”.

On Cloud Nine with Nimbus

Sunday, June 1st, 2008

 Here’s a screen shot of the Swing application that I’m currently working on:

System L&F

It has a nice potpourri of the older established Swing and the younger, hipper, SwingX components.  You’ve got your JTrees, JTables, JTabbedPanes, a JXEditorPane, and even a JProgressBar chugging away indeterminately in the lower-right corner.  Thanks to the”system” Look & Feel, it will fool most users into thinking that it’s a native Windows XP application.

After attending JavaOne though, I felt compelled to upgrade to Java 6, Update 10 (Beta)… (Build 23) so that, among other things, I could check out the new Look & Feel:  Nimbus.

Here’s how that same application looks now:

Nimbus L&F

When the application started for the first time under this new hotness my heart rate increased, my pupils dilated, I sat up a little straighter (which is to say, I slouched less), and in general exhibited all of the symptoms typical of being in the presence of something of above average beauty. I realized at once that I had developed my first ever crush on a Look & Feel.

The shadows on the text fields, the curvilinear bluish gradient scroll bars, the anti aliased fonts, the pulsating progress bar, they all seem to brag, “show me to your pretentious OS X friends…you know you want to”.

“That looks cool, what are you working with?”, a coworker asked me as he was walking by.  Caught off guard, and flustered, I closed the window immediately and brought up a web browser.  “What?  Me?  Nothing?  I wasn’t working.  I was just surfing the web.”

When I call my wife to tell her that I need to work a little later than normal, I’ll try to cover it up by saying, “but don’t worry, I won’t be working, I’ll just be going out for drinks with some coworkers”.

I realized that I may have been taking things too seriously in my mock-relationship with Nimbus, when upon hearing another coworker saying that they didn’t see what all of the fuss with Nimbus was about, I blurted out, “I’m sure everybody really wants to hear your perspective Mr. I-wouldn’t-know-a-quality-relationship-if-it-hit-me-in-the-head!” (no, that’s not really his name).

Healthy or not though, my relationship with this new L&F has made me more productive.  Now if you’ll excuse me, I have to log some overtime.