designsuperbuild

Translating POODR to Javascript

Writing better OOP JavaScript

What the hell is POODR?

POODR is short for Practical Object-Oriented Design in Ruby, which is a fantastic book by Sandi Metz that teaches you how to write object-oriented code that won't make you cry when you have to maintain it. It's definitely one of the best programming books I've read and the lessons in it can be applied in pretty much any language .

If you just leave this post right now and go and buy a copy then my work is done - the rest of these posts are optional. You can get it from Amazon in print or on Kindle) or see the Sandi's product page for more buying options.

Why translate Practical Object Oriented Design in Ruby into Javascript?

Short answer: For fun & profit.

Long answer: JavaScript is weird at the minute. It feels like you've been driving around in a Smart car and, over the years, when you weren't looking, elves have quietly upgraded every element of the car until it's as powerful as a Ferrari.

Meanwhile you're still driving it like a Smart car.

I still occasionally find myself writing JavaScript like it's 1999, and from looking at other peoples code that seems to be a common fault. I fall into the bad old habits of:

  • Files with hundreds, sometimes thousands of lines
  • Loads of unrelated code in the same modules
  • Methods that seem to go on for ever
  • Objects that cover multiple responsibilities
  • Constructing the "Pyramid of Doom"
  • No tests at all!

The Node ecosystem is better but still doesn't escape bad practices altogether - it's great that the community is committed to the unix-style ethos of "small modules that just do one thing", but when you start putting those modules together in an app it can get just as messy.

Basically, some of my JavaScript is far from SOLID

It's especially weird as I don't write code like that in Ruby. If feels like I'm dragging along the cultural baggage of the years when JS didn't have a module system, or getters and setters, or higher order array methods like map and reduce. And it ran in the browser 99% of the time.

This is not the world we live in any more. We can require things, natively in Node and in the browser with Browserify, RequireJS or one of the ES6 module system backports. We have some best practices on object creation, composition and inheritance. We have tools to handle writing async code like promises. We even have getters and setters (if we're not targeting IE8).

Given that the ecosystem has drastically improved, and will get even better with the next version (ES6) of JS, it's time to go cold turkey on bad practice. It's doesn't take much effort to write good, clean, modular code - we just need to be rigorous in our application of the best practices we use in other languages.

So, that's why I'm translating the POODR into JS. Translating code from another language helps me think differently about JavaScript development. I'm (hopefully) looking at the clarity of Sandi's code and thinking of the best way of capturing that clarity in JavaScript. It's probably not going to be as expressive as it is in Ruby but we should be able to get close.

But what are you actually doing?

I'm going to go through the code samples and translate them into (fairly) idiomatic JavaScript, hopefully learning a few things on the way. I'm not going to go deeply into the meat of

Contact Us

Get in touch, we're always happy to discuss your challenges.

phone: 079 1234 2125
email: david@designsuperbuild.com