Implicit Conversions with Identity Functions

Posted by & filed under Java.

What use is a method that just returns its input? Surprisingly useful. A surprising use is as a way to convert between types. There’s a well known trick that’s often used to work around Java’s terrible array literals that you may have come across. If you have a method that takes an array as an… Read more »

The Unruly Mob

Posted by & filed under XP.

At work, we’ve always pair-programmed all our production code, so we’re already pretty bought into it being a good idea to have multiple people working on a single problem. I previously wrote about some of the reasons for pairing. Mob Programming Recently, having inspired by a talk by Woody Zuill, we decided to give mob-programming… Read more »

Builder Pattern with Java 8 Lambdas

Posted by & filed under Java.

The builder patten is often used to construct objects with many properties. It makes it easier to read initialisations by having parameters named at the callsite, while helping you only allow the construction of valid objects. Builder implementations tend to either rely on the constructed object being mutable, and setting fields as you go, or… Read more »

Frameworkless Dependency Injection

Posted by & filed under Java.

Twice recently we have had “fun” trying to get things using HK2 (Jersey), to place nicely with code built using Guice and Spring. This has renewed my appreciation for code written without DI frameworks. The problem with (many) DI frameworks. People like to complain about Spring. It’s an easy target, but often the argument is… Read more »

Deep Pattern Matching in Java

Posted by & filed under Java.

This is a follow up to Pattern matching in Java, where I demonstrated pattern matching on type and structure using Java 8 features. The first thing most people asked is “does it support matching on nested structures?” The previous approach did not, at least not without creating excessive boilerplate constructors. So here’s another approach that… Read more »

Tuples in Java

Posted by & filed under Java.

The absence of tuples in Java is often bemoaned. Tuples are collections of a variety of types. They might look like (“benji”,8,”weber”) – a 3-tuple of a string, a number, and another string. People often wish to use tuples to return more than one value from a method. Often this is a smell that we… Read more »

Continuous Deployment and the Unforeseeable

Posted by & filed under ContinuousDelivery, XP.

Can you afford not to do continuous deployment? Continuous deployment is the practice of regularly (more than daily) deploying updated software to production. Arguments in favour continuous deployment often focus on how it enables us to continually, regularly, and rapidly deliver value to the business, allowing us to move fast. It’s also often discussed how… Read more »

JSON to Java Interfaces with Nashorn

Posted by & filed under Java, JavaScript.

Here’s a neat trick to transform JSON into Java objects that implement an interface with the same structure. Java 8 comes with Nashorn – a JavaScript runtime that has a number of extensions. One of these extensions allows you to pass a JavaScript object to a constructor of an interface to anonymously implement that interface…. Read more »

Pattern Matching in Java

Posted by & filed under Java.

One of the language features many people miss in Java is pattern matching, and/or an equivalent of Scala case classes. In Scala we can match on types and structure. We have “switch” in Java, but it’s much less powerful and it can’t even be used as an expression. It’s possible to simulate matching in Java… Read more »

Delete your Tests

Posted by & filed under XP.

Tests, like any code, should be deleted when their cost exceeds their value. We are often unduly reticent to delete test code. It’s easy to ignore the cost of tests. We can end up paying a lot for tests, long after they are written. When embarking on a major re-factoring or new feature it can… Read more »