Recently I heard about a fairly new programming language called Crystal. Crystal is a self-hosted statically typed compiled programming language with C-like performance with Ruby-like syntax. While I quickly fell in love with Crystal and Amber, this post will talk about my experience, I’m going to talk more about the bad parts and how they relate to the good parts as I want to provide a critical overview of what Crystal and Amber, can and cannot do for you. I don’t want to say Crystal and Amber is perfect, I want to be transparent about the current flaws, and lastly I want you to make an informed decision to choose Crystal and Amber.
What is Crystal? Is it worth learning yet another programming language?
Crystal is what Ruby would like if it were statically typed and compiled. While Crystal marketed as having C-like performance, in reality using benchmarks its performance is closer to that of Rust and Golang, sometimes outperforming both.
One of the appeals of Crystal to me is the compiler detects things like nil-unsafe code and will throw a compiler error rather than a runtime error if code isn’t nil-safe. As a plus, being trained by the compiler my code is nil-safe when writing Ruby as I’ve learned to write them before errors show up not after they show up.
While the performance benefits of Crystal are incredible it does have risks involved.
- Crystal only has one full time core developer funded, the rest of the core team works in their free time.
- Crystal’s funding is from its community and a small group of companies using it.
- There are very few Crystal Shards (Shards are the equivalent of RubyGems & NPM Packages) meaning you’ll have to write more functionality on your own. However Shards are also unique in comparison to RubyGems and NPM Packages. Rather the developer building a package and hosting on a third party package repository, the Shard system pulls the source remotely from a Git repo and is compiled on the same machine. This has huge security benefits and it saves the developers of Crystal from having to fund their own package servers. They can let GitHub (GitLab, and similar services included here) handle the load for them.
Does Crystal have a Web Framework?
While the Crystal language looked very appealing to me and I am ready to create a project in it, its website had no mention of any web frameworks. I was a bit worried about this, I did a few quick web searches and found a web framework for Crystal called Amber. The Amber Framework uses Crystal’s built-in HTTP Server, adds its own features and CLI tools on top. Needless to say, by building your web application with Amber, you get all of the performance and type safety benefits of Crystal. Amber feels just like Rails, just less mature but it fits my needs. It follows the model-view-controller paradigm and includes an ORM called Granite (which supports PostgreSQL out of the box).
Amber has some additional goodies included. For example, type
amber database into the command line and you’ll be dropped into a Postgresql shell without having to type out the database name. It’s a huge time saver and many similar things are included.
Using Amber does have its own set of cons to add on top of Crystal’s, these include:
- As with Crystal, the Amber Framework Developers have little funding (no funding according to their LibraPay Page 😕 ) and are a small core team (all working on Amber in their free time). Bugs may take longer to patch and there’s less people out there to support you.
- There’s less documentation available in comparison to projects like Ruby on Rails, although the docs available explain most things pretty well.
- Since there are less maintainers, it takes longer to get a pull request merged into their project. If you want to fix a bug quickly you’re out of luck unless you apply your own diffs to Amber after pulling the shard from GitHub.
Onto my first project using Crystal and the Amber Framework
Now that I provided background on Crystal and the Amber Framework, I can describe my experience developing an application with it. Previously I had been working with a Ruby on Rails Application that organizes ROM Hacks of the video game Super Mario 64 and I decided as a test of Crystal and the Amber Framework I would rewrite the app using them. I was able to port the code over in a matter of hours, since Ruby already has very similar syntax most of the rewrite was simply copying over routes and controls one by one and making minor changes to validation and model logic. Writing the database migration code to switch existing data from Rails to Amber was a bit harder for me as I had never written migration code before. Users observed 2 to 3 times faster page loads after switching from the Ruby on Rails Application to the Crystal with Amber Application. And it was a positive and pleasant experience for everyone involved. My users didn’t notice the change other than the new theme and faster page load times.
You can see the end result of the migration at https://hacks.sm64hacks.com/ and check out the source code at https://gitlab.com/sm64hacks/hackdb. I hope this has given you a nice overview of using Crystal and Amber to develop a web application and that you consider using them for your next project.
Update: Brian J. Cardiff of the Crystal Language Core Team responded to a post on their community forum with the following about the amount of paid core contributors and how the donations are used.
“The donations advertised in the homepage goes to Manas. Manas currently doubles that income as stated in https://crystal-lang.org/2017/12/19/this-is-not-a-new-years-resolution.html 1 .Brian J. Cardiff on Crystal Community Forum
Those allows Manas to allocate time from developers to invest in crystal. Although I might be de most visible Manas developer during the last months, other developers joins in scoped or sporadic efforts as well.
Those activities are not always visible. Sometimes is brainstorming, discussing ideas, preparing material, coding, experimenting, giving feedback, etc. Some of them are: waj, ggiraldez, mverzilli, and matiasgarciaisaia.”