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. 

Sunday, 3 August 2014

Teaching ClassLoaders, Reflection and NIO2

As a leader of the London Java Community and as a developer in several firms I have noticed one trait across Java developers. We are spoilt! We have nice APIs, abstractions, IDEs and build systems which means that often we don't appreciate some of the lower levels of what is actually happening within the Java ecosystem. Most people can get by without this knowledge, however in the situation that there is an outage or a problem and we simply have no idea why something has started to fail, it's not the best time to start reading up on advanced Java topics. 

Sometimes we think we have enough information, because in a topic like class loading it might be enough to say "It's how Java loads classes". In my opinion this lack of knowledge is fairly dangerous, so in the Advanced Java course I recently taught with Mallon Associates I wanted to build an example that would capture the attention of the delegates. It covers four topics that we may be forgiven for forgetting Java is capable of.

The topics that I was aiming to teach amongst others were:

  • NIO2 file watch service API
  • Classloaders
  • Compilation of code at runtime
  • Reflection
The example I came up with was the WatchingClassLoader. The WatchingClassLoader is a toy example which watches a folder for the arrival of a new file. When a file lands in the inbox it checks to see whether it is a .class file or .java file. If it is a .class file the class loader will load it, if it is a .java file it first compiles it and then loads the compiled version. The main thread also holds a loop waiting for input into the terminal, this allows the user to specify a class and see via reflection the methods of the now loaded class within system. As the class loader is hierarchical if it is asked for a class it doesn't know about it defers to the parent. It works with simple classes that are in the default package (i.e. no package) - though it could be extended to be smarter. 


The WatchService is a neat API that allows you to register a Path to monitor for a particular file based event to occur. You can specify the type of event that you are interested in using the StandardWatchEventKinds class. Using the .take() method on the watcher blocks until that event occurs. In the final example this runs on a separate thread.

Class Loader

It is possible to write your own class loader in Java. By default the class loader in Java looks on the classpath specified when running the java command - but more often this is provided from your build system specifying the path to all the library jars your running application will require. Class loading is lazy, so a class is only loaded into the system when it is first required - this is why your code can sometimes run for years and then when it goes down a spurious code path you suddenly find yourself with a ClassNotFoundException.

Writing a custom class loader gives you the opportunity to change this default behaviour. Examples of class loaders might be that when a class is requested it is loaded from a remote network location or your class files are stored in an encrypted format and the class loader is responsible for decrypting the file. In this case I just want to load the class in from where I am watching for new files to be added. To create your own class loader you need to extend the ClassLoader abstract class.

At minimum you need to be looking to override the loadClass method and basically get a byte array of the contents of class file to load. From here you need to call resolveClass and defineClass to correctly load the class into the virtual machine. My system works by checking if the class aiming to be loaded is visible in the directory I am watching - otherwise it defers to the parent class loader to load the class. Class loaders are hierarchical in nature so this is an acceptable way of delegating upwards the loading of the class. The situation that this occurs in my mini example is when the user types something like java.lang.String into the console. A cutdown example of how to do this is below.

Compiling on the Fly

One feature that surprised many of the people who took my course was that it is possible to invoke the Java Compiler from within an executing Java process. ToolProvider allows us to get hold of the JavaCompiler and from there we can pass in a CompilationTask which contains the .java file we are looking to compile. This is best shown by example: 


The final stage is reflection, which allows the user to look at the .java file or .class file they have added to the running system. In a demo this is the closest we get to the wow moment :). Reflection is the Java mechanism that allows inspection of a class at runtime. Most people are using reflection, even if they don't know they are doing so, as it is heavily used in frameworks such as Spring. In the example I just print out the methods as this is enough to prove the point, though on the actual course we use reflection for many other purposes including to invoke methods and generally prove what is possible. The simple snippet for allowing reflection on our classes via a custom class loader looks like this:

The Full Project

You can find the full toy example that you can run and play around with on GitHub. As this is a teaching example, there are plenty of places that this could be taken further - so I welcome any pull requests or tidy up to the code. It was actually something originally produced via live coding in front of the class, which has been a fairly tricky learning curve to get used to. That said now I wouldn't run a course without live coding, as it's where you get the most questions and although it's massively stressful it makes for a great session.

Saturday, 12 July 2014

Update and New Career

I thought it was about time I dropped an update onto the blog about why I've been quiet recently and what has been going on.

Early this year I decided to change career paths slightly and become a technical trainer. The highlights of my career as a developer have been to help others improve and learn from others in an informal supportive environment. I am now lucky enough to be able to do this full time designing new and fun ways of helping others learn Java, Python, C++ and .NET technologies. I work for my own company James Gough Ltd, with one primary client Mallon Associates. So far I've run ten training courses and had people booking onto my future courses, it's been challenging but one of the best things I've done. I have a fairly packed end to 2014 as I gear up for our 15 week training program.

As well as working on private training I have also teamed up with Richard Warburton and Raoul-Gabriel Urma. I am excited to announce will be offering public Java 8 courses with a focus on Lambdas and preparing Java developers for the changes that are right on our doorstep. Java as we know it is going to change, for years some developers have been dragging their feet with Java 1.4 style syntax and missing some key changes. An appreciation for the Object Oriented/Functional relationship is now required to both be able to design and build code that is succinct and solves the business problem in the best way.

This year is very busy with it being my first year in business and getting married so sadly I've had to put conferences on hold for a year, but I'm hoping that early next year I can attend jfokus.

Friday, 24 January 2014

Live Coding on Java 8 Date Time API

Yesterday I recorded a live coding session on java.time. Below is all the information so you can code along if you like:

The Video

The Source Code

All the source code is freely available on github you can get this running the following command:

The (brief) details for setting up the IDE can be found in the readme.

The Slides

Monday, 20 January 2014

Java 8 Date Time: Happy Martin Luther King Day

Today is Martin Luther King Day in the US. I had totally forgotten about this holiday until I saw a few tweets this morning about it. The interesting thing about this holiday is that rather than being on a specific day, it is on the third Monday of January.

As I'm currently preparing some JSR-310 material, I thought it would be interesting to share (and discuss) TemporalQuery - a new interface in Java 8 that makes it possible to write custom queries against the new DateTime classes. There is often a use case in business that an event happens that is unique to a particular domain, an example would be Futures market rolls of contracts or time periods of validity.

TemporalQuery allows us to capture the logic to perform an operation against a temporal class and return an object representing what we were querying. The interface is generic so we can choose what we return. This is a really neat design feature in Java 8 allowing us to externalise logic outside of the core API, but encapsulate the functionality as a data operation.

TemporalQuery is also a FunctionalInterface so it can be used as a lambda on a stream of temporals. In the example below we just consider one date. To find the next Martin Luther King day we can take the current date and perform the following: NextMartinLutherKingDayQuery());

The NextMartinLutherKingDayQuery is our implementation of a TemporalQuery:

Here are some things to note about the implementation:

  • In the private method I take a date at the beginning of January of the year passed. Using a TemporalAdjuster I manipulate that date to cycle it forward to where I want to be. TemporalAdjusters add more power than just plusDays or minusMonths to allow us to work on more expressive situations with date and time. 
    • The TemporalAdjusters class has some really nice methods as used here, or you can write your own TemporalAdjuster if it doesn't exist. 
    • NextOrSame is useful for this type of operation where we don't know what day we are on, but if it's the day we are querying we don't want to advance the day. We use it to iterate to the third Monday of the month of January in the year in question.
  • I then use a the Period class to find out from the date passed whether Martin Luther King Day has passed, or it is today. If it has passed I return next year's date, otherwise returning this year's date. Period is a nice abstraction so I don't have to start subtracting millisecond values etc - yes we've all seen it. 
If you're interested in playing around with this I suggest downloading the latest Java 8 beta and having a go. 

Happy Martin Luther King Day!

Saturday, 18 January 2014

User Group Visit to San Francisco

Why am I in San Francisco? 

Is it:
  • A micro brewery pub crawl?
  • The fact it's almost 20°C tomorrow?
  • To attend a Brazilian BBQ in January? 
These things are certainly going to be a nice bonus of being here, however that's not the main reason. I'm here representing the London Java Community at Oracle's big annual user group leaders convention. The purpose of the conference is to bring leaders of a variety of different Oracle user groups together to discuss best practices and drive improvement of communities and involvement for all those concerned. Particularly this year the topics I am interested in and will be participating in are:
  • Using social media - I'm particularly interested in discussing handling negative discussion and feedback via this extremely public forum.
  • Turning members into leaders and growing speaker talent. This has been an active stream in the LJC and Trisha Gee and team have been doing a fantastic job of running workshop and helping people gain the courage to make that first talk. 
  • Gamifiying your user group experience and improving user group content. I'm mainly interested in this because I get motivated by badges and awards for certain things. I think it could put a fun spin on how we run certain events in the LJC.
I plan to blog about interesting conversations and what comes out of these themes over the course of the week. The summit will only be the beginning of the discussions and I look forward to seeing more about what our members think to some of the ideas and which may work for us. 

When discussing any kind of conference and benefits of conferences I always say that it doesn't have to be a specific talk that motivates you but a catalyst to change. All the major turning points in my career have been down to ideas gained at conferences. It will be interesting to be involved in a non technical conference across a range of different products, experiences and backgrounds. 

I have very kindly had partial sponsorship from the LJC and the JCP Program Office to be here this week and I can't thank them both enough for the opportunity. 

I am now a JCP Executive Committee Representative

Why are the JCP helping to sponsor my visit here? After 3 years of working on JSRs with the London Java Community with the LJC JCP committee I have now stepped up my involvement and represent the LJC directly with other JCP executive committee members. I am attending my first face-to-face meeting at the end of this week. I'm really thrilled to be working with so many fantastic organisations and individuals to help maintain and form standards within Java. As the meeting has official minutes it's unlikely I will post anything further.