Nick Sutterer — Trailblazer and a New Architecture for Rails
Nick Sutterer is a software developer and conference speaker based in Sydney, author of the Leanpub book Trailblazer: A New Architecture for Rails. In this interview with Len Epp and Peter Armstrong, Nick explains what Trailblazer is, the Rails problems it solves, how it grew out of a decade of open-source gems, and his experience publishing on Leanpub.
Len Epp: Nick, I usually like to start these interviews by asking people for their origin story. How did you get interested in software?
Nick Sutterer: Oh, it all started with computer games. I was one of the lucky kids who had access to computers really early, so we’re talking about the early 80s. I was playing black and white computer games and I really got intrigued by the idea of making my own game. That’s how I got into programming. Of course I’ve never managed to write a game but I started with playing around with HyperCard on really old Apples and moved on C++, 15 years later ended up with Ruby. That was a nice development.
Len: I found a talk online where you said that you wouldn’t be a programmer if it wasn’t for open source. Could you explain why that is?
Nick: As I said, I started programming following this idea of writing computer games, and I was maybe 10 years old and you are not gonna write a computer game when you’re 10 years old. So I kept playing with lots of languages. I started Perl and I got intrigued by this idea of object-oriented programming, encapsulation, even though I did not have any idea what real encapsulation looks like in an actual program. I was also reading TCP/IP Illustrated and this taught me a lot about layering — this layer is not supposed to know about the other layer — and I saw the similarity to object-oriented programming. I started playing with a widget framework written in Perl, and that was when I had my first feedback from actual users or people trying it.
By that time I was still working as a Perl slash PHP web dev, working on other people’s products only, so the open source stuff gave me some new direction and motivation to keep working in the web environment. And it’s absolutely true, if I wouldn’t have discovered Ruby and all the problems in Rails that I was trying to solve, I probably would have ended up in a different discipline, probably like a game developer working for some awesome company and working on Assassin’s Creed 500. Who knows if that would have been the better choice.
As soon as I got into Ruby, I started working on open source and people started actually using it. That was amazing, or it’s still amazing. I got feedback, I got people telling me, “hey I found your project and look at it,” and I was like, “oh this person is so much better than I am,” and this is how I kept doing what I do for the last 10 years.
Len: About Trailblazer, could you explain a little bit about what it is and why you’ve built it, what problem is it solving?
Nick: If you have a superficial look at it, Trailblazer only adds technical complexity to a framework that is already complex, like Rails or Sinatra or whatever. The goal is to add abstraction layers on top of the existing primitive — primitive not in a bad way, but it is primitive — MVC stack. What Trailblazer says is writing or architecting complex web applications, and that’s what we all do every day, is a complicated task, and having two or three buckets for all our code — we don’t put code into views, so we only have M and C, two buckets — for complex apps is not enough. We need more layers to structure code, to know where to put certain requirements, to know where to put implementations, and to make it also a bit more straightforward how to test specific functionality.
What Trailblazer does is it says use Rails or Hanami or Sinatra or whatever as what I call an infrastructure framework. It gives us all the rake tasks, generators, routing, HTTP endpoints also known as controllers, and on top of that introduce some additional abstractions like deserialization, validation, callback objects and all this kind of stuff. The additional layers streamline our everyday task of writing functions for web applications, and then again go back to the original stack and use whatever persistence layer, it could be SQL, it could be Active Record, it could be Hanami model or ROM or whatever. So we give them abstractions from HTTP till persistence layer.
It was a little bit chaotic in the early days of Trailblazer because we didn’t really know how to communicate what we are doing. But now that we have this word called high-level architecture, people ask me, “what is Trailblazer,” I say “a high-level architecture.” And then they either run away and say “that’s weird” or they say “what is a high-level architecture.” And that’s exactly those abstraction layers that we provide.
Peter Armstrong: When I was reading the introduction and the first chapter of your book, I found myself agreeing with a lot of your descriptions of the problems that exist in Rails. What would you say are the main problems that Trailblazer addresses?
Nick: Generally there’s a lovely collection of a lot of gems I have written in the last ten years. I have written them just because I wanted to write gems and feel great about being an open source developer who doesn’t get paid for their job anyway. I was always tackling a specific problem. Trailblazer is solving the problem of — like we say in the book a lot of times — where do we put a specific kind of code? Trailblazer has a bucket for any kind of implementational code snippet you have. We have buckets for authorization, authentication, for callbacks decoupled from models, for rendering JSON documents, for deserializing documents. That is a problem in Rails I’ve seen in a hundred apps being solved in different ways. Every big project has a different architecture and there’s zero conventions beyond how to name table names and how to name controllers. So we give them a strong, high-level, real opinionated structure for their code and their implementations.
Trailblazer is also solving the problem of testing in a really different way. Writing Rails applications, I always found myself wondering, “is that a controller test, is it a view test, is that a model test, is that a unit test, is that an integration test?” In Trailblazer we go a completely different way. We say decouple all the business logic into specific objects, we call them operations. All the business logic, from deserializing the incoming data to persisting whatever you wanna do, is happening in an encapsulated object, in an operation. So we only have unit tests for operations and we have integration tests to see if the controller wiring works. It’s always really clear when you write a new feature where to put the test, what to test, and it’s also way faster because you don’t have to do everything via HTTP test. That wasn’t really planned but it turned out to be a great feature that a lot of Trailblazer developers love. We don’t have controller tests at all. We have integration tests and that’s it.
The third thing we solve is that people learn a bit more about object orientation, because all those layers are basically implemented as Ruby objects. Rails is absolutely misleading you when it comes to object orientation. Just because we have model objects or a controller object doesn’t mean that this is object oriented. By introducing smaller objects with limited scope of jobs, people learn how this is actually probably closer to what object orientation means. I’m not saying that we are doing everything right. Martin Fowler will read Trailblazer code and will say “this is exactly what I thought about object orientation”? No. But I think it’s closer to the original idea, and the benefit is that you really understand what the app is doing, because every object is only doing one thing and has only one public method and there’s no way to screw up internal state. That’s what lots of people told me, that after working with Trailblazer they started to feel what a good object design looks like, whereas in Rails you only know I have a model and my model has 500 methods and that’s called object-oriented design.
Peter: So Trailblazer is an architectural style, but it’s also a collection of gems. You started with one gem and then moved on — I’m curious how it evolved in your mind, when you realized that what you were creating was Trailblazer.
Nick: That’s a nice question. I’ll start chronologically and it’ll all make sense. As I started learning Rails, I wanted to have a reusable sidebar view component and I couldn’t find anything. They told me use partials and helpers and controller, so I was basically spreading the logic across the entire stack and it didn’t feel right. I wanted an encapsulated component I can reuse in every Rails controller action and have my sidebar rendered. So I started working on a gem called Cells, which introduces what we call view models into Rails. It gives you components, and like a typical component, all it does is it’s an object and it can render a template and execute arbitrary code.
So it’s an encapsulated partial — the partial doesn’t have access to the rest of the stack. Cells works in a plain Rails app, it works without Trailblazer, and that’s good because it’s the most popular — actually the only — view replacement for Rails. Lots of people use Cells, in the thousands and hundred thousands of projects. That was my first gem, when I realized, okay, if there is interest in this kind of stuff, then it can’t be all too wrong to work on additional and alternative gems.
So I wrote another gem, Representable and Roar, basically a document parsing and document rendering library. You can grab a document and parse it into an object graph in Ruby, and you can grab any Ruby object or an object graph, nested objects, and render that into, let’s say, a JSON document. That’s helpful if you write document APIs like JSON APIs or XML APIs — and no one’s writing XML anymore, but people still have lots of questions about how to render and parse XML. That is again something completely decoupled from Trailblazer.
After that I started working on Reform, which is basically saying we don’t want to have validations in our models, because sometimes I have a form that might incorporate two or three models, or something that is not even a model at all, and I still want to have validations. Reform basically says, “I’m a form, tell me what are my fields, tell me what’s my nesting, and tell me what are my validations.” And I’ll help you doing the validations and I’ll help you writing those validated data hashes back to whatever models you want me to. So we took the validation out of the model layer in Rails and put it into a separate validation layer.
So we had views, we had rendering and parsing of documents, and we had Reform, the form object. That’s when I started to realize the way I wrote controllers in Rails changed, because I was always using a form object, always using a view component, and in document APIs I would always use the representers from Roar and Representable. So I came up with this concept called operation, which is now the central notion in Trailblazer, which is what hosts your business logic. And how did I name this? Damn, that gem name operation is already taken. I could just start a new meta framework, as my friend called it. What do I call it?
I was looking on my desk and there was an old newspaper and it said “blah blah blah is a trailblazer.” And I’m like, “hey wait, Trailblazer is a cool name.” So I named the gem Trailblazer. The operation pattern was the only thing in the gem. The operation connects representers, connects form objects and connects your persistence layer. It’s just an orchestrating object. Once this gem was there and the operation pattern became more and more clear, new things popped up like policy objects and callback objects, but the main road was really Cells and then Reform, and once we had Reform we needed a place to orchestrate how the form interacts with the outside world, and that’s where operation came, and that’s how Trailblazer started.
Peter: I have one more question and this is a really selfish one. Let’s say you have an app which has a Rails legacy back end and a lot of complexity in it, but you’re doing a front end that’s written in say React — a single page application in JavaScript, talking to the back end via a JSON API. What parts of Trailblazer are still relevant, and would it still be a good choice for the back-end architecture?
Nick: Yeah, of course. A lot of people do that actually. In my current project we do the same. We have React frontends and we have Trailblazer backend. The only thing we do not use is the Cells gem, the view component gem, because that is supposed to render HTML. You leave out the view layer and you still use the deserialization, validation, the persistence layer, all that stuff that Trailblazer structures in your Rails app.
What we had, and it’s pretty cool actually, is we had this back end in Rails and we started to port certain functionalities into Trailblazer operations, so the controller would only delegate or dispatch to an operation directly without any logic in the controller. Eventually we could swap the underlying framework, so we could say “see you later Rails,” and we started using another framework called Grape. Trailblazer allowed us to decouple all our logic from the actual framework. But we still use Trailblazer in a React back end. So that’s answering your question I hope.
Len: Related to that, is there a specific intended audience for your book? Or is it for anybody who might be interested in learning the framework?
Nick: I didn’t really know who’s supposed to be the audience, and when I started writing the book it turned out that the audience for Trailblazer is all levels of developers. We had people who’ve been around for 15 years checking it out, we’ve had people who just started Ruby five days ago and they were intrigued by the idea of layering. Just because there’s more technical complexity doesn’t mean that it’s more complex — the opposite is the case, and the book tries to propagate that on every one of the 308 pages. You have smaller objects, nicely layered implementation components that make it easier to understand how software works, not harder. If you’re on the Trailblazer chat channel you will have people from all kinds of companies, from all kinds of environments. We try to target everyone, and that’s working.
Len: Your book’s been quite successful. Was there anything special that you did to promote it, besides writing a really good book and having the existing community on GitHub?
Nick: Just in case you didn’t notice, I have the coolest book cover in the world.
Len: The cartoons too. Who did the art for the cover and the cartoons inside?
Nick: There is an artist, his name is Josh Bowman, he’s an American but he lives in Berlin, and he’s one of the best friends of one of my oldest friends. They’re both artists and they share the same studio in Berlin. I was originally asking my best friend but he was busy so he told me to get in touch with Josh, and this was just the best thing ever. We would start with little emails, he would send me scribbles, and the outcome was that we have the best book cover in my opinion and lots of cool illustrations in the book and stickers. His style just fits the whole Trailblazer project. But I don’t think the book cover is actually what makes people buy the book. I think it’s driving more people away because it looks really unprofessional.
The thing is I have a huge community, people using one or two of my gems, and those people were excited when they heard there’s some brand new project starting. I think that’s what makes people buy it. We haven’t really started any marketing campaign, so I think we could do way better.
Len: One of the things we’ve noticed is that already having a community built around the subject you’re writing about is a really important factor. On that note, on your landing page you have a little note saying email me for a 50% off coupon if you’re in a country where the US exchange rate is really expensive. Did people ask you for that? Or is that something you just wanted to do on your own?
Nick: Both. I got a lot of emails from people saying, “hey man I really want to read your book but I live in, I don’t know, Ukraine or Brazil, and $39 is equal to what I earn in three days.” I got two or three people per week asking me, and I don’t have a problem — I don’t do this for money. I do this so people have happy software architectures and go home happy, and of course some dollars is nice. So I put this on the page, and since I put this note on the page it has increased, I get even more emails from people asking me, “hey please, I’m from Vietnam, I can’t afford this, this is like seven lunches, can I get a coupon?” So it kind of works and it’s pretty cool actually. People in the US don’t mind 39 bucks, or people in Australia, because a six pack of beer in Australia is $20, you know what I mean. But if you live where the exchange rate is not that good you have to help them. I don’t know about the plans of Leanpub — are there gonna be different pricing tiers for different countries?
Peter: We’ve kicked that idea around. We try to do things that let authors have as much control as possible in deciding how their book is sold and presented. We’re based in Canada but we have to charge VAT for our EU customers, and that’s tricky because we have this minimum and suggested price, and then VAT gets tacked on, so if you have a nice happy nine dollars or nineteen dollars as your book minimum price then it turns into like twenty six dollars and forty cents in random EU countries. So we were thinking, should we allow authors to say my book is this price in the EU, or this price in the Western world but cheaper in developing countries. The flip side is that one thing we don’t like is the idea that the world should be split up and books should only be available in certain places. We like the idea that you publish a book on Leanpub, it’s available in the whole world, as opposed to lots of publishers who don’t even make their books available in lots of countries.
Nick: I can totally see how hard it is. If you start doing like, “where are you from? Ukraine, okay, your price is this and that,” this makes it a bit odd and, as you said, it splits it into areas.
Peter: Basically people want to think that the price is fair regardless of where they are.
Len: One of the things that’s very important to us is establishing a connection between an author and a reader, and choosing how much to pay is one of the cool ways that starts to happen on Leanpub. So it made me happy when I saw your little note.
On that note, in your book you suggested people are free to email you anytime, and you said earlier that when you got interested in programming, partly it was feedback from people that helped encourage you. Have you been emailed by readers asking you questions about the book, or asking you to add things, or even typos?
Nick: Oh yeah, especially in the early days before I published the book. One of the main reasons I love Leanpub is I could push out my book without being finished and people would start reading it. I had more than 500 readers before the book got published and that was great. Because it was not published they would email me, and I had so many people telling me, “hey, on this page you have that syntax error,” or “we don’t understand what you’re trying to say with your semi-English.” And lots of people asking me, “so you said I can email you, so sorry for being blunt, but do you think it’s a good idea to use phrases like this and that in our current company?” So people use that a lot.
Now most people just join the chat channel, and it’s incredible to see the activity on this channel. It’s so good to see that your stuff is actually being discussed by people other than yourself. I think people still feel the urge to participate in discussions with the author, but now they use more the official channels. I still get lots of emails of people telling me, “hey, by the way, thanks for writing this book.” I haven’t had that in 10 years. I’ve been doing open source for 10 years and you get an email every year, someone says “hey thanks for that gem,” and now you get lots of people supporting you and telling you that they like the book, or they find chapter 7 pretty great but chapter 3 sucks, which is still better than no feedback at all. It’s incredibly cool.
Peter: It’s funny, I found out with my first book, Flexible Rails, that basically the better and the longer the book got, the less feedback I got. The book ended up being over 500 pages but I got the most feedback when it was a couple hundred pages and I had real inflection points, like I had to decide should I put REST in the book or not. This was back in 2006, right?
When people see that you’re making this effort and there’s a community actually forming around the book, then you can get really good engagement from readers. It’s really cool.
Len: One more question. Is there any one thing that, if you could have your dream feature for us to build or dream problem to fix, what would that be?
Nick: The process was already pretty cool when I started. My dream feature would be positioning images in flow text, because that’s what I had to do really awkwardly. You have to preview, and the resolution is fuzzy or blurry, and I had to play a lot with settings. If you had some way to position that in a what-you-see-is-what-you-get editor or something — I know it’s a terribly hard feature to ask for. It wasn’t even a problem, I would just use float left, width thirty percent or something, and it worked, but it took me like an hour to figure that out the first time. Other than that, I’m more than happy with how Leanpub works. It’s incredible. It’s bringing this whole publishing thing to a new level. I don’t know how many new authors Leanpub has created in the last five years. How old is Leanpub?
Peter: We just turned six. We launched in April 2010. So we’re in grade one now.
Peter: We’re self-funded and we’ve been at this for a while. We’re technical book authors, right? So myself and Scott founded it and then other people joined, and we’re trying to create the best way in the world to write, publish and sell books. We’ve been to date focused so much on the writing and publishing aspect that the selling and the community stuff is underdeveloped, but where we’re going is we’re going to be doing a lot of work around that.
Nick: When it comes to community, I think integrating existing communities is the way forward — like GitHub. You have this comment-on-this-book thing, but you don’t even see that it’s there. I think I have like three comments. And what feature would be cool — to have testimonials sortable? The feature itself is great, it’s so much more authentic when you really have photos of people like, “hey we use it and it’s great.” That’s a cool feature.
Len: Well, thanks very much, Nick. I think you’re the sixth Australian I’ve interviewed for these podcasts.
Nick: But the first without an Australian accent.
Len: That’s right. It’s really interesting that some of our more successful authors have this cluster who are living in Australia or come from Australia.
Nick: It must be the environment, the blue sky and the sun, so we don’t want to go out because it’s too dangerous. You get sunburned and all that kind of stuff. You have to do outdoor activities. We don’t like that, so we go home and write books.