Happenings

Modern Java: The Point

In the Modern Java series, I’ve looked at how Lombok removes boilerplate from bean classes, JSR-303 validation makes checking domain correctness neat and how JPA provides a convenient standardisation over the ORM space. Each of these technologies does something interesting and in an interesting way.

The point of this series, however, was not to try to provide any deep level of insight into any of those technologies; writing anything even approaching a useful tutorial about JPA alone would take up many more posts than I had ever intended on giving the subject. There is simply too much there, and too much intricacy involved to sum that up.

No, the point of this series was multi-faceted, and I’d like to take another few moments to try and explain what I was trying to achieve.

Firstly, the primary concern for designing good systems is understanding the domain of interest. If you don’t understand the domain well, you won’t be able to produce a nice user experience at the front-end. You won’t be able to write meaningful abstractions at the back-end. You won’t be able to write something that you can pick up a year or two down the line and immediately understand. Knowing and understanding the domain is a prerequisite for good design.

What does that have to do with the libraries I discussed? Each of them help you design in terms of the domain and only the domain. Lombok helps you write domain objects without all the cruft in the way. Standardised validation makes sure the correct rules are cleanly annotated and enforced on those domain objects. JPA ensures that when instances of those domain objects are persisted, they are persisted following a series of rules that make sense for the domain. Each of the libraries pushes you down a path of thinking about your domain and hiding other more mundane concerns.

Secondly, I wanted to show that clean code is infinitely preferable to cluttered code. In the examples I gave, I showed a simple bean class being built up with just a handful of annotations, each one describing it’s own purpose and providing a clean understanding of the domain. As a thought experiment, consider what that code would be like without the annotations. Imagine the number of classes and methods that even a well-designed alternative would have to orchestrate. While I’m sure it could be quite neat, I can’t imagine a solution quite as neat as the one offered by the technologies I mentioned.

That leads into my final point: modern Java features, such as annotations, and the libraries that make use of them allow for much richer, cleaner and downright better code than has been possible previously. I won’t try to argue that Java is perfect as a language as I like many of the alternatives, such as Scala, Groovy, Haskell, Ruby, Python and many others. What I am trying to say, though, is the situation with the Java language is a lot better than it was five years ago, when typical Java code was verbose, unsightly and headed away from the domain. It often obscured the domain, rather than illuminating it.

My hope is that future iterations of the language and future libraries continue to keep Java in a competitive place in the language market, by making even more problems domain-focused and removing the remaining boilerplate. With the Project Coin and Project Lambda improvements due in Java 7 later in the year, I really think this will be the case.

Film Fight 2010: June

Three new films this month, all quite different.

Rec 2 picks up immediately after the ending of Rec, with a SWAT team about to enter the quarantined Spanish apartment building. Rather than shy away from the weak ending of the original, it embraces it and builds a fairly silly plot around it. There’s all-sorts of terrible tell-don’t-show storytelling on display here, and that undermines a good amount of the work done elsewhere. Despite a forgettable plot, the audio-visual design is fantastic: in-camera glitches, sound breaking up and distorting in just the right way to make you squirm, and THAT scene with the boy. Genuinely creepy horror in places, let down by the plot changes. (See my Rec 2 Twitter review).

The Killer Inside Me is, if nothing else, beautifully shot with art-direction in the opening credits that most films can’t muster in 90 minutes. The look throughout is striking, whether we’re observing the lead sitting in a chair or brutalising another character needlessly, and he brutalises other characters frequently. There are several fairly uncomfortable, difficult scenes here; scenes of reasonably extreme violence against women (in the context of modern cinema, at least). While they’re extremely powerful and shocking moments, they sit amongst a plot that for the most part lurches along and doesn’t really get anywhere. The film is purposefully rife with ambiguity as we, the audience, try to fill in the gaps around why the lead is such a cruel and twisted man. Sadly, it’s just not interesting or well-done enough to really bother. The idea of irredeemable characters, monsters for their own sake, has surely been done better than this many times before. (See my The Killer Inside Me Twitter review).

Finally, Shrek Forever After marks the supposed end of the series, with Dreamworks claiming they won’t make any more. I wish they’d stopped at least one film back (perhaps two). The latest film in the series is definitely not a classic, lacking the originality, creativity or joy of the original Shrek. The plot is fairly forgettable and largely unimportant, with the majority of the laughs (and there are some good laughs in there) coming from little visual cues. The source material here has been stretched a little too thin: had this been an episode in a 25 minute cartoon series, you wouldn’t have noticed the difference. Okay, but not great. (See my Shrek Forever After Twitter review).

While I didn’t think any of the films this month were particularly great, the winner is Rec 2. Despite a fairly nonsensical plot, it delivered where it mattered: providing a horror-filled, cinematic experience.

Film Fight 2010: May

It may be a few weeks late, but the Film Fight for May is an extremely light one with only one film.

Four Lions is another interesting piece of comedy from Chris Morris. The premise is a comedy that follows a few would-be suicide bombers from their hometowns to training camps and back. Rather than being mean-spirited or relying on controversy, what we get is a great British farce as we see how absolutely incompetent most of the crew are. There are a few stark moments, but it’s almost entirely silly, poking fun at the idea of the bombers and their ridiculous scheme. (See my Four Lions Twitter review).

And the winner is… Four Lions. Obviously.

Film Fight 2010: April

A reasonably busy April at the cinemas has meant another bigger-than-usual Film Fight, with five films.

Kick-Ass is a ridiculous film. It’s an absurd premise (a kid decides to become a super-hero and finds out he isn’t the first one to try it), that holds together remarkably well when paired up with intentionally over-the-top violence in a pseudo-comic book world. It’s bloody, and goes quite far at times (mostly when it uses Hit Girl), but manages to pull it off by making it clear that this is entertainment and clearly straight out of a graphic novel. While the story isn’t anything to right home about, and it has some cringeworthy moments, it remains entertaining to the end. (See my Kick Ass Twitter review).

Green Zone sees Matt Damon team back up with Paul Greengrass, in a plot pulled straight from a Tom Clancy novel. Yes, it’s the usual military-complex, conspiracy nonsense, but it is reasonably fun. It works best when letting Damon get into the action set-pieces and starts to wane whenever it goes near the reasons for the conspiracy. For the most part, a reasonable balance is found to keep things moving forward. Where it really falls down is in the comparisons that have been drawn to the Bourne trilogy. Even on the action side, this is a much more relaxed affair than the impossibly well-developed skills of Damon’s previous super-spy character. Worth seeing. (See my Green Zone Twitter review).

Comedy, good comedy, can be painfully uncomfortable. Either bringing up subjects that most people consider taboo to shed some light on them or by poking fun at preconceptions. It was surprising to see that Crying With Laughter managed to blend that discomfort so well into a fairly dark thriller so well. It tells the story of a stand-up who is at his make-or-break point, going further and darker with every show, as he bumps into someone he used to know who will give him an incredible story to tell. At moments the film is incredibly funny, and at others it’s horrific as you discover the painful memories that full the lead. A few of the minor characters aside, this is a fantastic film; that brings together two kinds of unease, and produces an excellent story from them. Highly recommended. (See my Crying with Laughter Twitter review).

Mike Judge has an incredibly strong legacy with Beavis & Butthead, King of the Hill, Idiocracy and his classic, Office Space. It’s with that in mind that it’s fairly surprising his new movie, Extract, just isn’t that funny. There are some great moments and some well-observed characters (David Koechner delivers another great small part), but a lot of it falls flat. It’s not awful, but it is entirely forgettable. (See my Extract Twitter review).

Finally, Todd Solondz returns to the characters he introduced in Happiness 12 years ago, recasting them all in a pseudo-sequel, Life During Wartime. If you liked the first film, you’ll wish he hadn’t. It has a scant few jokes that hit the familiar territory of awkwardness that Solondz thrives in, but much of it doesn’t work. As a whole, the film lacks any real cohesion, with fairly uninteresting characters not really doing much or saying anything of real value. There’s not much of a theme, just a series of moments that hang together poorly. It’s a shame because a few of them work incredibly well: the scene where Billy is reunited with his father is absolutely gripping. A quite disappointing effort. (See my Life During Wartime Twitter review).

And the winner is… Crying With Laughter, for walking the fine line between horrific and funny extremely well.

Modern Java: JPA

In the previous post, I showed how JSR-303 is making validation neater. I concluded that article with the Person bean looking like this:

@Data
public class Person {
    @NotNull @Size(min=1)
    private String firstName;
    @NotNull @Size(min=1)
    private String lastName;
    @Min(18)
    private int age;
}

If we know we have a valid bean, at some point we probably want to persist it so we can use it again. I won’t waste time on spelling out how much boilerplate code is written for raw JDBC calls as most developers have moved a level of abstraction up from that anyway, with great tools like Spring’s JdbcTemplate.

I also won’t go deeply into the history of ORM in Java, as a lot has already been said. The Java Persistence API (JPA) standardised fairly sensible ideas about how ORM should work, starting from real implementations (primarily Hibernate) and pulling together the main concepts. To prepare a bean for JPA persistence, we merely need to add an @Entity annotation and specify an @Id column (which I’m going to have to add to Person):

@Data @Entity
public class Person {
    @Id
    private Long id;
    @NotNull @Size(min=1)
    private String firstName;
    @NotNull @Size(min=1)
    private String lastName;
    @Min(18)
    private int age;
}

And that’s it. You can then use a properly configured EntityManager to persist() your class (the configuration details are largely implementation specific, but relatively lightweight).

There are a lot of options available to configure exactly how an entity should be persisted when passed into an EntityManager. Again, I won’t cover them all, but it’s generally considered best practice to state your table and column names explicitly (rather than relying on convention, as I have above) and you’ll often want to make use of the built-in optimistic concurrency support to stop multiple changes happening at once.

@Data @Entity @Table(name="people")
public class Person {
    @Id @Column(name="id");
    private Long id;

    @Version @Column(name="version")
    private int version;

    @NotNull @Size(min=1)
    @Column(name="first_name")
    private String firstName;

    @NotNull @Size(min=1)
    @Column(name="last_name")
    private String lastName;

    @Min(18)
    @Column(name="age")
    private int age;
}

As a bonus, if you use a JPA2 provider AND have a JSR-303 validation implementation in your classpath, the default behaviour will be for your bean to get validated before it gets persisted, so you wouldn’t have to explicitly call the validator any more.

Again, there’s a lot more to be said about JPA, and the use cases that it supports (I haven’t touched on object-graphs, embedded collections, or the Java persistence query language), but you should have a flavour for it.