Archive

Archive for August, 2011

Making inheritable objects in JavaScript without objects

August 14th, 2011 4 comments

For a long time I’ve found the subject of the equivalence of closures and objects fascinating. The problem with creating objects using closures is that there isn’t a way to do inheritance (although you should always favour composition over inheritance anyway!). I thought it would be fun to see if it was possible to implement an object system in JavaScript with inheritance, without using any plain JavaScript objects.

To do this, I set myself two rules:

  1. Don’t create any plain old JavaScript objects (functions, arrays etc. are allowed)
  2. Don’t use the dot operator.

For that to work, I wrote some wrappers for the JavaScript methods I would need, like Array.prototype.slice.call (this becomes callSlice). Apart from those wrappers, there are no dots used anywhere. To dispatch methods based on a string name, instead of using an object (which would have been much easier but would have missed the point of the exercise) I’m passing around functions which dispatch methods using conditional constructs (if, else and switch).

Introducing funcobj!!!

Yeah I know, it’s a rubbish name. Anyway, the Github repo is here and I’ve got a running example on JsFiddle here.

Here’s some examples of how it works:

//call the doSomething method on myObject, with the argument 500
myObject('doSomething')(500);

//call the doSomethingElse method on the object's super object
myObject('doSomethingElse', true)();

To make an object like this, call objMaker with a function that defines and dispatches methods:

To inherit, pass a third argument to objMaker, which is the object to delegate to if the new object does not contain the called method (or: if the new object cannot respond to the message). Have a look at this long example in JsFiddle to see how that works:

This is stupid

Yeah, I know. It’s not meant to be used for anything. It’s probably hopelessly inefficient, and the syntax leaves a lot to be desired. But it does work, which was what I was going for. It might be a stupid idea, but it demonstrates the power of JavaScript closures. It also gives the possibility of dynamically responding to unknown messages, like in Ruby’s method_missing.

This is what objMaker looks like. There’s some fairly gnarly code in there, but if you can get your head around what’s doing what, you’ll hopefully learn something new about JavaScript!

Categories: Programming Tags: ,

Podcasts I listen to

August 13th, 2011 8 comments

I listen to a lot of podcasts. Any time I can find to listen to them I do – when I’m cooking, eating, washing up, walking to work, brushing my teeth, going shopping, running …

It’s not always easy to find the best podcasts, so I thought it would be helpful to list them here, along with a quick description, and some selected episodes where appropriate.

This list is in rough order of preference. I’m not saying the ones at the top are objectively better; just that I enjoy them more.

Drunk and Retired

One of my favourite podcasts. Coté and Charles Lowell just chatting, usually about software, but whatever else they fancy as well. Always funny, often enlightening.

Selected episodes

Back to Work

Merlin Mann (of 43 folders and Inbox Zero) and Dan Benjamin (of the Ruby world) chatting about creating, productivity, Buddhism and life. Hilarious and amazing.

Selected episodes

This Developer’s Life

An excellent podcast, unlike any other I listen to. Every episode follows a theme, with storytelling interviews around that theme, backed by good music. Can’t get enough of it.

Selected episodes

Ruby Rogues

This is a fairly new Ruby discussion podcast. It features all your favourite celebrity Ruby developers, like James Edward Gray II, Aaron Patterson, Peter Cooper, Avdi Grimm, Gregory Brown and more. It’s fascinating to hear these at-the-top-of-their-game developers talking about why they do things in a certain way. Each episode is a discussion around a specific topic.

Selected episodes

Software Engineering Radio

“The Podcast for Professional Software Developers”. An excellent, quite formal podcast about software engineering topics. It has a bit of a Java, patterns and model-driven architecture bias, but it’s second-to-none in terms of serious software engineering/development content. Most of the interviewers/editors are German, so it’s got a much more ‘European’ view on things (you may not think there’s any difference, but I think there is).

Selected episodes

The Pragmatic Podcast

This is usually interviews with Prag Prog authors, so can often come across a bit like a sales pitch, but occasionally there’s a real gem.

Selected episodes

A Minute With Brendan

Brendan Eich, JavaScript’s Daddy, talking about the present and future of JavaScript. Hear about JavaScript from the horse’s mouth.

Selected episodes

The Creative Coding Podcast

Fairly informal chats mostly about game development/general creative coding, with the odd interview thrown in.

Selected episodes

IT Conversations

I try to listen to every IT Conversations podcast, but there are a lot of them. It’s always interesting stuff, and it covers a wide range of topics in the tech world, from programming to biotech to robotics, etc. IT Conversations is actually a collection of other podcasts and conference recordings.

Selected episodes

Make All

Coté from Drunk and Retired, with his industry analyst hat on, interviewing programming people.

Selected episodes

ExplicitWeb

Three UK-based web developers talking about web development issues. It’s called ExplicitWeb because it has an Explicit rating on iTunes – be warned.

Selected episodes

The JavaScript Show

Jason Seifer and Peter Cooper with a slightly irreverent round-up of JavaScript news.

The Ruby Show

The Ruby equivalent of The JavaScript show.

Web Dev Radio

A podcast about web development from Michael Kimsal. Partly an interview show with various web developers, partly Michael just talking about stuff.

Selected episodes

The Basement Coders

Informal, Java-heavy discussion about software development. Don’t let the Java put you off though – I’ve never written a line of Java, but I still find it interesting.

Selected episodes

The Pipeline

Dan Benjamin interviewing various trendy tech people.

Selected episodes

The Changelog

“Open Source moves fast. Keep up.” New and cool stuff in open source.

Tech Weekly

The Guardian’s tech podcast, presented by Aleks Krotoski. A wide-ranging look at the tech industry.


There’s a lot here, but I can just about keep up. I never listen to podcasts when I’m not doing something else, so I don’t see it as a waste of time. In fact, I almost see it as a waste of time when I’m doing something mindless (like washing up, running etc.) and I’m not listening to podcasts. Maybe that’s a bit obsessive, but I think it’s a great way of learning masses of new stuff for free. Anyway, I hope you find something interesting in here. Enjoy!

Categories: Miscellaneous Tags: ,

Understanding the Ruby object model

August 7th, 2011 24 comments

Not a very long time ago, I found the Ruby object model very confusing. Part of my problem was that I was trying to model the relationships as a directed acyclic graph, whereas in reality, the model contains cycles. I’m guessing that others have found this confusing, so I’m going attempt to explain how it works in a way that my past self would have understood! Note that this is a basic explanation, and glosses over higher-level concepts like the eigenclass/metaclass (I’ll leave that for a later post).

Different things in Ruby

There are just a few things that exist in Ruby that you need to understand to really grok its object model.

Objects

Everything is an object. You can’t get very far into Ruby-land without hearing that phrase. It’s true though, everything in Ruby is an object. The interesting thing is how those objects are linked together and classified.

Classes

All objects are instances of a class. There are three special classes that are the key to understanding the Ruby object model: Object, Module and Class. More on them later.

Relations

Like I said before, it’s the links between the different objects that explain how the Ruby object model works.

Instance of/class

All objects have a class – they are instances of that class. That means that whenever you have a class, you can call .new on it to get an instance. And whenever you have an instance, you can call .class to get its class. This seems obvious, but it can get confusing when you remember that everything is an object, and so everything is an instance of a class.

Superclass/subclass

All classes have a link to a superclass. If you don’t explicitly inherit from another class in a class definition, that class will inherit directly from the Object class.

How they fit together

Here’s some code to play with:

class Animal
end

class Dog < Animal
end

fido = Dog.new

Once you know what the pieces and the relations are, it’s fairly straightforward to piece them together. All classes are an instance of Class. That includes the class Class, so there’s a circular relation there (which isn’t a problem).

Dog will basically have the following inheritance hierarchy (the superclass links):

Dog < Animal < Object < BasicObject

(Ruby 1.9 introduced BasicObject as Object‘s superclass.)

The Class class has the following inheritance hierarchy:

Class < Module < Object < BasicObject

Anything that’s a class (so BasicObject, Object, Module, Class, Animal and Dog here) has Class as its class – they’re all instances of Class. Any other objects have their own classes. So fido above will have a class of Dog, 3 has a class of Fixnum, “an awesome string” has a class of String, etc. All those classes inherit from Object ultimately (although classes like Fixnum have their own complex inheritance hierarchies).

To finish things off, here’s a little diagram of what I’ve just described, showing all the class and superclass links.

The Ruby object model

Categories: Programming Tags: ,

Reflections from a Kindle owner

August 5th, 2011 7 comments

Back in April I got a Kindle for my birthday. It’s changed my tech-life massively, and I thought I’d write a few words on that here, from a developer’s perspective.

(Disclaimer: the Amazon links in this blog post are Amazon Affiliate links, which means I get a percentage of any sales resulting from you following them. If you don’t feel I deserve that percentage, then feel free to go to amazon.co.uk or amazon.com [non-affiliate links] and I’m sure Amazon will be happy to help you find a Kindle.)

Books

I now read most books on my Kindle. In many ways it’s a better experience than a paper book – it’s much lighter than the average programming book, I can rest it on my leg without having to hold it open, turning pages is easier, and with a cover with built-in light I can read in the dark (e.g. I can read in bed without waking up my girlfriend).

It does have its downsides in comparison to a paper book as well – navigating between different spots isn’t as seamless/natural, even with hyperlinks; code listings often wrap (I usually view these in landscape mode, but even then it’s often not enough); it’s significantly more valuable than a book, so I need to take a lot more care with it; some nuances of book formatting don’t always make it through the conversion; and although it’s got a massive battery life, that’s still a consideration.

When I’m buying a new book online now though, I don’t hesitate to get the Kindle version (if it’s available). The benefits far outweigh the downsides.

Magazines

I’ve been wanting to read the Prag Pub magazines for a long time now, but I don’t like to sit in my computer and leaf through PDFs (and I certainly didn’t want to print them all out onto paper). Luckily, they’re all available for free download in Kindle format, so over the past few months I’ve gradually been catching up with the back issues. It’s a great magazine and I’d highly recommend it, whatever your e-reader of choice.

Blogs and essays

Instapaper is an essential tool for any Kindle owner. It gives you a “Read Later” bookmarklet, which enables you to mark any article to be read later. You can then tell Instapaper to deliver a collection of your unread articles to your Kindle regularly, for free! This has made such a difference to me. I come across so much good material online that I want to read, through Google Reader and Twitter, but when I’m at the computer I don’t just want to sit there reading blog after blog. Having those interesting articles delivered to my Kindle automatically means I don’t need to think at all – I can just read them when I feel like it.

In the same vein, I use Instapaper to catch up on older essays, such as Paul Graham’s massive collection or Steve Yegge’s drunken blog rants, that I’ve been wanting to read for a long time.

Free 3G

What a tongue-twister. It’s amazing though – if you get the 3G version, Amazon give you free web access, wherever you are. FREE. It still astounds me. It’s not particularly fast, and looks pretty ugly, but it works. For looking stuff up on Wikipedia it’s great.

Conclusion

If you’ve got an e-reader, or something e-reader-like, e.g. an iPhone/iPad/Android something, I’m not going to tell you you need a Kindle as well. Maybe you do, maybe you don’t. But for those that don’t have any way of reading electronic material away from a computer, I’d highly recommend getting one. It might change your life :)

Categories: Miscellaneous Tags: