The Beauty and Horror of Crystal Shards
I have been using a lot of Crystal for personal projects for about a year. Think of it like "native Ruby with type inference" or something like that.
But this post is not about the language itself, it's about one part of its ecosystem: the shards.
The Nice
Shards are like Ruby gems, or like whatever go calls them nowadays, or rust crates, just "libraries" that are written mostly in crystal.
The thing is ... they are awesome!
Suppose you want to use markdown in your app. You go to shards.info
which crawls GitHub and finds things written in Crystal. You search for markdown and
you find a few that look good. Suppose you want to use my cr-discount
shard.
You just add the shard to your shard.yml
and use it, like this:
dependencies:
cr-discount:
github: ralsina/cr-discount
Then in your code:
require "cr-discount"
markdown = "This *is* **markdown**"
html = Discount.compile(markdown)
And that's it, you just integrated it into your build, your code is using it, and that's all there is to it.
But not only that, suppose you are using a shard like docr but you find a tiny bug or two. You fork it, fix the bugs and create a PR. If the author is active, they will merge it, and you can go back to using the shard, but even if they aren't ... you can just use your fork in the meantime.
Just use ralsina/docr
instead of marghidanu/docr
in your shard.yml
and you are
using my fix.
And there's more! Suppose you find some code in your projects that you keep repeating. For example, I always use the same logging setup in CLI apps:
- Colorful if it makes sense
- Configurable verbosity
- Errors and worse in stderr
- Info and better in stdout
So, why copy that everywhere? Just make it a tiny shard.
There is no overhead in your code, you don't have to ask users to install anything,
you just add it to your shard.yml
and it's there for everyone that cares.
The Not So Nice
Of course nothing is free of cost.
- Because it's easy to create shards, it's easy to abandon shards.
- Because it's easy to fork shards, it's easy to atomize the ecosystem.
- Because it's easy to find shards and it's decentralized, it's trivial to poison the supply chain (although TBH it seems to be easy enough in any language).
So, you have to be careful. You have to check if the shard is maintained, if it's the best one for the job, if you fork it you need to commit to keeping your fork working, you always need to push the PR even if the author doesn't pick it up because it's there for other users.
So, conclusions... they are what they are. For me they are the most practical thing ever and I often wish Python had something like them, but they are also quite ... scary? And seeing the abandoned shards makes me sad for a language that should be much more popular than it is.