Save over 40% when you secure your tickets today to TNW Conference 💥 Prices will increase on November 22 →

This article was published on February 9, 2023

If Python is too slow for you, Crystal could be your savior

According to its creators, it reads like Ruby and runs like C — or faster


If Python is too slow for you, Crystal could be your savior

Learning a new programming language can open your mind in ways you never thought possible. Just like learning a new human language like Spanish or Mandarin, you learn to think with different words and structures.

You tap into the cultures and communities of the speakers and learn how they see the world. It’s enriching, to say the least.

The neat thing about programming languages is that the first one you learn is always the hardest. Once you understand basic structures like if-clauses and for-loops, you’ll see them popping up in many new languages you might learn.

And, unlike human languages, new programming languages are much faster to learn. They’re more logical and have less words — or, should I say, commands.

It follows that most programmers and data scientists master more than one programming language.

They might have a main or favorite one. But most software developers I know use at least five languages on a weekly basis, if you count scripting languages in.

Most programmers also try to learn a new language every so often.

It comes with the job. Coding means staying curious.

The case for Crystal

Crystal, as per its advertisement, is a language that’s fast like C and slick like Ruby.

The part about its slickness is true. It’s compiled and statically typed, which comes with its own advantages and disadvantages. Above all, however, it’s very similar to Ruby in its style.

Personally I’ve never written a larger project in Ruby. But as a fairly experienced Python developer, Crystal code still looks crystal clear!

Here’s an example of a recursive loop that calculates factorials:

Factorial calculation in Crystal. Image by author, code taken from Crystal by Example
Factorial calculation in Crystal. Image by author, code taken from Crystal by Example

The command puts sounds a bit weird to people who aren’t accustomed to Ruby. It’s basically a bad-taste version of print in other languages. There are some subtle differences between puts and print but we won’t dive into these here.

Apart from that, this example looks quite pythonic, right?

It’s similar for if-clauses:

If-clauses in Crystal. Image by author, code taken from Crystal by Example
If-clauses in Crystal. Image by author, code taken from Crystal by Example

Apart from the funny puts expression that non-Ruby-natives aren’t used to, this is again very readable.

Generally speaking, the syntax of Crystal is clear, even when dealing with larger and more complicated problems.

Static typing

Crystal is statically typed, but you may have noticed there weren’t many type identifiers in the examples above. That’s because Crystal only requires you to specify the type of a variable if it’s ambiguous.

This practice, of course, helps programmers be more productive.

Macros

Statically typed languages tend to be finicky around macros. Crystal, however, has a way with them.

As shown in this example, you can use a Crystal macro to change a piece of code at compile time through static reasoning, based on the contents of another piece of that same code. Try to do this in C++ and you’ll either fail or need 500+ lines of code.

Or don’t you, you genius? Let me know if I’m wrong…

This does come with some caveats because, as the saying goes, with great power comes great responsibility. Static typing usually reduces some danger, but insanely powerful things (like the ones that Crystal allows) somewhat undo this safeguarding.

No Nil-errors

Speaking of safety, Crystal does have additional safeguards elsewhere. Nil is a separate type for the Crystal compiler. So if you’re trying to access a method or container that turns out to be nil, the compiler will warn you. This might result in a failure of compilation, but at least you get to fix the error before you run a potentially large code.

No more dependency hell

If you’ve ever handled a large problem in Python, you probably know what dependency hell looks like.

One Python module might depend on another few, and once you’ve imported all the modules you need, you realize that different module versions aren’t compatible. Now you have to figure out which versions to use to make everything fit together, and all of this can take hours when your project is of a certain size.

In Crystal, these modules are called shards, and they get compiled into your binary executable via static linking. I could go into detail about this, but let’s cut the story short: Dependency management is much, much easier this way.

The big drawback: Crystal is tiny

Crystal had a moment in 2017 when it jumped from place 60 to 32 on the TIOBE index within just a month. The reasons for this meteoric rise are hard to say, but it’s quite likely that the creators of Crystal did considerable marketing at the time and got Ruby programmers curious.

However, Crystal’s moment of fame didn’t last very long. At the time of writing this article, Crystal is nowhere close to the top 50 programming languages on the TIOBE index.

Because of its early rise in popularity, some people hoped it could see similar levels of popularity as Rust.

This, however, hasn’t materialized.

Programmers have expressed frustration about sample projects or code bases. Also, the documentation seems a little bit incomplete. This turned new programmers off in droves.

And because of this, the Crystal community is so small that you’ll have to wait a while to get help if you’re running into problems. There just aren’t so many answers on StackOverflow, GitHub, and the likes.

The other drawback: Crystal isn’t as speedy as promised

The creators of Crystal wanted the language to be as fast as C.

This might be true in isolated cases. However, most of the time, Crystal takes about twice as long to execute as C does.

It’s not uncommon for programming languages (or tech companies, for that matter) to have overblown mission statements. And, to be fair, half as fast as C is still pretty fast.

Nevertheless, Crystal significantly under-delivers vis-à-vis its original promise. It’s so glaringly obvious that I can’t blame the average developer for feeling disappointed.

Famous last words: Crystal is for experienced programmers only
I love exploring new programming languages, but I prefer well-documented languages with a large and supportive community. Many programmers feel similar.

Crystal has had its shot at mainstream appeal, but overall it has failed in the last few years.

This doesn’t mean that there aren’t some interesting use cases for Crystal. It might be an interesting option for people working with blockchain technology. You can create a fairly powerful cryptocurrency with Crystal.

That being said, unless the documentation, marketing, and general community support for Crystal improves, it won’t be a great option for the average blockchain developer either.

If you want to play with a language like Crystal, you’d better bring some experience. Because when you’re faced with a problem, there might be nobody there to help you.

This article was written by Ari Joury and was originally published on Medium. You can read it here.

Get the TNW newsletter

Get the most important tech news in your inbox each week.

Also tagged with