Wednesday, 4 February 2015

Pragmatic Functional Refactoring in Java 8

Java 8 is here! Now the honeymoon period is over larger companies and innovators are starting to use the new features. However, Java 8 isn't just another version of Java, it fundamentally changes how you are able to approach problems.

"Java 8 and functional programming is not just for beards" -- Raoul, and is now available to developers in general. Many business problems can be expressed in an easier way than in a declarative style of programming. It is a well known fact however, if there is someone with a beard next to you they probably are a functional programmer. 

The slide deck from JFokus can be found here, the session was also recorded by the VirtualJug in September last year.

The talk is aimed at making functional techniques relevant to programmers and takes the approach of introducing a problem and how it can be solved using functional techniques. The good thing about this style is that the emphasis on how to apply functional programming techniques is not bogged down in buzzwords or potentially confusing terminology. 

The talk starts out with a problem we often face as software developers. Initially we have simple requirements that over time change and if the code is not designed correctly or refactored when necessary can be unwieldily and difficult to maintain. The problem domain introduced is around invoices:
  • Filtering all customers of a specific name
  • Later we want to process different customers rather than a fixed customer
  • We also now want to separate out different functions with that company that may have a different suffix representing the type of activity carried out
  • We also now want to state whether we want the invoice included or not
The result is some fairly messy code:

From the messy code the talk goes on to look at how applying functional methods can reduce some of the problems. Introducing the Predicate interface from the Java 8 API allows a test for whether an invoice should be included. This simplifies the code dramatically as the test for inclusion of an invoice can be packaged up and passed into the method as a function. This is the core of how first-class functions work, allowing a function to be passed around as a first-class citizen in the language. The introduction of method reference is shown as an intermediate step, but we finally see our lambda solution that makes everything quite clear. As a Predicate is a functional interface the test can be passed in as a simple lambda expression. The intent of the code is now clear and considerably simplified:

Compositing functions together to create more advanced predicates is next on the agenda, introducing how more complex functions can be combined together using the and, or methods. Just as it's a bit weird to wonder how this is working, as a Predicate is simply an interface we learn a new concept Default Methods introduced in Java 8. The discussion finishes with a conversation about how pipelines can be used to chain functions together, where the sequence of application is important.  The example discussed is an E-Mail where first the content is created, followed by the spell check and the addition of the signature. 

The next topic for the pragmatic approach to refactoring is currying and partial application. Currying is a tricky topic to initially wrap your head around and involves taking an initial argument set and splitting them out, so parts of the overall logic can be reused as functions. The talk looks at how to refactor a conversation function to be used for converting common measurements such as temperature and mass. I like to think of the approach as at each step the initial argument set is locked down or partially applied. I definitely advise watching the section on currying, Richard and Raoul do a great job of explaining the concepts. Here is the example used in the presentation:

Mutability is problematic, specifically where developers make things mutable that would be far better modelled as immutable objects. Mutability can introduce subtle bugs where a developer takes a reference and updates an object - however it could be years before that actually manifests itself as a production problem and can be tricky to find. There are however some cases where you need to use mutable objects and this is fine. The key is to use the right design for the right requirements within your application. 

The talk goes on to cover Optional and how it can be used to help prevent NullPointerExceptions in chained code and provide the ability for a developer to provide default values or return a slightly different result. Another key benefit of Optional is the removal of lots of boilerplate null checks that can lead to issues around code readability. This part of the talk is extremely clever and introduces a practical case for the flatMap method on Stream to get at the contents of the Optional container. I actually heard several developers comment on how useful this was to them after the talk. 

The talk is a taster of the full course that is run by myself, Richard and Raoul  More information can be found on our training site, with public course running in Switzerland, United Kingdom and Sweden in the coming months. 

Tuesday, 3 February 2015

JFokus 2015 First Impressions

In the centre of Stockholm, on a snowy first week of February, is the start of JFokus 2015. When I say cool it's currently -2°C with a "feels like" -11°C. The conference is a three day event, the first consisting of tutorials, the second and third are traditional  conference session styles. 

I came unprepared for Stockholm, and for future reference (mainly for myself) packing the extra items is recommended:
  • A hat. Only the speakers get the cool JFokus red hats!
  • Proper shoes, the snow is amazing here. It falls and everything just continues on as normal. The only effort I have seen of any snow being removed was a guy shovelling it off the roof of Stockholm Central Station. At least there was a barrier around the target area, on the pavement.
  • A decent coat for when you're outside, a fleece doesn't really do it.
When I got to the hotel on Monday I felt very unwell, got an early check-in and went straight to bed. I missed the majority of the first day except a few beers with RaoulRichard and Heather. The hotel bar was alive with conversations about the VM Tech Summit, which sounds to have proved popular with the attendees. 

Tuesday morning I got up and explored the hotel prior to the keynote starting. The hotel is next to Central Station, but navigating to it seems impossible. Almost everyone has a story about how they ended up in the wrong place or took about 6-7 different streets to get there. Waterside is a decent venue, with lots of space for the 1700 conference delegates.

The conference kicked off with a guy with a tape measure doing some robotic style dancing on the stage. My initial reaction was:
The "developer" was the joined by some other dancers to a pretty decent routine. The stage was then taken by Mattias Karlsson, sadly we did not get to see any of his dance moves. The main room is impressive here, with room for most of the delegates.

On day 2 I attended only a few talks.
I am planning to write up each of the sessions over the coming weeks, along with some sessions from tomorrow.