The Crystal Programming Language Forum

The core principles behind Crystal

We all know that Crystal is “fast as C and slick as Ruby”, but I wanted to get a conversation going on what the core principles behind the language are, and what we as the community actually want in a language.

To explain what I mean, let’s take a look at a few other languages:

Ruby - focus on developer happiness. Matz said it wasn’t designed for machines, it was designed for developers. So the goal was never to be the fastest language, or the most memory efficient. It’s goal was to make programming fun, and easy while still being a real language (as opposed to a toy language)

Rust - WASM as a first class citizen. I’ve never used Rust, but I know anytime you look up doing anything WebAssembly related, Rust is right there at the top. They’ve already taken the lead as the go-to language to do WASM stuff.

Elm - I haven’t used Elm either, but it was brought up to me that they had a principle of error messages and stack traces being at the core of everything they implement. I guess they really focus hard on making sure their errors are clear, concise, and easy to understand making debugging super painless.

Python - When ever you see anything that mentions “Machine Learning”, Python always shows up. Can you do machine learning in other languages? Yup. But Python seems to be the first go to.

When it comes to Crystal, I could see maybe the neat C DSL being at the forefront of the language. This is something you don’t really see in other languages, and it’s a unique feature for Crystal. But when it comes to this, it’s not really well documented on how to use it. If you don’t know C or maybe C++, then it still looks sort of foreign. But maybe that’s not it…

We also don’t have WASM support at all, and our error message, though pretty decent, still need tons of work. ref. We’re not really associated with anything other than “it’s like a faster ruby”. And anyone that’s written Crystal for a bit knows that that statement is a bit “cringy” :grimacing:.

This leads me back to the original question here. What is it that we want to be at the forefront of our language? What is that core principle or that one thing that when people google for it, Crystal is always at the top and is known for doing that?

I get that this isn’t an easy one word answer, and it may get lots of different answers as everyone has their own vision and such, but what ever that thing is, I’d like to see it put on to the website. It should be stated clear in a “Why choose crystal?” or possibly on a page that talks about people that want to contribute to crystal what things they need to keep in mind as they submit PRs.

Anyway, this is just to get the conversation started. Let me know what you think! Happy crystalling :heart:

3 Likes

This is a great question and topic!

I personally don’t think we should continue saying “Fast as C, slick as Ruby”, mainly because:

  • C has no GC and it’s actually faster. So we can achieve the speed of C if you program in a certain way (pointers, etc.) but it’s not the recommended way.
  • I don’t think we are as slick as Ruby because Ruby is more dynamic and there’s less friction because of lack of types

Then… what’s the thing “go to” thing in Crystal? If we look at the examples you posted, I didn’t know Rust was related to WASM. I’ve been following Rust and maybe that’s something that they chose to pick up recently, did really well with it and then it got know as the WASM-goto. Same with Python: you can do many things with it but I don’t think Guido thought of it as a machine-learning programming language. It just happened because a group of people started using it for it (maybe Google? I don’t know).

For Ruby it’s true that it’s focused on developer happiness, but, again, people mainly use it for Rails so right now it’s a “do a website quickly” thing.

With Crystal, my motto is “A language for humans and computers”, meaning that it wants to retain the fun factor that Ruby has but also talks nicely to computers, meaning it’s efficient.

About Crystal’s killer app… I guess someone should come up with something Crystal does really well, and if it goes well it can become popular and people would go to Crystal if they needed that. It’s just that it didn’t happen yet…

5 Likes

I use to give this talk on Crystal and I would break down the Fast as C and Slick as Ruby.

I said “Fast as C” is performance and “Slick as Ruby” is roughly Abstractions. Crystal is as predictable and performant as compiled languages and maintains the abstractions and practicality of interpreted languages.

It might make more sense in the context of the talk. But I would say if I were to write Crystal’s tagline it would be “Practical, Performant, Predictable”. But that might just be how I see the language.

2 Likes

It’s related, but the core principles of Rust are actually very different from WASM. Rust’s main focus is a secure systems programming language.
So I disagree with @jwoertink’s inital assesment. Python is similar. Machine learning is maybe currently hyped, but Python has a much bigger stand with scientific programming in general (that’s why it’s also prominent in ML), but also as a general purpose scripting languages for anything from operating systems to 3D animations.

IMO the principle of Crystal is similar to Ruby’s: developer friendliness. And it mixes in a lot of new goodies for that, that come from being statically typed. For example that the compiler already tells you when your code can’t work because of type issues. I agree with @asterite that in some cases Crystal’s type system adds more friction than Ruby. But again, it also helps implementing more helpful stuff. And a lot of Ruby’s dynamic features unavailable in Crystal (for example in metaprogramming) can actually be counterproductive when too much abstraction and “magic” makes it hard to understand what the code actually does.

I also wouldn’t completely disregard “Fast as C”. Of course, the runtime comes with a GC which has a performance impact. But the same applies to applications written in C or similar languages when they use garbage collection. And of course, it always depends on the behaviour of an application and the implementation, how much impact it actually has.

“A language for humans and computers” is a nice slogan, too. But I’d argue it’s lacks specificity. IMO it’s not self-explanatory to an unacquainted reader what “a language for computers” is supposed to mean.

2 Likes

For me what is important in Ruby (15 years of experience with it) and Crystal (1 year and watching Crystal development for few more years) is readability and easy to use.
For me it is warm English like language that is concise and compact with very nice object oriented paradigm where everything is an object.
Now with debugging mostly working it is even better. I am planning to build native Crystal debugger that can be part of Crystal.

3 Likes

Also, WebAssembly is supported in LLVM since version 8 so it can be used automagically if needed.

In order of importance:

  1. Its possible to write beautiful and concise, yet human readable code. Joy.
  2. Its a static typed language without the need to spell every type out. Smart compiler. Great.
  3. It’s compiled but has builtin meta-programming magic if you need it. Cool.
  4. Fast. zippy. quick. Which would make it actually environment-friendly because it is efficient :slight_smile:

Honorable mention: The superb standard library. Don’t count it as a part of the language design, or it would be in second place.

10 Likes

Echoing some of what has already been said, I use Crystal primarily because it’s enjoyable to write and read (or at least can be with very little effort). I’m a young’un , so my first “real” programming language was Python, which I found really fun to write. However, since I’ve been writing real software over the past couple years, I’ve really come to prefer the constraints provided by a nice static type system. For example, the difference in readability between a random Crystal library on GitHub and a random Python library is wild. The Python may have been a little easier to write, but it’s usually totally unclear what type anything is meant to be. Crystal, on the other hand, is usually very clear. So I agree with @mavu’s #1: Crystal is a joy to read and write without being too verbose.

Also, though, I agree with their #2 and #3: Crystal provides a fantastic continuum between power and simplicity. I can use it like I would Python to throw together a quick prototype of an idea I have just to see if my logic is correct. I can also write a straightforward library with explicit typing everywhere for clarity, easy-to-read specs for correctness, and automatically generated documentation. Or I can really go hard with the metaprogramming to make a library that allows users to accomplish really complex tasks with ease (like almost anything that @Blacksmoke16 does).

I do really like @wontruefree’s suggested tagline,

However, I think that the code aesthetics and legibility also need a place in the tagline. I also really like the “Practical” and “Predictable” from the above tagline, though, and I think that it’s more important to my love of Crystal than flexibility. And speed is clearly a primary strength of Crystal.

Perhaps Fast, Elegant, Reliable?

I’d like to give a little context as to why this was posted. I think my title of the post may be a bit wrong here, but maybe context will clear up my thoughts.

I teach programming, so I have students come up to me and ask me about doing something specific without knowing what tool to use. I’m a bit believer in a language not being the “do everything” language. For example, you can make video games in ruby, and it’s fun! But if you wanted to release an indie game on steam as your full-time job, Ruby wouldn’t be your first go-to. You might choose C++, C#, or even possibly Lua on top of some C++ or Python. If a student came up to me and said “I want to start looking at Machine Learning, where’s the best place to look for info?”, I’d point them to learn some Python, and send some resources related to that. That’s not to say that Python is solely about that, it’s just that ML has a strong presence in that community. If a student came to me and said “I want to do some WASM stuff”, I might point them to Rust or Go. And for web dev, we teach Rails in my classes because it’s a lot easier to get a first job knowing rails as opposed to knowing crystal (for now).

The answer I’m looking to solve is, if a student came up to me and wanted to do XYZ, what would be that thing that makes me say “Take a look at Crystal, that’s your best option”. So not to replace your tools, but to augment your toolbelt. Now, maybe it’s the exact same as Ruby, but with a few nice extras? Maybe the answer becomes “If you want to prototype a quick app, you can use Ruby and Rails, but if you need something really fast and solid, take a look at Crystal and Lucky (shameless plug)”.

I don’t know what the answer is here, but I do like the “Practical, Performant”… Crystal hasn’t really been predictable for me. “Fast, Elegant, Reliable” is a pretty nice tagline that states what I’ve felt and the reason my company has moved over to almost 100% crystal with 4 or 5 Lucky apps in production now.

Let’s keep the conversation going! This is great stuff everyone :smiley:

2 Likes

Hmm. Not sure about this. I’d go at it the other way around:
What things can I not use crystal for?
Does it have to run on windows? -> poor sod, chose something else.
Does it need a graphical UI? -> maybe consider something else.
Making a webapp that has special needs? Use Rails, better chances of finding gems that do-the-thing.

Honestly, for anything else I’d use Crystal.
You can easily use c libraries if neccessary, and those exist for almost anything.
Sure, some things might be more work, and if under time constraints and software available that already almost does what I want, I probably chose that.
But in general? Why use something else?

(This is an opinion only, and although I am a software developer, I can choose my tools freely, and live in a mostly linux/server environment. Outside constraints might very well push you to use different tools, but if I can choose, I certainly choose crystal if at all possible.)

1 Like

Well, one answer that comes into mind is programming competitions.

In these competitions you need these things, at least for the most challenging problems:

  1. You need to be able to write code fast. That means little or no ceremony: not writing types, boilerplate, etc.
  2. Similar to the previous point, if you have a nice standard library or abstractions to manipulate data, you can save a lot of coding time. This is the many Enumerable, Array and Hash methods.
  3. You need your program to run fast. Many competitions have an upper limit for the time the code can run. Or you need it to run fast because you are trying it out and you can’t just waste minutes or multiple seconds for the program to run.

So I think Crystal excels in those three points.

The only thing is… these are usually disposable programs. But still, it’s a use case I can recommend Crystal for, and programming competitions are something real and they can get you prizes and money, so…

2 Likes

To add a bit to that, I always do Advent of Code in Crystal. I start with the idea of also doing it in another language… but in the end I think “Well, in Crystal I can code it faster, with less code, and it runs faster, so why bother learning a language that will be worse in all of those areas?”

6 Likes

I used to love competing in Rails Rumble :joy: those were the good days. We should spin up a “Crystal Coding Chaos” or something hahah.

Total side note but I would rather see a document explaining Crystals principles over a slogan. But, to be honest I dont think a language needs either.

1 Like

Going to give a talk by the end of this month (an intro to the language), and that motto was in the title. Time to change the title :slight_smile:.

1 Like

My reasons for working with Crystal

  1. The beauty and expressibility of Ruby
  2. The deployability, speed, and multi-threadedness of Golang
3 Likes

Beside the syntax that brings fun, the intent for a type system that aim to balance safety and expressiveness is one of my preferred aspects. The separation of Nil as a value of every type it bring something relatively new to the table. The complexity of the type system grow from this point: Unions, NoReturn, Nil, Tuples, type restrictions and how they interact between each other is a lot, but they give life to the balance between safety and expressiveness.

The runtime is another strong player in the principles of the language. Reducing a lot the usage of callbacks and allowing the programmer to think in a more linear way rather than pending callbacks to be executed.

And having a std-lib with batteries included is something I’m proud of.


Regarding the tag line, a couple of months ago we moved away from Fast as C, slick as Ruby in order to express values without comparing it to other languages. So if you find one, let us now :-)

10 Likes

I like this explanation.

Want to add a quick opinion. A good basic principle of Crystal is productivity.

Which is vague, but I think it is reflected in a lot of its properties that were mentioned above.

  1. Ruby like syntax - Crystal “steals” the productivity from Ruby by using its syntax
  2. Type System - The compiler finds bugs quickly and requires me to write less tests than I would in Ruby.
  3. Readability - Crystal code is easy to read, so theoretically it should be easy to contribute to projects

I see only benefits by advertising the language as productive.

3 Likes

@asterite I really like how you resume it

With Crystal, my motto is “A language for humans and computers”

It describes the best of both worlds. I have always enjoy crystal because I can write well readable programs, that performs well with little to do optimizations.

I wonder how can we expand that vision/mission into core language principles?

1 Like