Java, Programming

Factory of Factories

I am convinced that Patterns are the new Anti-Patterns. As the joke runs, the GoF patterns show you how to write better C++.

One Pattern/Anti-Pattern that drives me up the wall are Factories. Or as the might often be better known: Constructors. After all if a Factory only constructs one type of thing it is no better abstraction that just putting the construction logic in the damn constructor.

Effective Java makes a better suggestion for Java coding that constructors like inheritance should be used sparingly. Instead of creating a whole new class for constructing instances just create static factory methods inside the class with appropriate names for the kind of construction being done.

This is neater, keeps related code closer and applies in a lot more situations. My rule when evaluating code is that an implementation of Factory actually has to create more than one type of object in a way that cannot be replicated by Dependency Injection to have value.

Not much code passes that test, not even my previous attempts to please the structural aesthetic of enterprise architects. In the past I have been as guilty of pattern abuse as anyone.

Standard
Programming

Baby steps in Scala

Okay so following on from the User Group meeting I have started the exercise of porting a Java application to Scala. I’ve chosen Jay’s Secure Diary (or just JDiary for short) and I have commited some code to Launchpad.

So then, learnings so far… Well this is all being done via the command-line and a text editor as I have the Eclipse plugin has just not wanted to play ball with me. It’s also a tremendous pain in the ass to get stable updates of the plugin. I’m getting support from the Scala Tools forum but it feels a curious mix of features. A lot of things are very polished but the basic behaviour isn’t solid.

I was mightly relieved when I got Gant working and taking some of the strain of the build. It is surprisingly easy to get going. I based mine on this blog post. The harder part is referencing the Scala Home Directory which I have hardcoded for now and need to sort out.

I also gave Scala GUI a go but gave up quite quickly because it seems very opinionated about how you should be doing guis and while I like some of the ideas it didn’t allow me to use any of my existing Swing knowledge and didn’t have any easy documentation for how you are supposed to use it. In Profligacy for JRuby and Groovy you are allowed to mix the two approaches a lot more freely.

I also spent a ridiculous amount of time trying to implement the ActionListener trait/interface until I hit on the fact I was meant to be using the override key word. The compiler error complains that the parameter (ActionEvent in this case) is not defined, that’s true but it’s really not helping you understand what is wrong with the code.

I also spent too long fiddling around with Launchpad, but that’s another story for another post.

After investing eight hours or so in the effort I can be happy that my code is looking relatively clean compared to the original but I have to say that I was enjoying Scala more when I was sticking to just Scala.

Standard
Java, London, Programming

London Scala User Group Meeting

Had a good meeting (I thought anyway) at the TW offices that featured a talk from Jamie Webb introducing Scala and then a quick dojo. Scala has a much terser syntax than Java but I managed to do my classic thing of mixing my languages and putting my constructs in the wrong place. The other thing that doing rather discussing taught me is that Scala’s constructor syntax is much better defined than Java with the default constructor going into the declaration of the class. That’s the kind of improvement on Java that I really like in Java. It seems to distill all these lessons you’ve been learning if you write good Java code.

The pub after the event also saw a lot of discussion, there was quite a bit of discussion about testing and dynamic versus static typing. Personally I have decided to follow Neal Ford and reject the issue. To me both Ruby and Scala are low ceremony, high essence languages that choose different approaches to address the same issue. As such I don’t think there is any contradiction in liking and wanting to learn both languages.

Similarly I love compile time checking but I’m not giving up on testing. It’s a synergistic practice and I want to get the benefits from TDD/BDD/Tests as Specification. To me there is some irony that if I do have a test suite then actually I have more flexibility about my choice of languages. Not testing means you get much higher value from strong compile checking and therefore something like Scala is going to deliver much more benefit quickly. At the end of the day though I don’t want to have treat my tests as the gamekeeper of my wild code. I do expect the language and the compiler to give some structure and help to the development process.

Despite (because of?) my mistakes  want to do some more and I think Aaron (the founder of the group) suggested converting an existing Java program to Scala. That seems pretty sensible and I have an old Java program that I use a lot hanging around that needs some TLC.

If you are interested in Scala and in London I would recommend coming along to this meetings because you have the chance to meet and talk to everyone who is really engaged in Scala in the city. And to the guy who came all the way from Belgium; I salute you, you are a real hero!

Standard
Groovy, Java, Programming, Scripting, Web Applications

Towards Groovier Projects

My latest project has witnessed an influx of Groovy. The project buildfile is run by Gant, there are Groovlets providing lightweight pages but it is in the test folders that the Groovy has made its most insidious advances.

Markup builder makes fragments of HTML to test, Groovy’s built-in Sql is beginning to setup and check the results of data operations at a functional level. Soon I plan to implement a Groovy Struts 2 result and then it will begin to replace Freemarker.

Standard
Programming, Python

Setting up a Mercurial work environment

So today I had a frustrating experience trying to get a Mercurial environment going for the current project I am working on. I am convinced for the kind of work we are doing the distributed branch model is going to be the right solution and I had been using a local Mercurial instance during my tech spiking earlier on the product.

Having built Python and Mercurial on the server and created a Mercurial user with the SSH keys of the individual developers I thought it would be easy sailing this morning. Not a bit of it and all of the problems stemmed from the lack of by default support for SSH in Windows.

Like most people I use the Putty Tool Suite for my SSH needs on windows. However simply aliasing putty to ssh doesn’t work. There are some key command switches that are different (including the trivial but annoying -P instead of -p for port).

Coming from a Java Subversion background I am used to having a portable library to take care of my ssh protocol needs. I also use OSX for my home development and that obviously provides a UNIX ssh command-line implementation, hence hassle-free SSH based source control.

The command line tool turned out to be the root of all my problems, both Eclipse and NetBeans Mercurial plugins don’t provide an implementation of the Mercurial client, instead they delegate it to the Mercurial client on the host OS, that in turns delegates ssh protocol invocations to the command line SSH.

The solution is simple once you know it, in the Mercurial.ini file you can alias ssh to Putty’s plink executable. There is in fact an example in the Mercurial book. Better still you don’t have to specify the key used if you are using Pageant.

However getting the first station to work was incredibly hard work. I even downloaded Cygwin just to get a more normal Unix ssh but by that point I had put a typo into the ssh alias and was getting a very weird error whenever I invoked Mercurial and I’d lost the forest in the trees.

At one stage I even gave Bazaar a go, hoping that SFTP might cure all my ills. However Bazaar uses Paramiko for it’s SSH support and on Windows that was failing due to its inability to find an OS source of entropy.

Server-side Python also let me down a bit as I was having an issue with the zlib module and after similar experiences with Ruby I knew that this would be because I must have compiled Python before zlib. Despite cleaning and re-configuring Python it still didn’t build the zlib module and in then end I had to go and run configure within the zlib module manually prior to a full Python build. This is exactly the same issue I have had with Ruby, what is the problem with this library?

Once you’ve done it once Mercurial repositories are actually easier to setup and manage than SVN (and that itself is pretty easy once you’ve done it a few times) and if you are working in a UNIX environment then they are extremely compelling.

However on Windows you are currently going to either do a lot of preparatory reading or be ready to set aside six hours to prevent frustration. In my case it probably didn’t help that I am also the one selling the utility of Mercurial. The pioneers always have the arrows in their backs; on the other hand they also get to better places long before anyone else.

Standard
Java, Programming, Web Applications

Notes on NetBeans 6.1

I am working on an exciting web prototype project at the moment and I am using NetBeans 6.1 rather than my usual Eclipse environment. Overall I am very happy with the experience. The good points are:

  • NetBeans seamlessly integrated with my existing Tomcat instance, configured it correctly and makes it easy to build and deploy my application making my build-compile-deploy cycle as quick as it has ever been
  • The Javascript support is excellent and allowed to discover the JQuery API with no problems
  • It has been easy to define libraries and have them brought in to the project
  • The built-in XSLT validation and one-click transform of a test file is really powerful and very simple to use
  • Associating a web project with different deployment servers is easy
  • The GUIs for writing Java Web App configuration files is great
  • Managing NetBean Modules is easy and the interface is good
  • NetBeans 6.1 is fast, really noticably faster than 6.0.1

That is a whole lot of positive, so what hasn’t been so good?

  • Code templates complete on a different keystroke to code completion. The seperation doesn’t make sense to me. Both things are about completing what I have typed.
  • The window of opportunity for completing a code template is too narrow. If you don’t complete while typing the template stub then it doesn’t work.
  • When entering the parameters of a generated method you have to hit return too much. If the auto-generated code has aready correctly assigned variables to the parameter position then I’m done. At the moment though you have to hit return for each parameter otherwise the code generator gets in a tizzy and often opens brackets on the line below the current method.
  • Really gnarly HTML can take a long time to parse for the navigator windows and the slowdown is noticeable across the whole IDE. XML blazes by comparision.
  • There’s a lot of magic going on to make the project work automagically and there isn’t a good “raw” representation of the project that would make it easy to work from CI and the command-line.

Essentially most of these problems are to do with the interface being too fussy and in some cases too specialised. However I do thing I am getting the benefit of the integrated development environment and while it may not be as fast as Rails it is some of the fastest Java web development I have ever done.

Standard
Programming, ThoughtWorks

Aftermath of a Geek Night

Last night was my first Geek Night at ThoughtWorks. There have been many Geek Nights but this was the first under the management of Paul Nasrat and myself.

I was pretty happy with the evening and I think both speakers and audience came away pretty happy too, which is great. Paul and I will be reviewing the feedback from the event before the next one on the 12th of June.

I learned about State machine support in JMock, something I have never used despite having used the framework a lot. The audience also go to see how Nat and Steve think JMock tests should look. It made me realise that I have tended to set assertions in my mocks rather than using stubs in the past. Well no more! Allowing is my new friend.

During the dojo I also got to do a segment on a classic refactoring of a block of code to a method, to a method in a private class, to a private class implementing an interface, to a collaborator decoupled by an interface. It is a classic technique (and you will be able to see it in the dojo code when it gets posted this weekend) and seeing it put to use by Nat, who is a great developer and someone who really loves to code, was very cool. It was an experience I genuinely felt privileged to be part of and I hope the other people in the pairs felt the same.

I would also hope that it illustrates how mocking should lead to changes in design of objects rather than “pickling” their behaviour at a certain point in time.

Standard
Programming

The Pain of Rails

There are a lot of cool applications written in Rails but I often find that the configuration and setup is such a pain that you quickly lose the desire to run them. There’s only so much moaning about some library interface or missing gem or obscure data driver error you can take.

This kind of flakiness (particularly around the database area) has been driving me back to Java applications and alternatives such as Grails. It has also made me look at other frameworks than Rails. Since JRuby 1.1 was released JRuby has often been easier and faster to use than MRI Ruby.

Yesterday I had a kind of typical example of the kind of thing I find a pain. I had setup a new Ruby instance on Hardy Heron and was going to give Mephisto a go with SQLite3. I could do with an easy to use CMS and Ola Bini had already Tweeted that the setup had been easy under JRuby.

So having downloaded a few gems and the code, copied and set the database configuration I give the server a run and get an error about not being able to include the “initializer” file. Now this foxed me and I double-checked the documentation on installing Mephisto and tried to install an Initializer gem if there was one and I had a look at the boot.rb and saw that it was indeed trying to require initializer.

The problem is that when I had initially installed my gems I had forgot to install Rails itself. Nowhere was there a simply message telling me that though! Instead you have to kind of know that Initializer is the first script that Rails runs (and indeed that it is part of Rails).

There is a lot of Rails problems that I am sure are simple for people who work with it all the time but often I find myself coming with a task I want to do and coming away with a tussle with Rails instead.

Standard
Programming, Ruby

Synthesis: decreasing tests, increasing reliability

George Malamidis gave a talk at Ruby Tuesday last week about Synthesis which is a kind of mock verification framework. It was the second time I have heard George talk about Synthesis but I really didn’t get it first time (partly due to a pointless debate at the first talk about functional testing (interesting but irrelevant)). A different environment and it just zinged into place for me.

Synthesis ensures that if you mock an interaction between two collaborators you also have a non-mocked test for the method calls in the collaboration. In short it shows when you have stopped unit testing and started testing your mock framework.

Like most people at the talk I could see immediately how that would lead to less worrying about mocking and the good news is that Java port is being worked on.

I wonder if the issue will arise in the Geek Night on Wednesday?

Standard
Programming, ThoughtWorks

ThoughtWorks Code Assignments

When you enter the ThoughtWorks recruitment process you are asked to code a solution to one of three problems.

Googling for the answer is clever in the sense they can be hard. It is also really stupid in that the code will be reviewed by at least two people who if they decide that your application will be taken forward will interview about your code and why you have chosen to implement your solution in the way you have.

You won’t be able to tell them that the code is the way it is because you copied it off someone else! So, write it yourself! The feedback you get will be more all the more useful if you code things in your own way. Otherwise the feedback will be relevant only to the person who wrote the code.

One piece of help I can give is that there are no trick questions or mistakes in the assignment paper. If you think you have found an error in the problem statement you have probably misunderstood the problem.

Standard