Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore.

15 St Margarets, NY 10033
(+381) 11 123 4567



Discovering Crystal – DZone Web Dev

Time for another in the series. Check out other languages we’ve explored in the past at:

A bit of background about me before we begin our exploration of a new programming language.

I’ve been in love with programming languages for a couple of years now. I’m very interested in their semantics, the reasoning of why their designers chose to do X over Y, and why they outright discarded Z. I’ve done my fair share of DSLs in my tenure as a software developer for both fun and profit; so programming languages are the reasonable “step up” to be interested in.

As such, I’m always on the lookout for up-and-coming languages.

Why Crystal?

A long time ago, far, far away I was working with Ruby on Rails on a project with other very skilled Ruby and Rails developers. Like many others working with Ruby, we liked its syntax, its ease of use, and its dependencies (gems), but we had some issues with it, like with any programming language.

Honestly, performance was not my main driver to look at other programming languages, even though in many benchmarks, Crystal does excel.

I was more concerned with safety and documentation. Let me explain.


Whilst programming in Ruby, I often had an error where an object I thought was a `Hash` was actually a block, or a string was in fact a number. Many times, a test would just fail or panic, and it took a minute to look at the trace, maybe put a breakpoint. Pry open the execution. Realize there was a branch of the code that did not set a key in an `options` parameter that got passed along and I did not account for. Fix it and continue coding.

undefined method ‘foo’ for nil:NilClass was particularly recurring in my development cycle.


Here, I’m not referring to the actual docs I can find in Ruby Doc, but rather how much information can the IDE help me out with. I’m a big proponent of making tools to help development, but having to hydrate an `options` hash to send to a gem’s method and having to yield to look at the gem’s code to understand what values are even available is a big reason why I enjoy typed programming languages so much.

At a glance (and with IDE support), one can deduce that foo’s options have a required bar string, and a biz that defaults to 3. Whereas similar code in Ruby could look like this:

Both these pain points were addressed, interestingly enough, by the same approach: Types.

In Crystal, there is a very smart, very competent compiler that keeps track of the busywork of knowing what methods any reference might have, if it is `nil`, or what can and can’t be done to an object. This was a godsend.

As luck would have it, I was also in close contact with some very vociferous advocates of Crystal. You can imagine, with the tagline “Fast as C, Slick as Ruby”, I was hooked.

In my breakout project, the moment I really “got” Crystal was when we needed some dashboard-like solution. We realized we had built too many internal tools and no one place to cluster them all. For whatever reason, we didn’t want some static lame webpage. We decided to roll out our own. Some YAML parsing, some macro goodness, and a lot of type safety later, we had it running. Adding apps was simple, and a CI build made sure that all the things were correctly wired. It was a small project, probably way over-engineered; but onboarding another Ruby developer was incredibly simple. That was a good selling point.

Why Crystal Now?

That’s leading the subject, your honor!

Recently™ the nice people of Crystal announced the release of Crystal 1.0: a production-ready, stable, and useful standard library all in a tight little bundle; so if it was ever the time to seriously look at Crystal; I would say today is the day.

Other Awesome Features

Type Ergonomics

We talked about type safety, and some might have rolled your eyes expecting a very verbose way of writing code; but let’s not forget one of the key drivers of Crystal is to have a similar syntax to Ruby. A dynamically typed language. So:

…is both valid Ruby and Crystal. You can try that out in your browser in the crystal playground here! No type to be seen, thanks to the incredible type inference that comes with Crystal’s compiler. There are many cool things about it that I would need entire blog posts to write, but luckily Crystal’s book does a good job covering the basics: Type inference.


It’s amazing how useful it is to have such a robust parsing library already in the standard library: No need to deal with extraneous dependencies to read a configuration file or write one out.

That’s all the code needed to parse something like

The same holds for JSON and XML. I find it baffling how, when reading and writing configuration is so easy, much more customization is allowed when developing tools with Crystal.

Decentralized Shards

Maybe as I’m getting older, I’m getting more paranoid about where my dependencies (which run, for all intents and purposes, arbitrary code that gets deployed on my behalf) are fetched; but I can’t/won’t wait for a cabal to approve a new dependency. Being able to choose either a local file or any `git` provider gives me a bit more peace of mind.

The Logo… It’s Alive, It’s Moving, It’s Alive, It’s Alive!

The black logo on Crystal’s home page is interactive. You can click and spin it around. At first glance, one can brush off that fact, thinking it is superfluous and useless; but I would urge you to wonder: Would a bad language¹ have such a polished home page, such well-written tutorials, and such active community?

1: Reader’s interpretation of what a bad language might be.

Where to Now?

If you are interested, you can keep learning Crystal with the book and the online playground. Have a look at the awesome-crystal compiled list of things, some might inspire you in your current or next project. Reach out.

And with that final note; I’ll bid you adieu, and see you around for the next post.

Credit: Source link

Previous Next
Test Caption
Test Description goes like this