Yield Return in Java

Posted by & filed under c#, Java.

A feature often missed in Java by c# developers is yield return It can be used to create Iterators/Generators easily. For example, we can print the infinite series of positive numbers like so: public static void Main() { foreach (int i in positiveIntegers()) { Console.WriteLine(i); } }   public static IEnumerable<int> positiveIntegers() { int i… Read more »

Adding toList() to Java Streams

Posted by & filed under Java.

The Java Streams API is lovely, but there are a few operations that One repeats over and over again which could be easier. One example of this is Collecting to a List. List<String> input = asList("foo", "bar"); List<String> filtered = input .stream() .filter(s -> s.startsWith("f")) .collect(Collectors.toList()); There are a few other annoyances too. For example… Read more »

Monitoring Check Smells

Posted by & filed under ContinuousDelivery, XP.

I have become increasingly convinced that there is little difference between monitoring and testing. Often we can run our automated tests against a production system with only a little effort. We are used to listening to our automated tests for feedback about our software in the form of test-smells. If our tests are complicated, it’s… Read more »

Work around Java “same erasure” errors with Lambdas

Posted by & filed under Java.

A common frustration with Java is the inability to overload methods when the method signatures differ only by type parameters. Here’s an example, we’d like to overload a method to take either a List of Strings or a List of Integers. This will not compile, because both methods have the same erasure. class ErasureExample {… Read more »

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 »