Thinking in Ruby

So, I’m learning Ruby (it only took me a year to get started!).

I’m working my way through Programming Ruby and doing a few different scripts to see what it can and can’t do.

Most of it seems fairly straightforward stuff and I’m liking what I’m seeing for the most part.

One of the things that crops up from time to time in examples in books and online is something along these lines:

print total unless total.zero?

That’s it, the “unless construct”.

I’ve seen this before in Perl and I’ve always avoided using it – I personally find it unintuitive so I always write my code in the if x do y style.

Do x unless y has always seemed a little, errr, backwards.

Seeing it again in Ruby I again decided I’d avoid using it and carry on as I had before – then I began to wonder if I was simply imposing my “Java programming style” on to my Ruby code.

It’s an easy enough trap to fall into, much like early C++ programmers wrapping their C-style static methods up in a class and think they were doing OO.

Thinking about it, most of my Perl code is written in a similar style to my Java – I always apply “use strict” and enable warnings, always put code into methods, almost always have a main method etc.

But hold on, am I writing Perl in a Java style and thereby restricting my ability with the language, or am I simply applying sensible practices to my Perl code?

My Perl code never really extended much beyond occasional scripts to process photos so I have no clear answer to that.

I hope that my Ruby coding will move beyond that (possibly into the realm of Ruby on Rails) so as it does I’ll have to constantly be asking myself if I am thinking in Java or thinking in Ruby.

Unchecked Exceptions

On our new project at work we’re using JPA sitting on top of Hibernate.

I’ve used Hibernate several times now and am familiar with it.

JPA is mostly similar in use but there are a few gotchas.

One that got me the other day was what happens when you write a query that you expect to return a single result.

In Hibernate I’d have called query.uniqueResult();

The Javadoc for that method says:

Convenience method to return a single instance that matches the query, or null if the query returns no results.

So, the query either returns my object or null (an exception is thrown if my query returns more than one result – fair enough).

I had to do something similar in JPA-land so I looked at its Query class.

It offered a similarly named method: query.getSingleResult();.

All good, I wrote my code, compiled it and restarted my application server.

Unfortunately, when I ran the code, it fell over with a NoResultException.

For my particular query, there were no results in our test database.

Fine, my code can deal with that, but clearly the JPA method works quite differently from the Hibernate version.

Its Javadoc says:

Execute a SELECT query that returns a single result.

Returns:

the result

Throws:

NoResultException – if there is no result


So, unlike the Hibernate version this one will throw an exception if the query returns no results.

Hmmmm, I think I prefer the Hibernate version.

Of course, if it had thrown a checked exception my code would not have even compiled.

As it was it was just luck that the database had no results so I found the problem right away.

I’m not saying unchecked exceptions are bad, on the whole I prefer them.

But there’s a certain element of retraining your brain to no longer rely on the compiler to tell you that you’re dealing with all possible error conditions.

I know, I know, there wouldn’t be a problem if I’d read the Javadoc up front, but how many people can honestly say that they read the Javadoc for every new method the first time that they call it?

How to mentor programmers

I was reading an entry over at Raganwald where the author talks about managing projects.

He covers a list of things that one should try to do to ensure a project is a success.

One of his main points is that a tech lead should always know exactly what everyone is doing on a daily basis.

Whenever I’ve allowed the details of a project to escape me, I’ve failed. […] And I can tell you, whenever the details of a project have slipped from my grasp, the project has started to drift into trouble.

I’ve been the tech lead on many projects over the last 6 years or so but I’ve always stopped short of asking people what they are doing on a daily basis.

It has always struck me as a form of “micro-managing”, which is something that I’ve hated when I’ve been on the receiving end of it.

I should clarify though; I always know who is working in what area on a day to day basis (Jim is working on the email module for these two weeks), but I don’t necessarily know what specific task they are trying to achieve on a particular day (I don’t know if Jim is writing display code today, or back-end logic).

However, after reflecting on how this has worked on my projects, I have to conclude that my approach was wrong.

I should know what people are doing – I just need to find a balance between knowing what they are doing and getting on their nerves.

Clearly a balance can be found.

I make no apologies for now insisting on knowing exactly who, what, where, when, and why. There’s a big difference between being asked to explain your work in detail and being told how to do your job.

I’m not sure of the best way to handle getting to that level of detail though.

Daily meetings where everyone reports progress?

I find these to be a bit of a waste of time (especially in large teams) where you talk for 2 minutes then sit and listen to everyone else for 20 minutes.

Walking around and sitting down next to each person in turn (sort of like a Doctor doing his rounds)?

This is better for the team as they are only interrupted when I am talking to them.

I’ve done this before but never in a “tell me what code you are writing now” way.

I still think this might annoy me if I was on the receiving end of this.

Another way?

What about other tech lead people reading this, what works for you?

Or, if you’re on the receiving end of this, where exactly does that all-important line sit?

Do algorithms still matter?

I attended the BCS Mini-SPA event a few months back.

The premise of the mini SPA is as follows:

If you attended SPA2006 you might find that the miniSPA2006 programme allows you to catch up with sessions you didn’t select at the event.



The annual SPA conference (formerly known as OT) is where IT practitioners gather to reflect, exchange ideas and learn.

It also served as a convenient advert for next year’s full SPA event.

It was also free, had a free lunch and got me out of the office for a day, so it pretty much fulfilled all my criteria.

The structure of the day was 6 sessions, divided into two parallel streams.

I attended “Distributed workforces”, “Modelling with Views” and “A Good Read” but the one that really interested me was “A Good Read”.

This was a panel of five people who had each proposed a book to discuss. Each member of the panel then read each book so they could discuss it and give their views and insights.

The really interesting part for me was that someone proposed Programming Pearls by Jon Bentley.

I’ve owned a copy of this book for years but have yet to finish it, (it’s back on my “to read” list now though).

Everybody roundly praised the book but one of the members of the panel questioned whether we needed to know that level of detail when it comes to coding efficient algorithms – “wouldn’t it be simpler to throw more CPU and RAM at a problem?” they said.

Someone in the audience then countered that algorithm efficiency was relevant once again when programming Web-apps. They said something along the lines of “Wait until 100 people hit that page on your site“.

Sadly the session ran out of time at that point so no conclusion was reached.

My own belief is that you do need to know code at that level, especially if you write Web sites or other similar client/server apps with many concurrent client requests.

I’m not saying everyone should know the Quicksort algorithm inside out, but if you program in Java (for example) you should know the difference between a Vector, an ArrayList and a plain old array and when to use each.

I have had personal experience of a badly written for loop bringing down a Web site on launch day.

The for loop in itself wasn’t the worst code ever written by any means, but it was probably executed 30 to 40 times per individual home page hit.

Multiply that by a few dozen concurrent hits (it was a busy site) and any flaws in that code were mercilessly exposed.

Embarrassingly for me, it was my code. Oops.

Ever since that day I’ve been unable to forget that no amount of “CPU and RAM” (and we had a lot) will help if you don’t get your algorithms right in the first place.

Need a new Programming Language

I need to learn a new Programming Language.

This is for two reasons.

In my time as a programmer I’ve learned and used; Basic, Ada, C, C++, VB, Perl and Java.

So that’s 7 (5 if you merge Basic with VB and C with C++).

It’s a reasonable amount, a little on the small side.

But that list is only half the truth; most of those languages I’ve not touched in years, some I’m definitely never going to touch again (Ada!).

The only ones I now use in any form now are Java and Perl.

I use Java in my day job and to write things like this site, and I use Perl for the odd scripting task.

My first reason for needing a new language is a pragmatic one. I need to learn a new scripting language.

I need a new scripting language because every time I go to do something in Perl I find I have forgotten how to do one of:

a, list the files in a directory.

b, pass an array to a function.

c, iterate over an array.

d. all of the above.

This is because I find Perl’s syntax to be on the whole inconsistent and unintuitive.

So, I’ve had enough of Perl’s kooky ways and would like to learn something a little bit more “sane” (definition: consistent and intuitive syntax).

My second reason goes a little deeper.

I’ve been reading a few articles and blogs of late that in some way or another point out some problems with Java.

A Quick Tour of Ruby

Java doesn’t provide a utility method for opening a text file, reading or processing its lines, and closing the file. This is something you do pretty frequently.

— Steve Yegge

Can Your Programming Language Do This?

Java required you to create a whole object with a single method called a functor if you wanted to treat a function like a first class object.

— Joel Spolsky

What was interesting was that once I was over my initial denial of such heresy, I found myself mostly agreeing with what they had said.

The surprising part for me was that I had not consciously noticed these things myself – even though I now realise such things had annoyed me at the time.

The reason that they had not bubbled up to the level of consciousness was that I could not see beyond the Java language itself.

Something was awkward to do in Java (ever tried reading a file?) – well, that’s just the way Java is.

I couldn’t question it, because I was so deeply ingrained in the ways of Java, I could see no alternatives.

This worried me somewhat, what other concepts and ideas was I ignorant of due to my Java mindset?

Sometimes you need to take a step back and get a fresh perspective on things.

And what better way than to learn a new programming language.

I’m a busy guy though.

I can’t simply afford to take two weeks off just to learn a new language.

So, to be pragmatic (I’m a pragmatic guy too) I’m going to try to solve both of these problems with a single language.

So, I want a general purpose language that’s also good for scripting work.

My shortlist of languages is not long:

Python.

I’ve dabbled with Python.

It’s fun, quick, easy etc.

I’ve not done enough to know if it’s “sane” as defined above, it doesn’t seem as freaky as Perl though.

Ruby.

Ummm, everyone’s talking about it.

A friend of mine is learning it and he’s not swearing about it too much yet.

Apparently it’s mostly “sane”.

I’ve not completely decided yet, I’m leaning towards Ruby at the moment mind.

Anyone care to convince me either way, or suggest other languages I should be looking at?

Making Wiphi Good

BBC News has an article about the Slim Devices’ Transporter wi-fi music player.

As usual when I read about such things I tend to compare it to wiphi, the music player that I built.

It’s interesting to note the amount of work that would have to be done to get Wiphi up to the spec of something that is commercially available.

Visual appeal

My player looks attractive initially.

But it’s a bit chunky, and the green VFD display is not very readable.

Ideally it would be half that height and have a blue LCD display.

It would also have a nice responsive dial for controlling the volume.

It could also do with a nice logo.

Remote control

It doesn’t have a dedicated remote – it’s just configured to use any remote that works with LIRC.

Building a remote control from scratch is beyond my skills.

User interface

The overall user interface isn’t too bad, but it’s not slick.

There’s no indication of what is going on until the box has booted (takes a few minutes as it boots over the network).

The only interface is the remote control and the display panel at the front – which is a bit limiting. It needs an on-screen display on the TV ideally.

Software

I wrote the software myself – it’s essentially some “glue code” (written in Java) between MPD, LCDproc and LIRC.

If it goes wrong and crashes the only way to deal with it is to ssh in and look at log files.

It is MPD based though – I could set it up so that I could control it from a Web browser on another machine for example.

There’s no way to configure it beyond hacking XML files and restarting the box. That could be fixed – but would require a Web interface of course.

Conclusion

It would obviously need a lot of work to get it up to speed.

Also, the software work is far more likely to happen than the hardware work.

I’m not really good with hardware stuff, plus I’ve spent enough on kit for it already.

It’s an academic question anyway, I’m unlikey to give up my day job and build music players for a living.

Wiphi
Wiphi

Hibernate

I’ve just finished given this site a massive overhaul.

It’s got a new lick of paint, the old design had been around since the site launched in 2002 so a re-design was well overdue (a 4 year old design is ancient by Internet standards!).

It’s also had some major surgery under the hood too.

I’ve essentially ripped out the existing home-brewed persistence layer and replaced it with hibernate.

This really was as painful as it sounded and I wouldn’t really recommend it to anyone.

I did it for two reasons.

The main reason being that hibernate looks good on my CV. 🙂

The second reason was that my home-brew persistence layer was, errr, a bit shit.

My existing persistence layer was very simple, it read the entire database into its cache at start-up then never touched it again apart from writing new data (whereupon it refreshed its cache).

It’s a simple but effective approach.

However, it’s not particularly scalable and the site was feeling the strain as data grew.

Hibernate of course is a completely different animal.

Once I’d finished plumbing it in and got a working site I then set about making it efficient.

I didn’t want to replicate the old system of trying to cache everything, I wanted a balance between speed and memory usage.

This involved a lot of fine tuning, reading of the Hibernate manual and a copy of Hibernate In Action which I borrowed from work.

I think I have struck that balance now, the main sections of the site are read from the cache but the cache has limits set so that it doesn’t try to cache everything.

The other important thing I set up was a reporting page where I could view the sizes of the various caches to enable future tuning.

As for my thoughts on Hibernate – I’m reasonably happy with it.

When someone at work first told me about it they sang its praises to the limit and talked about its many powerful features and how it was so easy to work with.

They were sort of right, it is powerful, and does have some nice features. It’s also easy to work with. If you don’t care about performance.

If you want Hibernate to work efficiently and scale well you need to spend time learning how it works so that you can tune it properly.

I think I have done that to some degree but I’ll be monitoring cache performance over the coming weeks to see how it shapes up.

Programmer’s Diary (part four)

For my app, I had planned to cache some data from the Flickr API in a database (hence the need for Hibernate).

But I was eager to do a “proof of concept” thing and make sure everything I wanted to do could be done before I wrapped my head around Hibernate.

So I’ve decided to compromise by caching stuff in the session for now.

I had to tweak more Flickrj code to make sure everything I wanted to store in the session implemented Serializable though.

So, having got Flickrj working, and stuff cached in the session to ease the load on the Flickr API I started writing the interesting code that actually does stuff.

This part took less time than all the other fiddling around (always the way) so after only a few hours I had a simple prototype app that pretty much proved the concept.

There’s no design or look and feel work yet, just black text on a white background and no navigation other than a link to the home page.

So, it needs a bit of tarting up.

But it works, which is kind of fundamental really.

Next steps are to test everything and do some look and feel work.

At some point I’ll even send it live.

delicious powered linkblog

I’ve added a linkblog to my site, it’s on the right-hand sidebar of my blog homepage.

The nice thing about this linkblog (for me) is that it’s automatically loaded from del.icio.us.

To add something there I just add a URL to my del.icio.us bookmarks with a linkblog tag.

Within 30 minutes it appears on the blog here. Fully automated – nice.

The mechanism behind this is the same as the automatically updated Bloglines links further down the sidebar.

The only difference being the stylesheet as del.icio.us uses RDF and Bloglines uses OPML.

The other tweak I did in the stylesheet is to only show the ten newest links.

So, here’s the stylesheet that I made if it’s useful to anyone.

Programmer’s Diary (part three)

Robustification*

I want to ensure the app is pretty robust.

By which I mean that it shouldn’t crash (much).

This obviously involves writing proper code.

It also involves dealing with user input and the Flickr API in a sensible manner.

It’s reasonable to state that no user input should cause the app to crash.

To achieve this aim I will be treating all user input with extreme prejudice.

By user input I don’t just mean forms on the site.

I also mean any form of “URL hacking” too.

I also need to look at all the possible error conditions that the Flickr API can throw at me and deal with them gracefully.

Errors from Flickr will either be network errors (come back later when it’s better), invalid XML (will need to be investigated to see if I can work around it) or one of its published error codes (should be the easiest to deal with).

* Robustify is a word some shit contractor used in a CVS commit message after I slagged off his toString method.

I think it’s an awful word so therefore I enjoy using it in an ironic manner.

And now, for some gratuitous photos.

Empty shop
Empty shop
Candy Chinatown style
Candy Chinatown style