Spring 3.1 not showing field errors using spring tags

After upgrading from Spring 3.1 we noted that many of our forms which have validation logic in the server side stopped displaying the error messages back to the original form submission.

Seems in certain circumstances when you create and return a new ModelAndView the BindingResult object is not appended to the Model automatically.

Adding the following lines is a quick work-around.  

Assuming method signature of:

public ModelAndView someMethod(@Valid User user, BindingResult result) {

if (result.hasErrors()) {

  ModelAndView modelAndView = new ModelAndView(PAGE);

  modelAndView.addAllObjects(result.getModel()); // added this line

  return modelAndView;

}

}

In our case we are not going to continue with this way of posting forms so we won’t invest more time in this.

I also noticed Spring has adopted the rails/grails ‘flash’ object which uses the session as a shared data store to pass messages or objects in between server side redirects, not a great pattern really.  Most Client MVC apps don’t need this complexity.

Learning new languages and their expressiveness

Syntax can be hard to learn. Although I have trouble reading lisp / clojure / haskell, I respect that for those who understand it is a simple pleasure to write code in.

The problem, as highlighted is not limited to computer languages. Often people who try to learn another language from English continually say “but why do they do it like that? English does it better.” Unfortunately, learning your first language (eg. mother tongue) is vastly different in approach from learning your second and third.

Many times learning the second is the hardest – why is this? You have to open up your cemented neural pathways to the idea that something can be expressed in a foreign way. Unlocking those pathways is hard, as it is like fighting an inertia you have had for a long time.

I learned Spanish, then French. After that I find my ability to learn languages is vastly easier. Why? Because I understand the concepts of tenses, gender, case, mood and respect the subtleties of each language.

I no longer learn concepts, I learn the implementations and the nuances of each language. The same now for most programming languages. In a language, I need to express conditionality. I need collections and looping, filtering
and transforming. I don’t need to learn these concepts again so I can pick up languages quicker and quicker as I learn more concepts.

I now see a trend towards less shared-state/synchronised realtime applications and a strengthening towards functional (map/reducible) programming and languages supporting shared-nothing and actor patterns. The interesting thing is that we used to think by sharing state an synchronising it was faster and more scalable than copying objects and using more memory. Now it is more scalable to do exactly that.

We now have the programming power, the memory and the disk space for almost anything – we are now stripping back our programming languages to make it ‘easier’ for us to write code. By easier I don’t mean we are writing simpler code. As Ola Bini points out in his blog programmers are wanting a higher level of expressiveness. To do this, we have to make some assumptions like we are not going to share and synchronise data/state everywhere in our application. Sure, it may happen on the database or in a 3rd party library like an aggregator, but we as expressive functional programmers don’t want the bother of covering all those horrid edge cases which are hard to debug and harder to reproduce. I’ve seen some shocking issues with ThreadLocal being used and abused. Which happens alot.

In my own code I have found issues where I have overridden variables or options to my own detriment. Each time thinking ‘if only I had put that into another (usually local) variable. I believe this is the biggest (current) pitfall in Java. It is too easy to modify the contents of objects from any library which has access to it. It almost sounds like a security flaw. It’s calling out saying “hey, you there. You want to use me somewhere else? Don’t clone me, oh no. Just change my state a bit. It’s alright. Surely.”. No.

I am finishing a book on Beginning Scala at the moment, which sounds interesting but mostly just copying the good things in Ruby and cramming it into Java-esque syntax. For these languages it is more important to learn the ‘idoms’ of the language, which is what should give you more expressiveness.

Ruby has alot of this. I am awed that this language was created over 15 years ago. It is a beautiful and expressive language. It reminds me of going from Windows background and learning Linux. You can do some really powerful stuff,
and you can really get yourself into trouble (metaclass?). Guard the gates of expressiveness and you will be treated with a powerful tool.

My next blog post will talk about JRuby in detail.

Agile… enough?

We’ve been doing a few inceptions in agile lately, and the question always comes up… how much work should be done for ‘this’ story? Well, you define your acceptance criteria, then you estimate your work based on that being completed and functional.

Honestly, there is so much more to it than that. There will always be acceptance criteria you miss out at the start, but perhaps you will cover the 80% of use cases. Perhaps you have to design a new pattern, so that other stories like the current one can be done quicker in the future, or perhaps you need to spike first because of the unknown estimate. Perhaps you need a 3rd party library to incorporate, so you need to wire that in and test it too.

Even in agile, I find myself asking, how much (or rather, how little) design is just enough? It really depends on the project. If the code is being rapidly developed, screens changing every day, database schema in flux, then perhaps you don’t want to do anything you don’t have to for that story, lest it be thrown away tomorrow and your efforts wasted.

Any developer hates the idea of their code being thrown away, it’s in our nature 🙂

Two areas I sometimes struggle with is a) only do enough to make the feature work and b) don’t think about things in the future. If you take b) as gospel, it means you will never refactor. I have seen developers go down this path. The code works, they iron out the bugs (with scant test cases) and it goes to production. They develop work quickly, but slowly but surely the code is polluted by duplication, wrong responsibilities in the wrong classes, or tight coupling due to bad design and/or lack of tests.

The begging question is how do you monitor this to stop it happening?

New Laptop – Mini Review Lenovo L420

Ok i’m getting rid of my MSI U270. A brave laptop, awesome size and good speed, but it didn’t cut it for development in jruby. Sorry MSI. I’ve scaled up to a ‘fuller’ PC with a 14″ screen.

null

Let’s get this straight – I got this PC cheap. AU$800 with a AU$300 gift voucher = AU$500. It’s an 8 GB, i5 2540 (2.6-3GHz) dual core laptop with a 320GB HDD. It is fast. Very fast. I was scanning around for a quad-core i7 but then decided this would be good enough and i’m very happy with my decision. It’s increased my productivity in a big way. The laptop itself is great, very rugged (daughter stepped on the screen and it didn’t get injured!). The keyboard is soft yet firm enough to be responsive. It comes with a trackpad AND a trackpoint, which i’ve never seen before. One gripe about the keyboard, the home, end and delete keys are up the top-right. I hate it. The MSI U270 had one of the best keyboard layouts i’ve ever used.

This laptop is a bit behind on features, no USB3, no HDMI. Do I care? No. Battery life? Not bad, though not excellent (what *is* it with laptops these days?) Lucky to get 3 hours out of it and that’s without playing music/videos. Hrm. It has a docking station in the bottom of it but who uses them.
It’s got a DVDRW drive which I must say is handy coming from notebooks which had tossed them away.

It works in Ubuntu LTS (10.04) which is awesome. I had to tweak the hibernate settings to get it to work, but thank god it does. For me hibernating the PC is a must-have feature.

All in all it’s a freakishly responsive PC for AU$500. It seems rare that you get a carry case with laptops these days, but this one lives in my backpack so that’s cool. The windows software comes with all sorts of customised stuff for Lenovo including ‘battery stretch’ which can give you another hour of power. Also worth mentioning is that the wireless on this is almost instant, like 3-5 seconds to reconnect which is great, especially if you are hibernating and waking it up all the time.

This laptop will be hard to beat. I can’t see myself upgrading for at least 1.5 years. Well done Lenovo!

Test Driven Development, not always to the extreme.

TDD is great stuff. It turns traditional programming on it’s head, back to front, upside down and says, rather than writing some code then doing a bunch of manual tests on it, tweaking it’s robustness as you do more testing, begin with the end state in mind.

I’m a firm believer of doing this with everything. It’s one of the 7 Habits of Effective People (Stephen R Covey), and it works in every aspect i’ve ever applied it. Just asking yourself ‘What am I trying to achieve?’ or ‘What’s important here?’ gives you instant focus, and it’s proven that when you’re aiming for a specific goal or outcome the human mind is more keenly focused and energised – some food for thought.

So TDD. Great. Do it all the time. Only ever do TDD. Right. Easier said than done, although ?-Unit testing frameworks have come a long way thanks to Kent Beck et. al. But TDD 100% of the time?

Although it’s possible, especially with greenfields projects setup the right way, I can’t imagine it being the norm. Kind of like pair programming. People say ‘you should pair 100% of the time’. I disagree. Like anything it’s finding a sweet spot that works for your team, your project, your culture. It may be 30% it may be 80%. It depends 🙂

Give me an example, I hear you ask, of where I *wouldn’t* want to use TDD? Two things come to mind. Startups and spikes (in agile). I almost consider them the same thing. Many startups begin with a horrible codebase. Why? Because most startups don’t start with best practices, they start as a spike on a PC somewhere.

Many startups readily admit this that after 3 months of production life, they have a technical debt. As the dust settles and their core market becomes clearer, they may shift to another codebase or refine their development to formally add tests, QA and processes into their development. It doesn’t start out that way!

How many tests are enough? J.B. Rainsberger alluded to the fact that you can write millions of tests, if they are not useful (testing frameworks not business logic, testing redundant code paths) then they will not serve you, you will serve them. Like anything else, tests add coupling to your code. It’s a fact. You need to accept this, and you should. There are only 2 situations where you should need to modify a test.

a) you have broken a test because you’ve modified code that tests executes such that it fails (you’ve created a regression bug).

b) you have modified the business logic on purpose because of a change in requirements. In this case, the test needs to be updated. This also allows you to verify you are in-fact changing the behaviour correctly.

You write tests to give you confidence about your changes. If you write good tests, they will not get in your way all the time.

I have been writing alot of tests in Jasmine lately. Jasmine is a beautiful DSL in javascript, similiar to RSpec, which allows you to write unit and integration in javascript which can interact with AJAX and the DOM. Before using this, we relied heavily on selenium tests to test the client side of the web application. TOO SLOW. TOO BRITTLE. Yes, selenium tests have their place, as the 10-20% of tests to test full functionality.

Jasmine really is awesome, 12 months ago I would never have imagined I can mock and stub AJAX calls, mock responses from the server side and test DOM interactions in multiple browsers. All of our tests run in Firefox, Chrome and IE. For this reason, I believe it is more important than selenium tests, since it works on a lower level than selenium, the tests execute orders of magnitude faster and they still physically run in a browser. Well done pivotal labs.

The future of web frameworks… and another Spring gripe…

We use Spring at work for our current project and it’s good and all… once the configuration is setup. Until then it is a bitch, because what used to be Java code is now pushed out to an XML file and, you guessed it, read into Java code at startup and parsed/validated. Yes we are using annotations so that makes it less painful but still the basic XML is awful.

Trying to do something which Spring has supported for 5 years, separately, doesn’t work.
You cannot define one set of URLs with basic authentication, and another set with form login authentication. Pitiful! Let me explain, you can do it if you upgrade to Spring 3.1 (RC3)
which isn’t quite a stable release yet. It has this awesome new feature which lets you define multiple sets of urls, with it’s own defined authentication manager. Wow, bleeding edge stuff..

Spring is definitely past it’s used by date, but will be around for a long time. I believe software development is in a transitional stage, with no ‘one size fits all’ progression (think of Play, RoR, Grails, Node.js) and there isn’t a clear successor out there for the traditional XMLRPC/REST/server side Java architecture. Maybe there shouldn’t be. But there doesn’t seem to be a good alternative in Java 😦 The best experience I have had to date in Java is using Grails, which leverages Spring, but hides 90% of the complexity through a much cleaner set of configuration files. You can still access everything in Spring if you wish, so it doesn’t ‘cut you off’ from functionality (as much as i’ve used it.)

I firmly believe software development will move towards a much lighter set of frameworks (think ruby:sinatra). More decoupled services with no session state will enable applications to be easier to scale and using API-driven development from the start will enable a clear and fast way to implement functional testing away from UI-driven tests. This will enable very efficient development.

Spring gripe and how I learn languages best

Most people like spring and what it brings to Java web applications. I don’t. Don’t get me wrong, I think it has made Java more popular, extended the servlet container spec and some key concepts were excellent for their time. Full credits.

But now, version 3.0 is showing ageing problems – simple things are complicated, tries to do everything ie. hook into security providers, different databases, different architectures of systems (API-based, traditional MVC apps, web 2.0 apps (ajax-based)). It just feels like it has tech-debt internally since sometimes to get simple things to work there is

a) no explicit documentation
b) other frustrated users (stackoverflow.com will confirm)

I feel this way with Hibernate sometimes, although hibernate tackles a much more specific problem (O/R mapping) which allows it to do one thing well vs. many things ok. This is also the same reason i’ve stopped buying text-books on languages. Sure it’s good for a deep-dive into the language and gives you more background etc. But if you already know the concepts (eg. blocks / functional programming / closures) then you are mainly learning the syntax of the language, and it’s idioms which define it.

Nowadays, I find it easier just to rip into the language, researching (eg. railscasts) and learning as you go. I find actually doing the development, feeling the pain and the joy when I learn how to cut 5 lines of code into 2 using a recommended idiom, distills it in my mind better. Kinesthetics unite.