I really couldn’t believe this when I heard it but the video is worth watching. It still seems insane but Steve Yegge makes a good case and it sounds like the kind of technical project that anyone would love tackling.
Category Archives: Scripting
Operator Overloading? Nada!
So today I got a perfect example of why operator overloading is such a nightmare.
What do you think this piece of Ruby means?
a << @b
The short answer is that it means nada. It will depend on both a and b and even if you learn what one instance of the operator means it will serve as no guide to identical operators for different types. This one operator resulted in a bug every time it was used and I don’t think it represents any value over a more verbose method call.
Groovy Gridbag Example
Before Christmas I decided to give the Groovy Swing Builder a go since I am in the market for a language that makes creating small utilities in Java easier. Groovy is now Groovy 1.5, fully aligned with the Java 1.5 release. The new release sees lots of improvements all over the place. The Groovy Console has had a substantial revision and is now a really good way of experimenting with more complicated scripts (previously I only really used it to run snippets of code). The text colouring and decoration really works and the previous problems with massive stack traces obscuring actual issues seem to have been fixed. Really there are just two things that are needed to make it perfect, the first is line numbers so you can relate error messages back to the script you are working on and auto-completion. I wouldn’t mind having tabbed editors in the console as well but I can live without it.
What hasn’t changed is that documentation is generally woeful and examples and tutorials are hard to find. Most of the Swing examples I could find really used small numbers of components and simple Layout Managers. I have tried to create a more complex example using Grid Bag and based around some existing Swing code I found.
It took me a lot longer than I thought it would to create this code. Part of this is just not knowing how to tackle things, Groovy allows you to set attributes within the constructor with the interesting syntax property: value. However it is hard to know when you should use this or when the interaction should occur within the braces. After getting a working example going there is only one major mystery to me here. If I do not put the table inside the scroll pane then I lose the header columns on the table in the second tab. I really cannot see why that should be and if anyone more versed in the language can help me I would be grateful.
Groovy or JRuby?
Martin Fowler blogged about the question a couple of days ago and ever since I have pondering that maybe it is not really the right question to ask.
I currently toodle between Jython, JRuby and Groovy for various reasons and I am an expert in none. The interesting thing I have found is that it is hard to pick one and just focus on that. To some extent they overlap heavily: they are all cross-platfrom, they are all dynamic, they all integrate with Java API stack I’ve committed to memory.
The first thing to say is that I am interested in scripting languages for prototyping and admin style scripting. I have never used Rails and the Grails data-model means that you need a specific kind of project to work on. If you want to use a particular product and that is only on one platform then that kind of makes your decision for you.
Each language has its own strengths, from my point of view I would categorise them in the following way. Jython has Python’s readability and solid language design, JRuby has Sun’s support, excellent community contributed library code and is very dynamic, Groovy is mini-Java, so it’s easy to learn and most importantly it has a functioning interactive console.
The last point might seem a bit weird, what about jirb and jython‘s interactive mode? Well Martin makes a very important point in his post about the purpose of these ports. Both JRuby and Jython aim to stay faithful to their source languages and be able to run code from their parent C implementations while expanding the API by accessing the Java libraries. Groovy on the other hand stays close to Java syntax and is the only one of the three that allows you to cut and paste code from a regular Java application and then play around with it in an interactive session. That is a very powerful and compelling feature.
Almost all the Groovy I do either comes from wanting to leverage or understand a piece of Java code.
All my Jython work on the other hand is about wanting to automate administration or manual tasks in a clear and concise fashion. Python’s dynamic data structures help, but so does zxJDBC the Jython specific database library that mixes DBI with JDBC to create a highly portable but simple database connectivity solution with no boilerplate!
JRuby on the other hand is something that only really comes up because Alpha Geeks love it. The syntax is gnarled and there is a significant learning curve before someone from a Java background can get “The Ruby Way” of things. The new integration of JRuby into NetBeans though makes developing in the language a comparative snap and I would suspect that JRuby will be a valid choice of application development language alongside Java now. The choice will be driven by the problems you are trying to solve not because one language is inherently “better” than the other.
Groovy Experiences
So I tried to implement one of my little Swing Utility ideas this weekend in Groovy. I need the utility and I thought it would be good to give the scripting language a go beyond simply using it as an interactive console onto the Java SE API. This is what I have learnt so far.
Well first off having an IDE helps. I used Smultron on OSX as it is an open source editor. Usually I use Komodo Edit for scripting and man have I become lazy about closing my brackets and braces. Probably the number one issue with my syntax.
I like to have a book to hand when learning a new language and in this case I have been using Groovy In Action. It is a good book and certainly seems to fulfill the role I have when learning something new. It is concise enough to allow me to jump to a particular topic when stuck on a particular point but also seems to have been written in enough of a structured narrative to give a context to what is going on when you are more particularly stumped. I would recommend it if you are looking for something similar.
Groovy doesn’t have the easiest error messages to understand. For example this foxed me for a bit:
links.each {
link -> if(link.startId = anId) { items.add(getItemById(link.endId)) }
}
The error message is:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, /Users/rrees/Documents/dev/groovy/MindField.groovy: 37: expecting ')', found '=' @ line 37, column 28.
link -> if(link.startId = anId) { items.add(getItemById(link.endId)) }
However the error is that I am trying to assign a value in an illegal location. It’s that old classic of = versus ==.
When an error blows up the stack trace is huge and this can be extremely distracting when, for example, a unit test assertion is failing. Since I am on the command line at the moment this is unavoidable but it also happens on the console and rapidly you need to clear the console before the next run. I think the console should definitely collapse the stack trace so you see the Message of the exception and then expand the stack trace when you want to see it. Often the Assertion Message is all you need.
Talking of unit tests, I needed them more than normal because while Groovy is very similar to Java there are enough differences to make it important that you double-check your assumptions. Not using equals once was a bit weird for example. Also Groovy arrays seem to differ from the Collections API safe-zone. GArrays are like Lists but don’t seem to support the List interface, no isEmpty for example. list.get(0) doesn’t get you the first element, list.getAt(0) or just list[0] does. Most collection interaction seems to be modelled after Ruby in that you have to get with the closures to do stuff. For some reason I found it difficult to get what I wanted out of the closures in one pass and I tended to get something working with the unit test and then shorten the closure to something concise.
Ruby closures did not seem this hard but maybe it was because the whole language is new on me. As Groovy allows you to bring over the Java idioms you are used to perhaps there was impedance between something that felt familiar but actually required a different approach.
My closing experience was another positive, Groovy’s XML and Swing Builder objects felt incredibly powerful and easy to use. The syntax is tough to grasp but being able to play around with it in the console helped. Swing Builder in particular feels like it could be the way you should make all Java Swing apps.
Overall it is a pretty positive experience. In term of the competitors it felt much more familiar than JRuby but the result was not as neat as Jython scripts tend to feel.
Python Functional Programming
Something that is relatively new to me in Python is the lambda function that allows you to do a bit of functional programming. Today I created a Dictionary of functions and it all Just Worked. It reminded me a bit of C function pointers, Ruby closures and Java anonymous classes but it seemed more logical and natural than all of them.
Starting Ruby and Python
This weekend I had a chance to start some Ruby and Python programming. My favourite scripting language to date is Perl and in terms of Java scripting I do actually like Groovy (although it has rapidly fallen out of favour with the Java crowd). Out of the two I have done currently done more complex stuff with Ruby but one thing is pretty immediate and that is that Python is very much on the back foot when it come to OOP scripting.
That’s not necessarily a bad thing though as actually scripting tends to be procedural and my Python script seemed to do a similar amount of work without the involvement of OO code. If I wanted something done quickly I would probably go for Python in the same way I go for Perl now. However if I thought something had legs and needed to be supported and expanded I would definitely go with Ruby.
One thing that both languages seem to have issues with is their big libraries. Most of my time with both languages was spent looking up how to perform some action. I was certain that neither of the languages required me to roll my own but I felt the situation was a lot like C and C++. C, like Perl, has the advantage of being syntactically very compact. You have to learn very little to know the language completely. Of course that means it lacks a certain power that comes from building on the capabilities of the language.
Perl answers this problem with modules which are a good idea as you only have to learn about standard libraries in little module sized chunks that match the problem you’re working on.
Python is kind of similar, in the sense there isn’t a lot in the core, but different in that, like C++ (and Java), you need to know your standard library if you are really going to get stuff done effectively.
Ruby is an altogether different beast. To get anything done you have to grapple with what feels, from my Java-centric point of view, like a very arbitrary API that is attached to every object. If you know the capabilities of a type then you have access to a lot of power. If you don’t, then you are swearing, scratching your head and reaching for the API description in the Pickaxe book.
One key thing that I feel is holding me back is the lack of variable typing. Having dynamically typed variables is cool, unless of course I know that I only want one type of variable (say an Integer). Maybe the code reflection and auto-completion features of Java are influencing me here but I do think it is helpful to be able to specify a type for a variable and then have the programmer’s editor or IDE be able to determine the API available to you.
Oh and yes, I might be making this complaint because I have already been caught out by the lack of automatic conversion between strings and numbers.
Overall my opinion at the moment is that Python is like Perl only cleaner and more legible. I think it would be really good for protoyping an application with a big team because there is a lot more consistency in the syntax and the style.
However Ruby’s object-orientation does mean that it would be an obvious choice for trying out OO Java ideas. Particularly if something like JRuby helped bring the benefits of both languages together (as well as making the most of my heavy investment in memorising huge chunks of the Java API).