Logo of the Crystal Language
Recently I heard about a new programming language called Crystal. Crystal is a self-hosted statically typed compiled programming language with C-like performance with Ruby-like syntax. I quickly fell in love with Crystal and the Amber Framework. This post will talk about my experience using it to develop a web application. This post provides a critical overview of what Crystal and Amber, can and can't do for you. I don't want to say Crystal and Amber is perfect. Rather I want to be transparent about the current flaws. I want you to make an informed decision to choose Crystal and Amber.
What is Crystal
Crystal is what Ruby would like if it were statically typed and compiled. Crystal marketed as having C-like performance. In reality using benchmarks its performance is closer to that of Rust and Golang.
One of the appeals of Crystal to me is the compiler detects things like nil-unsafe code. The compiler will throw an error if code isn't nil-safe.
While the performance benefits of Crystal are impressive 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 few Crystal Shards. Shards are the equivalent of RubyGems & NPM Packages. This can make development of new programs take longer. You’ll have to write more functionality on your own.
Shards are unique in comparison to RubyGems and NPM Packages. They behave similar to C++ libraries. You have to compile it on your own. This has 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 Crystal looked appealing to me, its website didn't mention any web frameworks. A quick Google search found Amber. The Amber Framework uses Crystal's built-in HTTP Server. Amber adds its own features and CLI tools on top to make things easier. You get all of the performance and type safety benefits of Crystal while using Amber. Amber feels like Rails. Amber is less mature but it fits my needs. It follows the model-view-controller paradigm and includes an ORM called Granite.
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 is less documentation available in comparison to projects like Ruby on Rails. The docs available online explain most things 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. You will have to 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, I can describe my experience developing an application with it. Previously I had been developing a Rails Application that organizes ROM Hacks of Super Mario 64. I decided as a test of the language and framework I would rewrite the app using it. I was able to port the code over in a matter of hours. Ruby has similar syntax to the language. The rewrite process was copying over controllers and views. I had to modify each controller in view to work in Amber, one by one.
How difficult was the change from ActiveRecord to Granite
Writing the database migration code to copy existing data from Rails to Amber was challenging. I had never written migration code before. After the rewrite was complete page loads were three times faster. 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.
Was was the end result
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 gives you a nice overview of using Crystal and Amber.
Brian J. Cardiff of the Crystal Language Core Team responded to a post on their community forum. Brian said the following about the amount of paid core contributors and how their 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 .
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."