First Professional Java

Share:        

I thought I’d hit a milestone in my career, but in writing this, I realized I was wrong. I’m a professional computer programmer, among other things. I recently moved to a Java development team, and I checked in my first code today. I thought it was my first professional Java code, but that’s not quite true.

I started out as a systems administrator for a mortgage credit reporting agency, and in addition to shell scripting and Perl, I also wrote a basic call center application in VB4 of all things using an Access front end. I moved on to becoming a software support engineer, but beyond a few patches in ANSI C, my repertoire was limited to Perl, with some HTML and JavaScript for my personal homepage.

I couldn’t get anyone to consider me for programming positions. One day though, a few years later, I was approached for such a position – because of that program I wrote in VB4! The hiring manager wanted a VB programmer with with previous mortgage experience. I wrote my first professional program in VB.NET, converted it to C# shortly thereafter, and spent the next decade as a C# programmer.

I’ve had brief stints in other languages. I’m often learning new languages to expand my toolset and to learn new ways to program. Right now, it’s Node.js. And, although I’m a programmer, I’m still a shell scripter, though these days it’s in PowerShell.

I’ve done some professional work in other languages as well. I wrote a scripting engine in Ruby for a C# web application. At another place, I developed an iOS application that exposed some service data – and then I deflated when I realized that I contributed some code to the Android application we made as well.

So, today wasn’t my first check-in of Java code, but it is my first with Java as my primary language.


Bowling Game kata with Express and Node.js

Share:        

bowling

Last time, I talked about the Bowling Game kata in Node.js, and mentioned that the next step in looking at the MEAN stack is Express, which is a JavaScript web application framework.

The inclusion of Express means that the game scoring will need to be exposed as a web application. This seems like a good use case for REST. I’m aware that restify is a better fit for this kind of application, but in keeping with exploration of the MEAN stack, I stuck with Express. In doing some more exploration with Restify, I found that it needs the node development environment to be installed on Windows, which I found off-putting.

For testing, I used supertest, which offers nice functionality for testing HTTP servers with a fluent interface, another programming technique that I’m fond of.

Here’s a sample of what the tests look like:

 var request = require('supertest'),
 should = require('should');
 var game = require('../game.js').app;

var assertScoreEquals = function(expectedScore) {
 request(game).get('/score').expect(200).end(function(err,res) {
 result = res.body;
 result.should.have.property('score').eql(expectedScore);
 });
 };

var roll = function(pins) {
 request(game).post('/bowl/' + pins).end();
 };

var rollMany = function(times, pins) {
 for (var i = 0; i < times; i++) {
 roll(pins);
 }
 };

describe('Scoring a bowling game', function() {
 beforeEach(function() {
 request(game).get('/start').end();
 });

describe('gutter game', function() {
 it('should return 0', function() {
 rollMany(20,0);
 assertScoreEquals(0);
 });
 });
 });
 

The supertest library is what provides the request object. Whenever you call request, you need to execute it with an end() call. I fumbled with this for a few hours. You’ll notice that I’m ignoring the error object. I found that when I tried to use it, not only did it clutter up the test code with a done callback:

 describe('gutter game', function() {
 it('should return 0', function(done) {
 rollMany(20,0);
 assertScoreEquals(0, done);
 });
 });
 

but I also found while running in a mocha -w loop, it caused Mocha to throw exceptions after some time elapsed. I need to dig into this deeper.

Here’s the game code, using Express to expose some REST endpoints for knocking down pins and scoring the game:

 var express = require('express');
 var app = exports.app = express();

app.get('/start', function(req,res) {
 rolls = new Array();
 ball = 0;
 });

app.post('/bowl/:pins', function(req,res) {
 rolls[ball] = parseInt(req.params.pins);
 ball++;
 });

app.get('/score', function(req,res) {
 var total = 0;
 var ball = 0;
 for(var frame = 0; frame < 10; frame++) {
 // omitted, the scoring algorithm
 }
 res.send(200, {score: total});
 });

app.listen(process.env.PORT || 3000);
 

As you can see, while the plumbing is more complicated, it uses the same structure as the pure Node.js solution. That’s why I find tools like the Bowling Game kata to be valuable. By solving a problem I’ve solved many times before, the problem fades into the background and I’m able to focus on learning the language.

The next step is to include AngularJS.


Part of a Movement Toward Fluidity

Share:        
Music box, copyright CC BY-NC-ND 2.0 by Lys*

I recently posted about doing the Bowling Game kata in Node.js. On Google+, an online friend of mine Aaron commented:

Not being a software engineer, I just like reading how you write about code development. You’re clearly part of a movement that is about improving your work life’s fluidity. As your writing conveys, this encompasses efficiency and skill, but also has a notable element of beauty. I wonder why this type of movement is not present in more career fields.

I saw this as a strong complement. I’m so glad that Aaron saw my passion for both subjects in my writing. I was particularly taken by his description of the movement, which captured how I feel about my work and inspired me to expand on it.

I see myself as a shaman of sorts, a word that came to me as I’m writing this. I have one foot in the software development world, where I help solve problems for a living, primarily by programming computers. The other foot is in the lean and agile delivery space, which is focused on efficiently solving problems through delivery of incremental value. I see a number of parallels between the two worlds, but many of my colleagues don’t. When I point them out, like the similarities between refactoring and story grooming, and it’s as though I performed magic. Sounds like a shaman to me.

Aaron is right, a lot of lean and agile practices are all about increasing fluidity. We talk about the freedoms that product owners have to change priorities to suit evolving understanding of the customer and the problem. We talk about the freedoms that teams have in ordering their work so that they can develop as efficiently as possible. We talk about supple design, where the product can take a different direction to accommodate the discoveries made while constructing it. All of these are difficult, and all the people I know who do it well are highly skilled in a more than one discipline.

Those people I describe sound like Extreme Programmers. They know how to wear many hats, how to maintain an eagerness to learn and to share solution ownership. From the little time that I’ve spend in an XP shop, I know it’s an environment I’d thrive in. The moments when I can bring those collaborative practices into my day are some of my favorites.

I think those of us who practice software development this way are drawn toward the beauty that Aaron describes. We talk about it more often as “elegance”, admiring elegant designs or elegant solutions to problems. Yet I’m reminded of the book Beautiful Code, which is a collection of essays on how leading software designers approach their, dare I say, art.

Reading his comment, I felt like I was in their company. Thanks again, Aaron, you made my day.


Bowling Game kata using Node.js

Share:        

bowling

At work, my team is working on building commodity functionality for a SOA network of services. While learning more about SOA, I’ve become interested in Node.js, a JavaScript library that makes it easy and quick to build network applications.

When I’m first learning a language, one of the first projects I tackle is Bob Martin’s Bowling Game kata. I’ve written about code katas before, suffice it to say I still think they are a valuable learning tool. When learning a new language, one of the first questions I want to answer is how to unit test my code, and this kata presents a fairly simple problem that’s well-suited to test-first design.

So, I coded up a solution to the kata using node.js. There are plenty of examples on the web already, but I didn’t use them directly. I fumbled around with node.js for a while, then I went looked to improve my knowledge of the language’s facilities and constructs.

I used Mocha as my test framework. I discovered after a while that Mocha has a mode where you can have Mocha watch a folder for changes and execute tests using mocha -w. I find the instant feedback of continuous testing to be very valuable.

I started with assert for testing, but ended up choosing should, because I prefer BDD-style syntax when making assertions. Here’s a representative sample, taken from early on in the kata:

 game = require('..\\game.js');
 should = require("should");

var rollMany = function(times, pins) {
 for (var i = 0; i < times; i++) {
 game.roll(pins);
 }
 };

describe('When scoring a bowling game', function() {
 beforeEach(function() {
 game.reset();
 });

describe('all gutter balls', function() {
 it('should score 0 for 20 gutter rolls', function() {
 rollMany(20, 0);
 game.score().should.equal(0);
 });
 });
 });
 

With assert, the assertion would look like:

 assert.equal(game.score(), 0);
 

For simple assertions, assert is legible. However, for more complicated ones, I find that BDD-style syntax more naturally expresses what I’m verbalizing as I construct the test.

The code to actually perform score the game is (game.js):

 module.exports.reset = function() {
 rolls = new Array();
 roll = 0;
 };

module.exports.roll = function(pins) {
 rolls[roll] = pins;
 roll++;
 };

module.exports.score = function() {
 var total = 0;
 var ball = 0;

var isStrike = function() { return rolls[ball] == 10; };
 var strikeBonus = function() { return rolls[ball + 1] + rolls[ball + 2]; };
 // other helpers

for (var frame = 0; frame < 10; frame++) {
 // omitted, the scoring algorithm itself
 }
 return total;
 };
 

I’ve removed the algorithm in case you’d like to code it yourself. I will say it took quite a while to get the hang of all the parentheses and curly bracket nesting. Before doing this kata, I didn’t often touch JavaScript and would often mess this up the first time. Afterward, it became natural.

I did have some trouble understanding how modules expose methods and variables until I read How to Use Exports in NodeJS, which I found concise and informative.

After I got this kata under my belt, I started doing more research and getting interested in the MEAN stack (MongoDB, Express, AngularJS, and Node.js). Next time, I’ll show the kata with the inclusion of Express.


The Sundered Land, a nano-game

Share:        

I’ve been having fun with Vincent Baker’s nano-game The Sundered Land the past couple of days. It’s a small roleplaying game set, comprised of a number of single encounters, suitable for playing in a forum thread. In one called A Doomed Pilgrim, one player plays a pilgrim, and the other participants try to stop the pilgrim from succeeding. However, there is a constraint: the other players can only respond to the pilgrim’s direct questions, so they may not be able to do it. A typical exchange might look like this:

Jordan: There are two shrike banshees circling overhead, slowly swirling in a spiral that has you at its epicenter. The wind bears the unmistakable musk of their digestive pheromones. As they are normally solitary creatures, this is most disturbing. 
Me: My eyes dart back and forth, looking at the caves around me, weighing which might provide the best shelter. Have the shrike banshees definitely seen me? Anyone should answer.

The rules provide the barest amount of structure needed to make a game like this work, as well as some advice for the pilgrim. I’ve been on both sides of the table now, both as hazard and as pilgrim. Both are enjoyable. It’s fun to try to foil the plans of the pilgrim — tightening the noose, at it were. It’s also fun playing as the pilgrim to try to ask a question that will get you out of your current predicament.

The game comes as a group of similarly-themed encounters, all written on a single letter-sized page. Other games have different rules. For example, in another encounter called Caravan Guards, all but one of the players takes the role of a member of the same caravan. The remaining player takes the role of the Hazard, whose goal it is to decimate the caravan.

If you’re looking for a half-hour’s diversion, something lightweight that takes no preparation, you should give one a try. The bundle sells for $5.