Thursday 11 December 2008

Functional Web Development with SISCWeb

Functional paradigms offer some innovative solutions to long standing Web development problems :-

1. Continuations fix the inversion of control problem caused by the stateless nature of HTTP. With continuations you can start a function and define some variables you can then 'call out' to the user i.e. send them a page. When the user responds, your function continues where it left off with all the variables still in scope. This means no need for hidden variables, sessions packed with transient data etc. Imagine now that your entire business process is now captured in one easy to read function, not spread over numerous java classes with varied XML structures.

2. Concurrency problems with poorly defined static variables leaking confidential data to the wrong customers are a thing of the past. Everything is passed on the stack as function arguments - there is no mutating state shared between requests. This also makes the application highly scaleable.

3. Security - the continuations approach means that the only way to link one page to another is via a continuation which is unique to each user. This means that data-driven attacks i.e. manipulating keys in forms to get access to data won't work - you can only access data within your continuation.

Try it with SISCWeb - a Scheme continuations based framework that runs on top of J2EE. This allows you to reuse all of your Java libraries and business components but benefit from a functional presentation layer. Also being Scheme based, it offers an embedded 'REPL' so you can build and change your application on the fly without restarting Tomcat or losing existing application state.

It's even better than Ruby on Rails but don't tell anyone - they'll all be using it.

Monday 10 November 2008

Manchester-on-Sea


I read recently on the BBC news website that both the Greenland and Antarctic ice sheets are melting three times faster than previously thought. If both of these ice sheets melted completely, sea levels would rise by around 70 metres !

Hopefully we are not in any immediate danger of this happening but it set me wondering what Britain would look like if sea levels rose by 70m.

I dug out some old Trailgauge code that I had and some elevation data that I had and generated a map.

Surprisingly, most of Scotland and Wales escape with minimal land loss. The effect for southern England, however, would be rather more catastrophic. You can see the full map here.

The good news for us northerners is that Manchester is transformed into an elegant seaside resort - perhaps it might be elegant if it wasn't overpopulated from all the refugees from the completely submerged Merseyside and Cheshire.

If you want to see what happens to your town or you want to model different sea levels rises, you can download the utility that I created and try it yourself. It's a Windows command-line utility that generates the map and lists any towns that match your search and gives you their new elevation above sea level, e.g. :-

C:\Data\sealevel>sealevel 70 manchester
Rendering "sealevel.bmp" for sea level = 70m.

Manchester Ship Canal (13500, 8821) submerged
Manchester (13856, 9004) = 5m
Godmanchester (15249, 7703) submerged

Saturday 8 November 2008

Have we lost our way ?

I've been using Java since 1995 so must be one of the most experienced Java developers around but despite years of my life invested in this technology, I'm increasingly thinking that this has been a mistake.

When I first used Java, it was for writing Applets which is what it was really designed for i.e. object-oriented with a ready set of libraries for GUI widgets (AWT) and everything built-in to download and execute within a web page. Having come from C where it was very hard to build a GUI, Java was a huge step forward. How ironic, therefore, that I've never seen another decent Java GUI apart from Eclipse in all these years and instead we now use Java almost exclusively server-side for writing web applications and business components.

The types of applications that we build are invariably web applications with a database backend i.e. replacements for the old client/server applications that we wrote 10 - 15 years ago using 4GL's like Oracle Forms, dBase, Ingres OpenRoad, Pick or in my case Dataflex.

Java is an object-oriented general purpose 3GL and we are using it to write apps that we would have written with a 4GL ! Is it any wonder that there are so many Java jobs advertised or that organisations are having to recruit armies of programmers in India just to get their work done ? Productivity has nosedived.

I used to write entire modules (say 10 use cases) in Dataflex in a couple days. Now it takes me 2 - 3 weeks in Java even using the best frameworks the world has to offer. And using these 'great' frameworks I end up with dozens of Java files all glued together with untidy XML - what great progress when I would have had one or two Dataflex source files !

OK Java is a general purpose langauge and I can do things in Java that I could never have done in Dataflex but then how often do I need to do this ? In Dataflex, I could have glued some C code in to do something unusual if necessary, painful but still a possibility.

I've recently completed a couple of projects in Ruby on Rails and it has really opened my eyes to this situation that we've gotton ourselves into. Rails offers a big increase in productivity over Java but partly because it's a script langauge so it saves on the edit/compile/deploy debug cycle. One nice thing is that there is no nasty XML, all the code is in your Ruby source files. This means that it can be syntax checked by your editor and everything reads together. I don't particularly like Ruby as I find the syntax untidy and not intuitive. Java has a simpler and more regular syntax so is easier to learn, but there is no denying that Ruby on Rails is more productive partly because Rails uses mixins and symbols to create a semi-Domain Specific Langauge for web apps.

So why do all these Java frameworks rely on XML ? Simple - XML is being used to supplement the language because it's not extendable. People are trying to create a Domain Specific Language DSL, a higher level language, dare I say it - a 4GL but the language won't let them. Instead they are forced to use a data structure (XML) to do this. This is bad because the data structure is not a language and can't contain any logic. So we end up with more and more convoluted and unfathomable XML and masses of disconnected Java classes e.g. Spring - nice try but wrong approach. We have lost our way.

So, how do we get out of this mess and recover our productivity ?

Having recently discovered Functional Programming through Scheme (a Lisp dialect), I can easily see how Scheme would readily support the creation of a high level DSL whilst offering the full power of the underlying language. There aren't many people using Scheme at the moment so not many frameworks out there but there is a growing following and it feels right.

Maybe Scala is the answer ? Scala is a replacement Object Oriented language for the JVM that also supports functional programming.

Thursday 6 November 2008

Luhn's algorithm in Scheme

I came across Luhn's algorithm yesterday whilst trying to resolve a work related issue and was surprised to find an implementation in Scheme for it on Andrew Dashin's blog.

Looking at Andrew's implementation, it struck me as being a not very scheme-like implementation because it used an iterator (do) and mutating variables (set!). This gave me an opportunity to test my new found Scheme skills :-

(define (luhn-check account-number)
(letrec (
(digit (lambda (str pos) (string->number (string (string-ref str pos)))))
(add-digits (lambda (str)
(+ (digit str 0) (if (> (string-length str) 1)(digit str 1) 0))
))
(luhn-pair (lambda (x)
(+ (digit account-number x)
(if (> x 0) (add-digits (number->string (* (digit account-number (- x 1)) 2))) 0)
(if (> x 1) (luhn-pair (- x 2)) 0))
)))
(luhn-pair (- (string-length account-number) 1))
)
)




It takes a string and returns a number which should be a multiple of 10 for a valid mod 10 check or a multiple of 11 for a valid mod 11 check.