When this series started, I said I would build a real application in Hanami 3 with my Rails habits switched on, to find the places where twenty years of muscle memory stops working. The companion app, a small tracker for CTF challenges, will not get new features, and the places where a habit broke are in the posts and in ten tagged checkpoints of its repository. This last post holds what no tag can: what I think of it all.
Yes, and gladly
Would I use Hanami for a real project? Yes, completely, and with a lot of enthusiasm.
The framework feels more rigorous to me. Decisions have to be thought through instead of being made for you. A Rails application puts a lot in your hands straight away, and you follow rules that are very much Rails’ own. Hanami gives you less of that, and asks more. In exchange, it is designed to perform well, even in Ruby, and you can feel that the people behind it put a solid architecture in place.
What I would keep
The direction Hanami takes is what attracted me most, and a handful of posts show it better than a summary would:
- The frozen action: one instance shared across requests, so request state travels through the response instead of sitting on the action.
- Four objects instead of a model: a relation, a repo, a struct and an operation, each walled off in its own job.
- Explicit associations: data you load because you asked for it. Rails has improved on this, but here a whole family of N+1 queries never gets started, and the code says what it loads when you read it.
- Parts: presentation objects provided by the framework. In Rails I usually write my own presenters, as that post showed.
None of this changed how I write Rails, because I took that road a long time ago. Splitting an application so that each piece has one role, keeping behaviours isolated and easy to test, is what I already push for in my Rails apps with service objects and everything else that stops one model from doing it all. Hanami simply starts from there.
The traps my Rails reflexes miss
Nothing annoyed me for long. What I hit were things hidden from someone coming from another framework, and some frustration is normal when you learn one. I went through the same thing moving to Elixir and Phoenix, where I had to rethink some of the ways I designed an application.
Several posts in this series came back to a suite that was green and a property it never looked at: output that was never escaped, a sort that a spec fed a Symbol while the browser sent a String, a first request that only a fresh process could see. I do not think Hanami is why those suites stayed green. Whatever the framework, you can write tests you believe are solid and still miss something specific to the framework or the language. My Rails habits know Rails’ traps by heart, so I step around them without thinking. A new framework comes with traps those reflexes do not cover, and I discover them as I go.
Who I would hand it to
Not to someone starting with Ruby. Hanami expects more of you: you need to know the language itself well, not only the Rails ecosystem, which is by far the most documented one. I would recommend it to a team with someone who knows Ruby very well, or to a Rails developer with that same grasp of the language.
The people around it
The Hanakai documentation, the forum, the Discord and the other places where you can ask questions are very good. What stands out to me is how kind and attentive the community is, core developers included. Some decisions in Rails, coming from DHH, have felt arbitrary to me. Here, I have found people who listen.
The ecosystem is reasonably mature. Some gems are written specifically for Rails and will not work in another Rack application. I do not see that as a real problem, because many gems today are written for any Rack application, Sinatra included, and are not tied to Rails at all.
I opened two tickets for things the app ran into. hanami generate action
ignores its --url and --http options
(hanami-cli#476), and
the spec generators overwrite a spec that already exists
(hanami-rspec#53). The
second one has a reply already. It points out that the class generators do
not skip an existing file with a message either: they stop with an error
unless --force is given. For specs, it proposes a third way, skipping the
existing file with a message instead of overwriting it.
What I never got to
Background jobs are the obvious gap. The series never sent anything asynchronously, and the Hanami 3.0 guides have no section on jobs. I wired Sidekiq into Hanami back in 2016, on a much older Hanami; I have not tried it on Hanami 3.
Slices are the gap I find most interesting. A slice is a separate module
of the application with its own container, and the
slices guide uses
examples like an API or an admin. Giving the public pages, the admin and
the API a slice each strikes me as brilliant. A process can even load only
some slices through HANAMI_SLICES, which opens the door to running each
of them on its own server.
If you arrive here first, every post of the series is under the Hanakai tag, and the companion app’s README lists the ten tags, one per notion, so you can check out the state behind each notion.
Comments, questions, or just a reaction?
Send an email to ~bounga/bounga.org-discuss@lists.sr.ht. It is public and archived, so other readers can follow along and answer too.