Thursday, November 20, 2014

Going the other way

As my career in software has trundled along, it's become increasingly important to me to try to give back to the community that has given me so much over the years.  From a sheer numbers perspective, most developers simply enjoy the largesse that the "givers" put forth.  Given, many of these contributors are paid to put these things out there as part of their day jobs, but the giving and sharing nature of the software community is one of the more remarkable -- and less discussed -- facets of the profession.  

My resolution for the last couple of years has been to share any code I write that could possibly be of use (and, of course, isn't proprietary) with the community.  The hope is that one of those tidbits can help somebody solve something, and maybe start a spark that can get something widespread going.  

As a part of this, I've been doing a bit of (for lack of a better word) "backsliding" to writing Java code.  As an ardent supporter of the Groovy language (and most other JVM languages), I rarely use the "mother language" to complete my day-to-day tasks.  But one thing I've realized as I've tried to get involved with the JVM-based community at large is that not everybody has this luxury.  Or indeed this opinion.  As much as I'd love to write all of my library code in Groovy, I realize that you're asking a little something of your users if you force them to include Groovy and its associated libraries as one of their dependencies.  Taking the long way around with Java results in a much cleaner set of dependencies for the end user.  

A library I offered recently was a Java implementation of the wildly-complicated "National Retail Federation 4-5-4 Calendar."  When I wrote it initially, it took advantage of Groovy's date shortcuts, with things like: 

Date aWeekLater = date + 6

There were a few bumps in the road with Map interactions, but the conversion over to Java was pretty painless.  To be honest, it was almost comforting in a way, like reminiscing about the old days with a friend.  I used to dutifully rattle off the long, repetitive lines (with the requisite semicolon at the end) and have that intuition of where I had to stick that cast statement to make things compile.  

The other plus is that you can use Spock to test the code, since it's all going to be bytecode anyway.  The library user can simply ignore the unit tests. 

I had a small sense of cockiness as I proceeded to my next conversion task (all of my code starts out as Groovy).  As part of a reporting suite I built, I put together a Java wrapper for Adobe's absolutely atrocious SiteCatalyst API.  I did my best to unfurl the insanely complex rat's nest of JSON that they return.  It was easy enough with Groovy -- JsonSlurper and JsonBuilder could marshal and unmarshal with the greatest of ease.  I decided it was time to unleash to the world, since I no longer had use for it, and also invested a pretty large amount of time figuring out how to get meaningful data out of Adobe's analytics vault.  

This where I started to appreciate the strength of Groovy.  With the single groovy-all dependency, you get everything.  Fantastic JSON support, and all of the wonderful shorthand for map access.  The work on this conversion is still in progress, but here's an example of where you can really save yourself with Groovy: 

    def requestStructure = [reportDescription: [
            reportSuiteID  : omnitureReport.reportSuiteID,
            dateFrom       : omnitureReport.dateFrom.format(OMNITURE_REPORT_DATE_FORMAT),
            dateTo         : omnitureReport.dateTo.format(OMNITURE_REPORT_DATE_FORMAT),
            metrics        : omnitureReport.metrics.
                    collectAll {[id: it.id, segments: it.segmentId ? [[id: it.segmentId]] : null]},
            sortBy         : omnitureReport.sortBy,
            dateGranularity: omnitureReport.granularity?.value(),
            elements       : omnitureReport.elements?.collectAll {
              [id            : it.id,
               top           : it.limit == -1 ? omnitureReport.limit : it.limit,
               search        : getSearchKeywords(it.elementTypeAndKeywordFilter),
               classification: it.classification ?: "",
               startingWith  : it.startingWith,
              ]
            },
            segments       : omnitureReport.segmentIds ? omnitureReport.segmentIds.collect{[id:it]} : null
    ]]
Nothing complicated, right?  In one statement, I'm building up the structure of a report with a handful of succinct expressions.  Let's just take a peek at how we'd do this in Java:

DateFormat dateFormat = new SimpleDateFormat(OMNITURE_REPORT_DATE_FORMAT);
Map<Map<String, ?>> requestStructure = new HashMap<>();
Map<String, ?> reportDescription = new HashMap<>();
requestStructure.put("reportDescription", reportDescription);
reportDescription.put("reportSuiteID", omnitureReport.getReportSuiteID());
reportDescription.put("dateFrom", dateFormat.format(omnitureReport.getDateFrom());

This is all pretty doable, but where the rails really come off are on some of the compound statements:

omnitureReport.metrics.collectAll { [id: it.id, segments: segmentId? [[id: it.segmentId]] : null ]}

How does this look in Java? I'd start by busting it out into a method: 

private List<Map<String, ?>> getMetrics(List<OmnitureRequestMetrics> metricsIn) {
  List<Map<String, ?>> result = new ArrayList<>();
  for (OmnitureRequestMetric metric: metricsIn) {
    Map<String, ?> row = new HashMap<>();
    result.add(row);
    row.put("id", metric.getId());
    if (metric.segmentId != null) {
      row.put("segments", Collections.singletonList(Collections.singletonMap("id", metric.getSegmentId()));
    } else {
      row.put("segments", Collections.EMPTY_MAP);
    }
  }
}

And frankly, it only gets worse from here.  

Thankfully, there are some wonderful JSON libraries out there, namely Jackson, which makes marshaling and unmarshaling pretty simple.  Not as simple as in Groovy, I'd dare say, but at least palatable. 

I suppose I should go and keep working on this conversion task... 

Thursday, November 6, 2014

... And now for something completely different

Like so many others in the IT community, I can directly trace my path into IT back to gaming.  I was 5 years old when my parents sprung on an 8086 PC clone.  It sounded like a vacuum cleaner when it booted up.  The dulcet tones of the boot sequence are hardwired into my brain even today.  First, a RAM check, then the hard drive would spin up and give a gratifying "BEEP-BEEP."  I'd say that my old man was actually quite a trailblazer -- he had a primitive modem in this behemoth and dialed in to his college's e-mail system on occasion.  None of that mattered to me, however (not until a while later anyway).  What I loved the most about that system were the games.  Much to my delight, the computer came with a pretty extensive array of games: a healthy combination of bootable 5.25" floppies and games installed on the hard drive. 

I cut my teeth on Moon Bugs, Bouncing Babies, J-Bird, Bushido, BurgerTime and a bunch of other ones that aren't even worth mentioning.  As the years went on, I started to feel crippled by the constraints of our old metal-clad behemoth.  You see, the main purpose of the PC was word processing.  For both my academic father and my aspiring siblings and me in our school projects.  I couldn't cajole my parents into fitting it with the hardware necessary to interface with a joystick, and it was equipped with a CGA monitor: a 4-color affair that was quickly eclipsed by EGA (16 colors) and VGA soon thereafter.  I clearly recall looking at software boxes at Radio Shack and seeing "Joystick Required," or "EGA/VGA" and feeling my heart sink, knowing that I was missing out on yet another game.  A lot of my friends were getting newer machines, equipped with the ubiquitous joystick hardware, and some even with the mind-blowing SoundBlaster card.  With this hardware, games had soundtracks and effects that were nothing short of staggering.  Coming from a machine with a "PC Speaker," this severely limited the possibilities.  Most game publishers didn't push the envelope much.  But occasionally, you'd see some brilliance on the part of the developers.  Two that come to mind are Where in Time is Carmen Sandiego? and the unlikely and addictive Street Fighting Man.

Like so many other families, my folks eventually allowed us to modernize, and we got a Pentium-powered Gateway machine and entered the "cyber age."  At this point, my interest in games really started to wane.  Development became much more interesting to me, and as first-person shooters became de rigeur, I bowed out from gaming essentially for good.  In later years, I would go to "guys' weekends" and be the only one that didn't want to get in on a networked game of Halo.  

My interest in games didn't really spark up again until the faux-musical games exploded in popularity.  I was a Guitar Hero phenom that could devastate nearly anybody.  And a few of the arcade-style games on the Wii would hold my attention.  My Wii, though, saw most of its action in "Virtual Console" mode, where I could play my old 8-bit favorites.  Blades of Steel was a frequent go-to, as was Punch Out. 

Mind you, this was YEARS before the iPhone.  I was shredding up the Guitar Hero with a wonderfully durable Nokia dumbphone in my pocket -- and I was happy about it.  When smartphones DID come around, I think it revealed to us what really worked with games.  

You see, I've noticed that a lot of iOS/Android games that have exploded in popularity of late have really leveraged that old 8-bit style.  Case in point -- Flappy Bird.  This brutally simplistic game came as close to plagiarizing NES graphics as possible: the pipes, the fishy-looking bird, the bright and simple sounds.  There's another game that I enjoyed playing for a while called The Firm.  It reminds me a lot of games I used to play on the old 8086.  

This got me to thinking.  What was the major turning point in gaming?  Where did it go wrong?  At least from the perspective of a person like me?  I really pinpoint it to the era of the affordable home PC.  I look back fondly to the Sunday paper inserts from Best Buy and Circuit City, toting ridiculously cheap Packard Bell systems with 15" CRT monitors flanked by cheap speakers.  Even with Windows 3.1 as an operating system, game publishers started to outkick their coverage.  Games like Myst really heralded the end for simpleton gamers like me.  I liked things simple and easy to learn.  Something that was minimal and insanely difficult to master.  I got a lot of mileage out of some Disney-licensed titles as a kid: Matterhorn Screamer and The Chase On Tom Sawyer's Island, the latter being a fanciful Pacman ripoff.  

Games like these didn't have ridiculous resolution capabilities and required very visible pixels to render their sprites.  This look was always very comforting to me.  And in a way, it's great to see it coming back with games like Flappy Bird and the Firm.  But what it really boils down to for me is simplicity in play.  I've really enjoyed the Plants vs Zombies games, and whiled away many hours on Angry Birds until they started to introduce complexities like gravity that overcomplicated things. 

Recently, I've been doing a lot of exploring of Abandonware and developing ideas for a game that I could develop and release to the mobile world.  If you're a Mac user like me, I highly recommend downloading Boxer.  It's a DOS emulator that is extremely well-acquitted when it comes to playing old games.  Certain games require some monkeying with processing power, and you can actually sandbag things pretty well to get them under control with cues from the program of where you're at with processing power (XT, 386, 486, etc).  It's really a lovely program. 

I also recommend doing a google search for "abandonware."  You'll find that just about every game from your childhood is out there for the taking.  You'll be amazed at how well these emulators can recreate the experience.