Anti-pattern: Re-use through inheritance

When I first started programming in object oriented languages I used inheritance to reduce code duplication. You know the deal - you have two classes that do the same sort of thing so you create an abstract superclass and extract the common code into methods in the superclass. Voilà. No more duplicated code. But I was unwittingly creating an untestable codebase. I have worked extensively with dependency injection containers over the last five years and have begun to rely on the clean code they facilitate. [Read More]

Synchronized code in distributed systems

I encountered some code recently where a GUI client was listening for Object updates from a multi-threaded server, and it was using the version number on the Object to determine whether the incoming Object was the latest version. The server incremented the version of the Object by inserting a new row into a table in the database and using the primary key as the version number. Now consider the case multi-threaded case where two threads, T1 and T2, are making updates to the Object. [Read More]

Reflection HashCodeBuilder Performance

I recently embarked upon a performance investigation to figure out why a particular operation was slow. My theory was that it was the serialization/deserialization cost with a set of large objects; my colleague’s theory was the iteration over the set. We were both wrong. The investigation became intriguing when we started measuring the elapsed time at various points in the application and noticed that the bulk of the time was being spent in a seemingly innocuous call to construct a HashSet from an existing Collection. [Read More]

How to insulate yourself from static methods

Sometimes your code needs to call a static method in a 3rd party library and unit testing suddenly becomes difficult, particularly when that static method requires context in order to work. Enter the insulating class (also known as a Facade). Create a new interface that declares a method with the same signature as the static method you want to delegate to Create a new Facade class that implements the interface Replace the calls to the static method with calls to your new Facade class Let’s assume that the method you need to call looks like this [Read More]

How to divide without using division operator

I attended a talk at the Toronto Agile Tour on Deliberate Practice that focused on TDD Games and other programming challenges like Kata. Among the challenges were things like solving a problem without using the ‘if’ keyword, or without using primitives. Solving simple problems using these challenges makes us better programmers because they force us to come up with multiple, creative solutions to the same problem. Here’s my solution for how to divide without using the division operator ("/") in Java. [Read More]

How to be Agile when using 3rd party software framework

It can be difficult to be Agile when working with 3rd party software framework. The vendor product may dictate many aspects of the software architecture thwarting your attempts at automated testing and continuous build. However there are steps you can take to make Agile easier. I’ll discuss a few of the options that have worked for me on my current project. Insulate yourself from static methods. Introduce wrappers (Facades) to vendor classes. [Read More]

ControlTier: Initial Impressions

For the last 2 months, we’ve been using ControlTier to automate deployments to our integration and QA environments. Our deployment is a little more complicated that most Java applications because it runs on a pseudo-grid. That is, there are 2 clustered JBoss servers, a database server, and 4+ Linux servers that each run about 8 Java processes. I was able to get ControlTier up and running in a few days, and defining the jobs was relatively straightforward. [Read More]

Closing tasks

I read a great quote in I’m feeling lucky. It described a note sent from one Google employee to another as he handed over a piece of software. Here’s what I’ve done. Here’s how it’s running. Here are some things to look at. Here’s what’s going to go wrong if it goes wrong. Here are the steps to turn it off. These five points would be invaluable documentation to any new code or functionality. [Read More]

Use affirmative phrases for boolean names

Please use affirmative phrases when naming your boolean variables and flags - I find that it makes code much more readable. This is particularly true when naming the configuration settings used to turn features on or off. If the setting is named in the negative then I have to use a double negative to enable it, and everyone learnt in high school that double negatives are a Bad Thing. For example, if I have a feature that sends emails and I want to make this behaviour conditional then an appropriate variable name would be “boolean sendEmail = true”. [Read More]