Crystal Programming Language: The Why and How

Crystal Programming Language: The Why and How

Crystal combines Ruby's elegant syntax with C-like performance. Discover why this statically typed, compiled language could be the holy grail for developers seeking both productivity and speed.

Gerardo Ortega

Crystal is one of the new languages on the scene. Although not as well-known as Rust, Elixir, or Julia, it has a lot to offer. Started in 2012, according to its creators, its main features are:

  • Ruby-like syntax
  • Statically typed
  • Compiled
  • Self-hosted (Crystal is written in… Crystal)

Beyond that, the language’s purpose is simple:

  • Have Ruby-friendly syntax with C-like performance.

There are many more features available on the official website. But what particularly caught my attention, as a developer who loves Ruby, is its similarity to the language. Having syntax as good as Ruby with performance comparable to C is the holy grail for developers.

Crystal Syntax

I want to start with the syntax, which is the face of the language. When I started with Ruby, coming from Java, what captivated me most was the simplicity with which things were done; it was like writing pseudocode. And Crystal is no exception. While it’s not 100% identical to Ruby (more for better than worse), it preserves its essence. Here’s an example of an HTTP server:

require "http/server"

server = HTTP::Server.new(8080) do |request|
  HTTP::Response.ok "text/plain", "Hello world! The time is #{Time.now}"
end

puts "Listening on http://0.0.0.0:8080"
server.listen

Beyond Syntax

Crystal runs on LLVM, a technology that serves as a compilation framework and can generate machine code with performance similar to (or sometimes superior to) C. LLVM is also used by other new languages like Rust or Swift and offers enormous flexibility, support, and many complementary libraries. This has allowed Crystal to add features that Ruby lacks, such as concurrency and parallelism—the latter is still under development.

Now, I should clarify that Crystal as a language doesn’t bring any innovation to the table that other languages don’t already have. In other words, the design of many new languages that have emerged recently has been oriented toward extending or creating better ways to do certain things. Crystal, on the contrary, has largely tried to integrate many of the best features from languages like Go or Erlang. What Crystal does offer, and what shouldn’t be overlooked, is elegant syntax plus excellent performance, all without having to relearn many things, which makes it unique, productive, and pleasant to work with.

Learning Crystal

Here’s a list of resources that can help you get started with the language. It’s worth noting that, being a young language, there isn’t much available yet, but things are improving over time:

The Future of Crystal

I’m a Software Engineer, not usually a good fortune teller. But I can make my own bets about Crystal’s future, especially considering what has captivated me and what fills many of the gaps that Ruby currently has, to mention a few:

  • Concurrency
  • Performance
  • Parallelism (in progress)

And that, in my case, is of utmost importance. After all, times have changed, as have our needs. We live in the era of Big Data, the Internet of Things, VR, etc. One area that has particularly caught my attention is Artificial Intelligence, and I increasingly need tools with excellent performance, but traditionally you have to choose two technologies: one for modeling (Python or R, for example) and another for implementation (C, C++, or even Java) for obvious reasons. With a language like Crystal, it’s a panacea; there’s no need to duplicate efforts.

In this regard, I’ve also tried a new language that’s specifically designed with performance and productivity in mind for scientific computing: Julia. However, I don’t want a niche language, but one that helps me provide solutions in different areas (something like Python with all its libraries, but with C’s performance :p).

That’s why I’m betting on Crystal, a language still in its infancy but with great growth potential; it has everything necessary for that. Ruby wasn’t always popular until Rails came along; the same can happen with any programming language. The key lies in the community around the language, who make things happen. For my part, I plan to contribute in the area I’m passionate about: Data Science.

Gerardo Ortega

About Gerardo Ortega

Software craftsman with a focus on scaling, polyglot programmer, coffee enthusiast, and lifelong learner. Passionate about machine learning, data science, and building great products.