Categories
Uncategorized

Java Leads To Great Libraries

Tim Bray wrote Having Done Java about how writing Java leads to better quality libraries. Needless to say, I disagree. I originally sent this by mail to Tim Bray, but I don’t think that there’s any reason to not publish it here as well.

I’m afraid that I’m going to have to disagree with you on this one. I’ve relatively little experience with Java, but I have been called upon to do some web things. And the standard library makes very, very angry when compared to CPAN[1]. There are a couple of aspects to this:

  • Firstly, everything seems to have been vastly overengineered. Simplicity got thrown out of the window from what I can see. I blogged about one incident (PUTting an URL), but there have been others, frequently involving the bizarre servlets API.
  • Secondly, the strong type system leads to weirdness. This isn’t necessarily Java’s fault, more the people who use it seeing no other way to do things. Again, I’m forced to compare with Perl here. I’m
    thinking in particular of URI.pm, which happily handles https, and http URLs. However, they’re completely different classes in Java. And if you try to feed in one to a method which expects the other, it’s no-go.

Ok, I’ll give in. I’m less moaning about the quality as to the bizarre design which I perceive. But it certainly feels like it should be a factor in any discussion of quality (nebulous as that might be).

1 I’ll admit, however, that there is a huge amount of crap in CPAN too. Anybody remember Meta.pm?

Categories
Uncategorized

London JavaScript Night

The wonderful Greg McCarroll of london.pm is organising London JavaScript Night. If this is anything like the Perl tech meets, I expect it to be well run. I’m already looking forwards to both the full-length talks on that page, and I can’t wait to find out what the lightning talks have in store.

Categories
Uncategorized

Greasemonkey Hacks

I’ve just gotten a copy of Greasemonkey Hacks and I’m working my way through it. The book itself is great. It’s a really good introduction to greasemonkey and what you can do with it. Eventually, I’m hoping to may my bank usable in firefox.

The only quibble that I have is the code samples. Many of them (and they appears to be the ones written by Mark Pilgrim, the author) are really difficult to read, because they idioms are wrong. For example, nearly every for loop I have seen looks like this:

  for (var i = arTableRows.length - 1; i > = 0; i--) {
    ...
  }

Which is walking through the rows of a table, backwards. Why backwards? I have no idea. I would expect it to look more like this:

  for (var i = 0; i < arTableRows.length; i++) {
    ...
  }

Aside from a marginal efficiency gain (which smacks of micro-optimisation), I don’t see what the benefit is.

And “arTableRows” is another quibble with the code. It’s filled with hungarian notation. Great if you work at Microsoft, but unreadable to the rest of the world.

Categories
Uncategorized

Subclassing with Prototype

I really like Ajax.InPlaceEditor from script.aculo.us. But it doesn’t quite do what I need. Nonetheless, bending it to my will ought to be fairly simple if I can just override a method or two.

This involved getting very familiar with Prototype’s Class.create and Object.extend.

After spending a while looking at the source for those two, my big book of JavaScript, and google, I was still flummoxed. Until I scrolled down a bit in controls.js (where Ajax.InPlaceEditor) lives. I saw this example of inheritance:

  Ajax.InPlaceCollectionEditor = Class.create();
  Object.extend(Ajax.InPlaceCollectionEditor.prototype, Ajax.InPlaceEditor.prototype);
  Object.extend(Ajax.InPlaceCollectionEditor.prototype, { ... });

That is:

  1. Create a new class (or constructor function to be accurate).
  2. Copy all the template functions from the source class into the destination class.
  3. Start adding our own functions into the class.

This is a good start! But there’s a tricky problem that I need to overcome: I wanted to override the constructor and run some code after the base class constructor. There’s no easy way to do this, because there is no link between the new class and the inherited one after you have called Object.extend(). So, you just have to be very explicit about exactly what you want to do. This is what I ended up with:

  var MyInPlaceEditor = Class.create();
  Object.extend(MyInPlaceEditor.prototype, Ajax.InPlaceEditor.prototype)
  Object.extend(MyInPlaceEditor.prototype, {
      initialize: function(element, url, options) {
          Ajax.InPlaceEditor.prototype.initialize.apply(this, arguments);
          // Don't respond to events on the main element of interest.
          // Only listen to events on the externalControl.
          Event.stopObserving(this.element, 'click', this.onclickListener);
          Event.stopObserving(this.element, 'mouseover', this.mouseoverListener);
          Event.stopObserving(this.element, 'mouseout', this.mouseoutListener);
      }
  });

It’s that line at the start of initialize() that’s the interesting one. It grabs the initialize() from Ajax.InPlaceEditor, and then uses apply() to call it using the new MyInPlaceEditor object as this. So it gets treated like it’s part of MyInPlaceEditor, even though it’s really defined in Ajax.InPlaceEditor.

So now I’m in a position to start overriding the other bits that I care about.

Categories
Uncategorized

Chanctonbury Ring

Yesterday I cycled out to Devils Dyke, and along the South Downs Way towards Chanctonbury Ring. The south downs way is remarkably hilly. I was quite pooped by the time I got there. So I went back down through Steyning and on to Henfield over part of the downs link path. From there, it was a small matter of going back to Poynings and along the Saddlescombe road to Brighton.

In total, it took around 4 hours. I would have been slightly quicker if my chain hadn’t snapped in the last 15 minutes of the ride and I had to walk back. Naturally, the first thing that I did this morning was go and purchase a spare link…

Categories
Uncategorized

Typo Upgrade

I’ve finally bit the bullet and upgraded Typo to the trunk, so I can get everything up to Rails 1.1. Unfortunately, this was quite a painful process…

Normally, I track bits of software I might want to hack on in a vendor branch in subversion, and use svn_load_dirs.pl to update to new releases. This works extremely well with wordpress at work, for instance.

Sadly, svn_load_dirs.pl couldn’t cope with the typo trunk, as a directory got replaced by a symlink. Ooops. I tried working around it, but eventually, it was easier to give up and start afresh, redoing the changes I’d already made in subversion. I suppose it would have been a lot easier with SVK.

Unfortunately, a number of things haven’t caught up with the latest typo changes. In particular, the origami theme that I was using needs some loving attention. So it’s back to the default theme, “azure” for the moment, until I can spend some time on it.

Also, I couldn’t figure out how to make the google sitemaps patch work, so that’s another “gone for now” feature.

Please let me know if you spot anything else…

Update: Comments now enabled. Sorry about that.

Update#2: I’ve now fixed the comments feed a bit. It turns out that one of the migrations missed out on creating guids for all the comments…

  % ruby script/console production
  >> comments = Comment.find_all
  >> comments.reject { |c| c.guid }.each { |c| c.create_guid; c.save }
Categories
Uncategorized

My Sysadmin Toolbox

After seeing lots of these at Linux.com recently, I thought I’d try to come up with my own list. I used to be a sysadmin (I’m now a programmer), and I’ve long felt that you really a good set of tools (and to know how to use them) in order to be most productive.

  • zsh

    I spend the vast majority of my time at a command line. Zsh ensures I make best use of my time. If you’ve used bash, you might think you know what completion is—press the tab key and it fills out file names for you. But zsh takes it to a whole new level. Not only does it complete file names, but also users, hostnames, option flags, environment variables, PIDs and more. On top of that, it does it in a context-sensitive manner. So if you type in “chown ” and press TAB, it starts completing usernames. Type in a space and another TAB and it starts completing file names again.

    On top of that, it allows partial completion. If I type in /u/l/e/r/ and press TAB, It gets expanded to /usr/local/etc/rc.d/. This is phenomenally useful.

    But it’s not just completion that zsh is good at. It’s also good at globbing. That’s turning wildcards into filenames. In addition to the usual forms of globbing, zsh can glob recursively. So if I want to look for “foobar” in all my files (but not directories), I can do:

      % grep foobar **/*(.)
    

    The “**/*” is the recursive glob, and the “(.)” limits it to files and not directories. You can also limit by user, by timestamp and a few other things.

    This is just covering the surface of zsh. Suffice to say that if you make heavy use of the command line, investing some time in learning zsh will make you vastly more productive.

  • screen

    This was mentioned on a few of the other lists as well. GNU Screen on the face of it doesn’t do anything. You just end up with another command line when you first run it. But the beauty of it is that if you get disconnected, you can just log back in, run screen -d -r and pick up exactly where you left off. For me, this is ideal, given the flakiness of my home wireless network. But you might want to use it so you can shut down your PC at night and pick up where you left off in the morning.

    On top of that, screen lets you run multiple command lines at once inside it, log the output and cut’n’paste between them. Think of it as a safety harness for your work.

  • rsync

    Rsync is one of the closer things to magic that’s around. It’s a simple file copying utility. But the clever bit is that it only copies the things that have changed. This doesn’t sound like much until you’ve edited several files in a collection which is 200Mb and needs to be on another box. When rsync tells you it’s finished and only transferred 10Kb instead of 200Mb, you’ll really come to appreciate it.

    If you’re still using tar/gzip or zip to create an archive to ship to another computer, stop wasting your time and disk space. Learn to use rsync and your life will be far more pleasant.

  • OpenSSH

    Thankfully, ssh is pretty ubiquitous these days. It seems to have mostly worked in its mission to eliminate telnet. But it has a few tricks that are worth knowing about.

    First, the agent. One of the nice things about ssh is that it doesn’t have to rely on sending passwords around. Instead, you can use public key authentication. However, even typing in your passphrase can get pretty tedious after a while for every connection. Enter the ssh-agent. Just stick eval `ssh-agent` in your startup scripts and then run ssh-add once. After that, you don’t get asked for your passphrase any more. The only caveat is that now you really need to lock your screen when you walk away from it.

    Next are the tunnels. Ssh is able to create network “tunnels” in and out of otherwise secure locations. This is very handy for creating ad-hoc networks. For example, I’m allowed to ssh into my work, but not to anything else. Yet, I can use RDP to connect to my workstation by running this command:

      % ssh -L3389:myworkstation:3389 firewall.mywork.com
      % rdesktop localhost
    

    That says: listen on port 3389, and any connections that come in, forward them on to myworkstation port 3389 from the other side of the ssh connection to firewall.mywork.com.

    If you’re on windows, check out PuTTY. It’s got all the features, but wrapped up in a nice GUI interface.

  • lsof / pfiles / sockstat

    Lsof (List Open Files) is one of the first diagnostic tools that I reach for when I need to understand something. The purpose is simple: it tells you what files (and network connections) a process has open. If you’re wondering where a process is logging to, this might be able to tell you. Conversely, it can also tell you which processes have a particular file open (usually a lock file).

    On Solaris, the pfiles command is similiar.

    On a related note, FreeBSD also has the very, very useful sockstat command, which lists all open sockets and what processes hold them open. The useful bit is that it does this without needing to be root (unlike Linux’s netstat -anp).

  • strace / truss / ktrace

    These are the second diagnostic tool that I reach for when something’s not right. Unix operating systems have a very clear distinction between userland and kernel, and this tool shows all the points where a program crosses between the two (makes a system call). If you really want to know how a program is interacting with its environment, these tools will tell you. It’s godo for answering questions like:

    • What files is this process opening and closing?
    • What connections to the network are being made?
    • What’s been read in by this program?
  • multitail

    A recent addition to my toolbox. It’s like tail -f, except that it looks at more than one file at once. It also does highlighting of search terms. Dead handy.

  • curl

    Most of the stuff I do these days involves the web. Curl is a fantastic little tool for inspecting the web from the command line. It covers all the protocols you need, and can dump out any information about the transaction. Want to issue a PUT request to an SSL server, verifying the certificate and specifing basic auth? It’s got you covered.

  • vim

    Everybody needs a good editor. Vim isn’t the only choice, but it’s pretty likely to be available wherever you go. And once you’ve started learning how to use it properly, you won’t go back. In particular, I can’t live without ^P and ^N for doing completion inside the file you’re editing.

  • sudo

    If you’re still using su, then you need help. Sudo allows you to dole out root access on a much more granular level and you get proper logging of who did what. If you haven’t looked at the manual recently, then check out sudo -e for editing a file as another user. It ensures that you get your regular editor (vim or Emacs) instead of the incredibly unhelpful ed that root probably defaults to.

  • subversion

    Everybody needs version control. If you’re editing files, stick them in subversion. You won’t regret it. Particularly when you need to see what those files looked like 6 months ago.

  • mutt

    Every now and again, you need to deal with mailbox files. Mutt is a great choice for that, thanks to its mini language for filtering mail. Need delete all mail over 10 days old sent by cron@somehost? Not a problem. Even if you don’t use it on a regular basis, it’s worth getting familiar with.

  • gdb

    Yes, this is a programmers tool. But it’s worth knowing a tiny amount about if you’re a sysadmin as well. What for? It lets you see why something dumped core. If you find a core file, then do file core to see what program left it behind and then gdb /path/to/program core. When you’re inside gdb, type in where and it will (most of the time) give you a stack trace, showing what it was doing at the time of the crash. This is normally a big help in trying to figure out what went wrong.

    You can also use gdb to find out what a running program is doing by specifing a PID instead of a corefile.

  • perl / python / ruby

    If you perform the same series of actions more than a couple of times, you should consider investing some time in automating the process. Shell scripts are handy, but can only go so far. Learning one of these languages will give you a really powerful ability to write your own sysadmin toolbox.

  • mediawiki

    Documentation. Everybody hates doing it. Why not make it as easy as possible? A wiki is the answer to that, and mediawiki is one of the better pieces of wiki software out there. It’s pretty simple to get going (although it does depend on MySQL).

    Remember: getting it documented is first priority. Once the information is in the wiki, it can be restructured later. So long as the information is there, it will be searchable (and hence useful).

Hmmm, that’s a bit more than the 10 they wanted. But it’s a large portion of my regular toolkit. Hopefully there’s something useful for other people in there as well…

Categories
Uncategorized

Cross Browser JavaScript Still Hard

Recently, I’ve been doing a reasonable amount of playing around with prototype and scriptaculous. They’re both really great libraries that give you loads of features and take a lot of trouble out of your hands. But they certainly don’t absolve you from cross browser compatibility testing. Whilst the libraries themselves are pretty much cross browser compatible, your own code likely isn’t until proved otherwise…

The discoveries I have made that lead to this:

  • MSIE doesn’t like trailing commas. e.g. this throws up an a JScript error in Internet Explorer.
  {
    foo: 1,
    bar: 2,
  }
  • I’m still tracking this one down, but I started getting errors when I did Element.show('non-existent-id') in Internet Explorer, but not FireFox. Why doesn’t Firefox complain? I think it should do.
    • Oh all right, it does complain. I missed the warning.

Doubtless this is just the beginning of a list.

Of course, I managed to waste plenty of time arriving at these problems, because Internet Explorer has such incredibly lacklustre development tools. For example, to get any idea at all of where the error in your code is1, you have to install the Microsoft Script Debugger. This is very primitive, but it will show you where the error actually occurred, and gives you a fighting chance of examining some variables in the locality2.

In my case, the script debugger didn’t even work properly when first installed. After reading somewhere that Visual Studio surperceded the Script Debugger, I remembered an experiment I’d performed a while back. It turned out that something called MDM had gottened installed. Deleting it and all associated registry entries made the Script Debugger spring back into life. Finally!

Another hint worth noting about the Script Debugger: if you want it to run in a non-administrator account, you need to put yourself in the “Debugger Users” group.

1 The error that pops up in Internet Explorer gives you a line number without a filename which is none too helpful.

2 Hint: you have to use alert() to see any useful output in the command window, then flick back to the browser to see the popup.

Categories
Uncategorized

Apple+-

Well, I’m both annoyed and happy at Apple.

Good news first: 10.4.6 fixes my problem with zsh. I would be curious to find out what the fix actually was…

On the bad news, my replacement mouse still hasn’t turned up. My wireless mouse stopped working in February. Apple promised they’d ship me a new one (after spending an hour on the phone to India). I watched the tracker on their web site. The blasted courier company failed to deliver to my workplace, claiming that they’d tried three times. I never saw them, nor did anybody else in my office. So I phoned Apple again, only spending half an hour on the phone to India this time. They again promised that I’d be shipped a new mouse. That was several weeks ago.

Seriously, Apple make some nice kit, but their phone support is atrocious.