Thursday, April 5, 2012

Grails, Hazelcast, Hibernate and 2nd level cache

If you'd like to use Grails with Hazelcast caching solution you need to be aware that there is an issue with Hazelcast compatibility with the version of Hibernate used by Grails (3.6.10.Final).

Reading on the Hazelcast Wiki you'll be tempted to use the "new" ways and specify hibernate.cache.region.factory_class. Unfortunately that doesn't work. See this bug report for more information.

But there is hope! Instead you can specify hibernate.cache.provider_class and everything should work as expected. Hibernate does all the adapter heavy lifting and life is good again :)

Anyways, Hazelcast is plain ecstatic - you should really give it a try and get a feel of what a good library feels like when you use it. It's awesome!!!

Back to the roots

I've been going over some of my old stuff that I wrote back in the day in 1998 when I started to work professionally as a developer. Among other things (like using polish names for variables, classes and what have you and seeing absolutely no automated tests whatsoever) I've also spotted some nice stuff that I wanted to share with the class.

DDD was normal

Back in the day having a TTeacher (Delphi) class that knew about the schedule and could tell you which one is going on at a particular point in time was no big deal. Classes were not PODO, they actually knew how to do stuff. Just to give you another example here's something that struck me fairly recently:

Given a Company company and a Candidate candidate if the company decides to hire candidate then the output is an Employee employee. This could be written down more/less like this:
Employee employee = company.hire(candidate)
Now take a look at this and tell me how would you call this now a days? IT IS DDD at its purest! We have domain classes that can be retrieved from some source and operated upon.

Short methods were normal

Actually this isn't entirely true but it was pretty much the standard that a method contained calls to other methods that contained calls to other methods. I kind of felt the necessity too express things clearly or else the code would then be unreadable. So the whole "one-screen method" or "CCC < 5" was kind of natural even though nobody told me to do so. It just felt right.

Coding standard

This has surprised me most: All (and I mean it when I say "all") of the code has one and the same coding convention. There's no deviation whatsoever when there is no special case that needed separate consideration of formatting rules. And above all: it was based on VCL's code. Maybe not 100% equal but very, very close. Now that is some code you read like an open book just a after minutes of getting used to the coding style.

I, J and K were iteration variables - and nothing else!

Back then there was a number of variable names that were reserved for a certain task. "I, J, K" were always and without exception the iteration variables. There was no itinerary "i" or class "K". Similarly "S" was always a temporary string variable. The code was so damn short even though there were no closures to express things better!

What has changed?

Well, what has really changed after the year 2000? There were 3 major factors that influenced me to write worse code:

  • I stopped writing code for myself
  • The schedule became a lot tighter
  • The complexity sky-rocketed when I started writing calculation-intensive apps

With that in the picture when I look at the code I wrote after 2001 I feel like the IQ of the author just dropped 30-40 points!

The Corporate times

One very much disastrous time for me was the time when I started working for an international corporation. And I mean it in every single way one can possibly imagine. That was the place where I learned that even though the application is mission critical (people's life depends on it!!!) it is not really important that the quality of the code is kept at a reasonable level. It was really enough that the application worked around 60%-70% of the time! AAAAARGH!!! I remember fighting the original team members to the death trying to teach'em about quality of the code. That was the time I worked for the first time with "legacy code".

Java - the ultimate readability killer

As much as I hate Java the language for its verbosity and lack of advanced features like properties I'm inclined to say that the worse thing about Java are Hibernate and POJO-style programming model. Why?

Let's summarize what a POJO is, shall we? A POJO (plain old java object) is just a class. This simple and quite good idea turned into a disaster with Hibernate where most of the objects started to be those gigantic pile of annotation crap but besides that didn't contain anything else. That's how I came to learn POJOs as a programming style: dump, idiotic and trivial classes. So it is not a POJO anymore - you might say. And you'd be right! That'd be a dumb Java Object (DJO) but unfortunately when you see code that has to do with EntityManager, pure Hibernate or some other ORM (which is very likely) this is what you are going to experience..

Back to the roots

Now that I am old enough to look back and see the mistakes I made I've decided to find a way to get back to the programming style where a "Company" has the ability to "hire" a "Candidate" which would spit out an "Employee" but at the same time to keep things as separated as they should be for the sole reason of testability.

Spring sucks - Grails to the rescue

It should come as no surprise that my first attempt to mimic a rich domain object was undertaken in Spring. I admit I'm no spring expert and I still believe it is a vastly bloated library but I do get the idea behind it (unlike myself 3 years ago). But stuff is very much complicated with pure Spring. And one thing in particular - dependency injection in domain classes.

When I say that Spring sucks it really means that "Spring sucks by comparison to Grails". It is really great that spring does all the wrapping and all that but making domain classes automatically injectable borders on the infinity...

The whole point to this exercise is to have an object model that is usable without GUI and still makes a whole lot of sense.

Candidate.groovy:
class Candidate {
    String firstName
    String lastName
}
Company.groovy:
class Company {
    String name

    def hiringService

    Employee hire(Candidate candidate) {
        return hiringService.hire(candidate)
    }
}
Employee.groovy:
class Employee {
    String firstName
    String lastName
}
Now how about this:
Employee employee = company.hire(candidate)
Isn't that nice? And it is so damn readable!

The best part of all this is that in Grails domain classes are by default eligible for dependency injection which means that the hiringService is injected automatically. And I really mean AUTOMATICALLY! This means that when you do "new Company(name: "Test")" the "hiringService is automatically injected!!! This is the best thing ever!!!

I strongly recommend you try to look at your application from within the code and see if you can read it as you'd read plain text. If not there's something wrong with either your eyes or the code before you. If I was a betting man I'd go with the latter one :)

Write clean code!

Saturday, March 31, 2012

Spring, MVC and Groovy

If you'd ever find yourself choosing the whole Grails stack only because it is sooo much easier to setup a project by comparison to pure Spring MVC I strongly recommend you try out the new Spring MVC on Groovy Maven archetype. That sucker punches in Spring in an easily selectable version, Groovy (1.8.6) and the groovy-eclipse compiler (also 1.8.6), JUnit and Spock, JavaMelody for monitoring - basically all you need to create a web application using Groovy, some IoC container, an MVC framework and test all that in a reasonable fashion.

http://www.testdriven.org/spring-groovy-web-archetype

Give it a shoot. If there's something you'd like to add to it then by all means - drop a note on GitHub.

Have a nice day!

Wednesday, March 21, 2012

Fatal dual mistake

http://bit.ly/GIbHyi
http://bit.ly/Hu5Ugl

The video material is in polish but it goes pretty much like this:

A group of anti-terrorists slams on doors of some people, kicks'em, slacks'em, drags'em over the floor, throws some grenades - basically they are dealing with some kind of bad guys. However they are "mistaken" so they say "Ups - sorry - never mind" and go one floor above. Then they do the same thing one floor above and again "Ups - sorry - never mind"... Third time's the charm, right? So there he is! The so goddamn dangerous car thief with a gun.

WHAT THE FUCK?!

Is anybody with a gun a terrorist these days?! Why don't you guys at the police go and RTFM what terrorism actually is! And if you ain't got a manual about it there's plenty of material on the net about it! A car thief ain't one of them - that much I can tell you. He's a thief, probably a murderer if he kills someone. But if that really is the case that owning a gun makes you a terrorists then those poor man and woman have been terrorized by terrorists because they did have goddamn guns. Big guns!!!

It's outrageous! I can't believe I'm living in a country where a car thief is a terrorist, where innocent people are being treated as criminals with an "Ups! Sorry! Nevermind" attitude afterwards! WHAT THE HELL IS GOING ON!?

Spock - the testing as it should be done

It's not very often when I get really excited about some piece of software. I'm not saying it doesn't happen because that wouldn't really be true but those times when I get the willies just after taking a brief look at something are really rare. Today is that day. Today I met Spock.

Well technically speaking, I met this guy, Luke Daley, on 33rd Degree conference when he gave a talk about Spock and how it differs from what you can do with regular JUnit tests.

OK. Enough of about the conference - let's see some code!


If you put this into an example.groovy file and execute it it'll actually run 2 unit tests. On the test we see the following sections:

  • We see that Spock is being grabbed (excluding Groovy since Spock tries to pull in some non-existent version)
  • We then create an Example Specification (that's how you should read the class declaration line)
  • We then say that we define that a sum of a and b is equal c (whatever those are)
  • Then some magic happens: we say that we expect that a + b is equal to c
  • And last but not least we define the set of data to create separate tests in a tabular, fitness-like form

Here's the output of that script (you should run it yourself to see it really do all the work!):
JUnit 4 Runner, Tests: 2, Failures: 0, Time: 15
Isn't it great? You spent about no time creating a few lines of code, got 2 test cases in return that execute in no time and are no match for JUnit's @Parametrized tests (which sucks big time by the way).

This is obviously the absolutely simplest example possible. There's a lot more to Spock than meets the eye here and the deeper you go there the more you wonder how's all that even possible. This is why I'll stick to Groovy as the most versatile, universal, non-problematic and powerful language ever created for the JVM.

Monday, March 19, 2012

Who broke the build massacre

Recently it's becoming more and more popular to create gadgets that "attack" or "shame" the one that broke the build on Jenkins. Some of them are really creative and funny!

This doesn't change the fact that it is outrageous and completely unprofessional that such "tools" are even needed. All that under the assumption that the person that did break the build (or even made it unstable) couldn't care less about it.

So my advice to you: do care about the stability of your work! Or else don't do software development - it's out of your league! You don't fit in!

Saturday, March 17, 2012

REST Service - the two words that make no sense

It's been bothering me for like ages and I finally decided to put a blog post about it. Let's start with a couple of definitions:

WebService - a way of servicing someone (like "giveMeSomething" sort of way) over the web

REST - Representational state transfer. We're transferring a representation of the state (of someone) - not servicing anybody.

To see why is this so damn important first let's see what classic SOAP web services look like in practice. They consist of 3 things:

1. WSDL / XSD (description)
2. Endpoint (URL)
3. XML (data)

In fact there's one most important thing to note: the "endpoint" is/has usually a verb: "go get some flowers", "fetch history", "make me pretty" - you get the idea, right?

Now on the other hand REST has nothing to do with verbs - REST deals with nouns: people, cars, addresses, gasoline, money... So how do you make a person give money for gasoline for their car to allow them to get to a certain address they are heading?

WebService: MySuperDuperWebService.makeHimPay(Double money)
REST      : GET /person/54623445/account/1/balance (get the current balance)
            PUT /person/54623445/account/1/balance + current balance

The difference here is you're not giving a REST place orders other than update, delete, retrieve.

So the next time you'll be tempted to say REST service please bear in mind there's no such thing. What is however named as a "REST service" is a URL, that responds (usually) to POST (cuz that's so damn easier to pass data to) that has no predefined schema (usually..) and that instead of giving you back XML hands you over JSON (possibly using JSONP). That my friends is a definition of a JSON Service and has nothing, absolutely nothing to do with REST whatsoever. Even worse is the term RESTful web service. My God is that a horrible thing to say. It seems like the service being referred to rusts in peace and should never ever be called.

Make the world better for a change!