Javascript: its actually pretty cool

So, I used to think that javascript was evil and bad, and made for brittle websites that only worked in one browser. That danger is still present, but its not really the fault of javascript. Sticking to the ECMA and W3C DOM standards should avoid most of the pitfalls.

I’m more interested in Javascript as a language. Its a prototype-based OO language. Prototype based languages don’t have the concept of classes per se, you define types by instantiating objects and then attaching function definitions. Once your prototype object has all the behaviour you want, you can use it as a template to instantiate other objects of the same type. Functions can be defined either as global, or bolted to an object as methods. Functions can also be directly referenced as variables. Function parameters can either be named explicitly, or referenced by indexing into the arguments array. So although a function may state that it takes 2 arguments by saying ‘function foo(bar, baz)’, there’s nothing to stop you calling it with 20 arguments.

What is also very cool is that you can make HTTP requests directly in javascript, and refresh parts of the page independently. This makes it possible to build very interactive portal applications that can update the screen asynchronously without an obvious browser refresh.

The netWindows open source project has a bunch of very useful stuff, including a standalone signals & slots implementation for javascript, which is very handy for building event-driven pages.

Avi Bryant has graciously published some javascript that can selectively replace chunks of HTML based on an asynchronous server call.

The Oddpost guys even built a full fledged 3-pane email application (with drag and drop) inside a web browser. Shame they’re no longer taking subscriptions. I want one!

Peelable UI’s

On pretty much every project, the subject of whether its feasible to perform unit testing (and indeed, TDD) at or near the user interface comes up. Over the last couple of projects the design that I find easiest to work has looked something like this:

public class SimpleView : ISimpleView {

private TextBox txtFirstname;
private Label message;
private Button okButton;

public string Firstname {
get { return txtFirstname.Text; }
set { txtFirstname.Text = value; }
}
public string Message {
set { message.Text = value; }
}

private void okButton_Clicked(object sender, EventArgs e)
{
controller.OkClicked();
}
}

public interface ISimpleView {
string Firstname { get; set; }
string Message { set; }
}

public class SimpleController {
private ISimpleView view;
public SimpleController(ISimpleView view) {
this.view = view;
}
public void OkClicked() {
string greeting = "Hello, " + view.Firstname + "!";
view.Message = greeting;
}
}

The general pattern is that any operation that requires the system to take action should be a method call to the controller, and that all the values from the view should be exposed on the interface. Widgets should not leak from the view into the controller. This approach allows for testing with NMock or JMock, as well as keeping the view classes so thin and logic-free that it is a simple task to peel off and replace with an alternative implementation (swapping thick client for web front end for example), or a test harness.

Its not perfectionism its pragmatism

Been thinking a lot about OO just recently, as you do. A lot of the practices I use get questioned when I demonstrate them, exactly as I would have done had the situation been reversed. Many of the questions are of the form ‘Isn’t this OO for OO’s sake?’, or ‘That seems like a lot of work, couldn’t we just use a Getter?’. What’s interesting is that the question of whether something is a lot of work depends very much on your perspective. I’m fairly lazy (aka. ‘pragmatic’) so I seek to find neat ways of reducing effort, but my perspective is usually a fairly wide one, so I’m usually thinking about ways of reducing effort over the life of a project and not just the code I’m typing at 2.30 on a tuesday. Its often the start of an interesting discussion when stuff I’m doing to reduce effort is queried as too much work. It is also be a hard thing to explain, when it does take demonstrably longer to write some code that doesn’t break encapsulation, as opposed to just adding a Getter. I’ve seen what happens to a system when all the objects in it have had their boundaries eroded by the addition of getters, and I remember wishing that there was another way to do it, which ultimately led me to considering the following approach, which I’ve worded as a ‘challenge’:

  • Can you take any class in your system and instantiate it easily in a test harness? -Credit to Michael Feathers for this one.
  • Can you write a system where none of the classes have Getters (or public properties in .net)?
  • Do all your objects have only one constructor?
  • Can you write a system where none of your objects take primitives (including string) as parameters to their public methods?
  • Where a class accepts multiple parameters in its constructor, can you write it in such a way that none of them are primitives (or strings)?
  • Can you do the above using TDD?

I’m not saying I do all this all the time, but it is a goal I try to keep in mind when developing. As an approach it leads to many more fine grained classes with names like ‘FirstName’, ‘Age’ etc that might otherwise have been represented with strings and ints, but it is a fascinating way to develop, and leads to some very interesting code. For example, lets say I want to take an object and render it out as HTML. Usually I immediately think of adding getters for all the fields that needed to be written. If you’re avoiding getters, then you end up with something more like ‘public void WriteTo(IRenderer)’. Which is also really easy to write a test case for, either with a mock object, or by having the test case implement the interface itself.

Collating Thoughts

My blog has atrophied recently, as I’ve been busy doing stuff which has left me with little time to write about it.

I can’t see any imminent pick up in activity, as I’m due to fly out to the US for several months, in a week’s time. This will be an adventure and I suspect the trend of doing things rather than writing about things will continue.

However, not wishing to totally drop the ball, here are the ideas, topics, cool things etc. that are buzzing around my head at the moment.

  • Start a travelog? Can I combine my pending travels and interest in photography with blogmapper’s technology to create a snail trail of my movements linked to photos of the places I go to?
  • Staying in touch with friends. iChat AV is compatible with AOL, so I’m planning to get my home pc set up with a webcam so my flatmate and I can chat over the interweb. Finally, a reason to own an iSight.
  • Start writing my ‘technical issues I’ve seen on every project I’ve worked on’ magnum opus.
  • Camino. Will probably replace Safari as my browser of choice, as Safari occasionally barfs and gets into infinite loops on sites with complex redirects.
  • Write down my crazy idea that Smalltalk could make a comeback if it had a sufficiently credible brand backing it up. Hint: Apple
  • Keep a close eye on WordPress as a potential competitor to Moveable Type

Searchable Images

EXIF, IPTC, Dublin-core, huh?

Crash-only Systems

Finally got around to reading this paper about crash-only software. What a great idea. Given that fatal errors that require a component be restarted are by their nature unanticipated and almost impossible to design out, why not make crash/reboot the default mode of operation? If a system is designed to be safely arbitrarily crashed and restarted, then the number of fail/recovery scenarios is reduced to one.

The paper takes a number of good ideas for building robust distributed systems, and turns them up to 11. Go read it.

Who needs a messaging system?

One of my colleagues, Steve, spoke to me the other day about using SMTP for application integration.

Its something that I’ve thought about from time to time, and it just seems to make sense. Here is a protocol that has been around for years, is battle-tested, and is used by millions of systems all over the world to reliably transfer messages. In case you haven’t guessed yet, SMTP is the protocol used for email. Why spend a fortune on enterprise message brokers, when you could download exim or postfix (or sendmail if you’re a masochist). Most linux, BSD or unix boxes will probably have one of these installed already.

SMTP is point to point, so what if we want pub-sub? Handily, there is a protocol called NNTP which has been around about as long as SMTP. This is the protocol that USENET (aka newsgroups) uses.

I definitely think that there is much our industry could learn from technologies invented 20 or more years ago.