Simple Flow Control Made Easy

We are currently working on a product that utilizes a long-range and low-bandwidth network. Moreover, the devices that are being communicated with are severely limited with regard to memory availability and CPU cycles. Given these constraints, it’s important that the network and devices are not overwhelmed with traffic.

Recently, we encountered an acute problem where the network and devices could easily be overwhelmed if they are powered on and join the network at approximately the same time.


When the devices join they communicate with a server, informing it that it has just joined and that it needs to be configured (name, time, etc.). It will continue to send these configuration requests at a steady rate until it receives all of the necessary configuration information. The problem is that the server would gladly respond to every request, even if the information it is sending is duplicated.

We had to make sure that duplicate messages were dropped within a certain time period. Read more on Simple Flow Control Made Easy…

Floating Point Numbers & Currency Rounding Errors

A reminder to those who learned this lesson long ago, and a warning to those that have not learned it for themselves: Don’t use floating point numbers for money.

When doing any kind of calculation with currency, accuracy is extremely important. And floating point numbers (floats and doubles) don’t have an accurate enough representation to prevent rounding errors from accumulating when doing arithmetic with monetary values.

An excellent StackOverflow response that explains the issue states:

Representing money as a double or float will probably look good at first as the software rounds off the tiny errors, but as you perform more additions, subtractions, multiplications and divisions on inexact numbers, you’ll lose more and more precision as the errors add up. This makes floats and doubles inadequate for dealing with money, where perfect accuracy for multiples of base 10 powers is required.

Ways it Can Go Wrong

When it comes to dealing with money in a computer program, a developer needs to stay ever-vigilant. Just because you, or a previous developer, made the right call by using a DECIMAL column when creating the database schema, doesn’t mean that accuracy can’t be lost before the result of some calculation is displayed to an end user. It’s all too easy to slip up and lose the necessary accuracy without even realizing it. Read more on Floating Point Numbers & Currency Rounding Errors…

Return From a Finally Clause?

I recently discovered that you can put a return statement in a finally clause in Java and it will still compile. I also discovered that if there is a return statement in a finally clause, it will silently discard an exception if one is raised in the try.

Once again, in case that didn’t sink in — a seemingly harmless return in a finally clause will silently discard exceptions and return the specified value!

Now I can’t think of why one would put a return in a finally. And I was surprised to find out it is even allowed. This got me wondering if it’s something other languages allow, and if so what happens to exceptions in those languages?

Read more on Return From a Finally Clause?…

System Testing a Java Web Service in Jetty

Automated system tests are an invaluable part of ensuring your software product is functioning as intended. On many of our projects, we are lucky enough to work with languages and frameworks that have a number of testing tools built around them, making it relatively easy to create automated system tests.

However, there are times where the tools haven’t been made yet. Recently, we have been developing a large and complex Java web service for a client. We found that while we had access to a number of well-maintained testing libraries (JUnit, Mockito, Hamcrest), there weren’t any tools to create and run automated system tests against the web service.

So, we built a way to do it.

Read more on System Testing a Java Web Service in Jetty…

RubyFlux – Statically Compile Ruby to Java

Charles Nutter, creator of JRuby, has a new offering for the Ruby/Java community: RubyFlux (formerly called fastruby). RubyFlux is a static Ruby to Java compiler. It uses JRuby to analyze the AST of the Ruby files and generate Java source files. The generated code has no run-time dependencies and ends up being pure Java.

RubyFlux could be an amazing tool if/when it gets built into a usable state. It provides massive speedups over MRI or even JRuby.

Here’s a preliminary benchmark. It generates a Mandelbrot Set ten times to compare the native Ruby to the much faster RubyFlux. Here are my results:

Read more on RubyFlux – Statically Compile Ruby to Java…

Guice AOP and the Java Persistence API

The Java Persistence API provides a programming language framework for managing relational data through entity management and ORM APIs. Libraries like Hibernate and Guice Persist implement the services defined in the JPA specification. The result is an abstraction layer that manages the underlying concerns of a relational database while providing a consistent API that doesn’t vary from one database to another.

However, even with a database abstraction layer, there are instances where the concerns of the database are also the concerns of the programmer. Moreover, these concerns can affect a number of areas in an application, and addressing them often leads to code duplication.

Aspect-oriented programming (AOP) is a programming paradigm that addresses cross-cutting concerns and eliminates the need for code duplication. Guice has built-in support for method interception that brings the AOP programming paradigm to life.

Read more on Guice AOP and the Java Persistence API

Hate-Driven Development

Most development at Atomic gets done in Vim, but occasionally we get work that better fits the use of Visual Studio or IntelliJ IDEA. When we do, we take advantage of the intelligent code features of these IDEs. Despite occasionally feeling slower in .NET or Java than in, say, Ruby or CoffeeScript, one way to keep up the pace is with a technique we call Hate-Driven Development.

Read more on Hate-Driven Development…

The UDP Firewall Punching Technique Explored

I recently read an article on The H entitled The Hole Trick: How Skype & Co. Get Round Firewalls. This article does a great job of explaining the technique of punching holes into firewalls — with an emphasis on how Skype does it.

The need for “firewall punching” arises when two clients that are behind a “typical firewall” wish to connect directly to each other. In this case, I am defining a “typical firewall” to be one that permits outbound traffic but blocks inbound traffic. Neither client can send the initial packet because it will be immediately dropped by the other’s firewall. The solution is to use a mediating server to establish a direct connection between the two clients.

Read more on The UDP Firewall Punching Technique Explored…

Toolsmithing Java with JRuby

JRuby is my favorite Java tool. It lets me wield the power of Java libraries while building tools and abstractions to speed development.

On a current project, I’m using JAXB2 to bind a large set of deeply-nested XML Schemas into Java classes. I have a Rake task to create the classes from the xsd files and bundle them into a jar. From there, I import the classes into JRuby where the software uses them to interact with other systems via XML marshaling.

Read more on Toolsmithing Java with JRuby…