by neya · 6y ago · 0 comments
Elixir is one of those languages where everything has been done perfectly as of the time of this comment. When I say perfect, I mean, there has been never once in my career where I hit a roadblock due to the language's limitation or complexity or flawed assumption. I faced this with other languages, but not Elixir.
I'm currently a full time consultant writing, teaching and deploying production apps for clients in Elixir. Most of my apps have served my clients well beyond my contractual agreement. It's almost deploy and forget because of Phoenix. It also scales amazingly well. It's extremely performant.
I just re-wrote the entire crappy mess Wordpress core in Elixir and enjoyed the process. In any other language, I wouldn't be saying the same. It's really an enjoyable language that forces you to rethink the way you write code. Don't be put off by this statement, I mean that in a really good way. And once you start thinking interms of functions, you just won't touch any of those terrible OO programming paradigms (Eg. writing for loops, nesting conditionals, etc.). You will start writing maintainable, beautiful code.
You will appreciate Elixir a lot especially if you have a background in horrible programming languages like Javascript. You will literally start finding ways to use Elixir in your entire stack like I am. It's that good.
Enough said. Give it a shot.
This is pretty amazing praise! Are many of the other languages you've used strongly typed? That's the thing that gives me pause - I feel like I rely on the compiler a lot in languages where it can do a lot for me, and that I'd be really frustrated managing a large project without it.
I'm not sure what other languages you have a lot of experience with, but does anything in Elixir stand out to you as the reason dynamic typing works well there? I think dynamic typing is the main reason I nod along with "horrible programming languages like Javascript", for instance.
My other huge gripe about Elixir when I worked on it last year was lack of IDE autocompletion. This to me is a the #1 dealbreaker in working with dynamically typed languages. Im a horrible speller and having to run my program every time just to find out that I missed a capital letter somewhere in my code is infuriating.
Has Dialyzer and IDEs (VSCode I assume) gotten good enough yet to have decent autocomplete support? When I was using Elixir with WebStorm the experience was less than optimal.
That said, I'm head-over-heels for Elixir. As for autocomplete and such, there's a pretty good project for vscode called something like "Elixir LSP (fork)". Be sure to use the fork version, it's a continuation of the old stale original extension.
In more general terms, the `elixir-lsp` project is pretty solid (and lies underneath the VSCode extension, FWIW), and I've had good luck with it so far in neovim with CoC + coc-elixir.
There are trade-offs.
In my case I left Java for Ruby for web programming in 2006 because working with Rails was infinitely easier than working with Structs. I didn't even know that Ruby was a programming language before Rails.
As someone who has been writing Elixir professionally for 4-ish years, and who is otherwise about as big of a proponent of static types as is possible, this is simultaneously my biggest critique of Elixir, and also a critique that has ended up being more theoretical than not. I wish that it were statically typed, but in practice that hasn't been slowing me down.
Part of the reason for that is that while Elixir is dynamically typed, I've found that it's possible to pretend it is a statically typed language if you squint just right. By that, I mean making judicious use of typespecs [0], dialyzer [1], Norm [2], and just generally constraining the way you write code to mirror the sorts of code that you'd write in a language that offers algebraic data-types, like Haskell. I've been meaning to put together a blog post on what I mean by this, because I often hear people talking about how unusable Dialyzer is, so I feel like my team is a rare example of a large production app that requires that Dialyzer typechecks on every build. This makes large-scale refactoring of our app almost (but not quite) as easy as it would be in Haskell.
The macro system in Elixir also means that it's possible to write libraries like typeclass [3], that offer compile-time guarantees (using compile-time property tests!) that your implementations correctly implement a given interface. I think that as the language evolves, we'll be seeing a lot more examples of macros that move runtime logic into the compilation step, to offer compile-time guarantees and safety. For example, a few weeks ago, I prototyped an experimental library in a few hours that added support for compile-time OCaml style parameterized-modules. [4]
Obviously, all of these techniques don't get you quite as far as proper static typing would (though the Whatsapp team is working on static types for Erlang! [5]), but the rest of the language and the ecosystem is just so well thought out, that I've been okay with that!
[0] https://hexdocs.pm/elixir/typespecs.html
[1] https://github.com/jeremyjh/dialyxir
[2] https://github.com/keathley/norm/
[3] https://github.com/witchcrafters/type_class
And I totally agree with you here:
> I've found that it's possible to pretend it is a statically typed language if you squint just right.
My Elixir code tends to use a lot of pattern matching on structs. It's not real static typing, but it gets me a lot of mileage!
[0] https://gleam.run/index.html
[1] https://gleam.run/faqs.html#how-is-message-passing-typed
We have fully type safe message passing, we just don't have any special syntax for it. I'll update the documentation to make this clearer.
Not disagreeing with you, I guess. It was just surprising/interesting to read.
Sadly I can't find the quote...
Type checking unrestricted message passing would be difficult, but I can imagine a subset that's typeable.
That being said, with the tools I described, you still have type annotations and automated type checking, so the documentation benefits of static types are still present.
Here are a few pain points I've ran into: 1. Typespecs leave something to be desired compared to other type systems 2. Maps vs structs and easily using one in place of the other
Things that my team would like to see: 1. Components in addition to templates as a first-class citizen in Phoenix. My team loves React because of the component model even though we hardly use an insane amount of interactivity.
Surface is a server-side rendering component library that allows developers to build rich interactive user-interfaces, writing minimal custom Javascript.
Features: * An HTML-centric templating language with built-in directives (:for, :if, ...) and syntactic sugar for attributes (inspired by Vue.js). * Components as modules - they can be stateless, stateful, renderless or compile-time. * Declarative properties - explicitly declare the inputs (properties and events) of each component. * Slots - placeholders declared by a component that you can fill up with custom content. * Contexts - allows a parent component to share data with its children without passing them as properties.. * Compile-time checking of components and their properties. * Integration with editor/tools for warnings/errors, syntax highlighting, jump-to-definition, auto-completion (soon!) and more.
And in general Erlang and Elixir have very little automatic type conversions, if any.
But maps and structs are interchangeable, I don't get why using one or the other would make your GenServer code any different. A struct is just a map with an additional field called "__struct__".
Also worth noting (because Elixir was strange and new for everyone at some point) that a lot of what used to be mind-boggling to me in earlier versions of Elixir/Phoenix have become relatively commonplace when dealing with things like React or modern Javascript.
If you've ever handled a JS promise for something as simple as a `fetch()` query and processed results, you _already understand_ how to build code in functional components.
There's much less difference between
fetch(url)
.then(results => doSomething())
.then(results => doSomethingElse())
and doSomething()
|> doSomethingElse()
than it looks like at first blush.That is interesting.
I've gone from Geocities pages to ASP Classic, PHP, WP, Rails, Flask and have been dabbling with Elixir and Phoenix for a bit.
So far LiveView (and some bits of Elixir in general) has been the only time in my career where I felt like I'm seriously struggling hard to understand how things come together and how to solve real but fairly common web dev problems without asking for help (mainly with LV). I've had to ask for help on IRC / the forums an embarrassing amount of times. Where as with Rails and Flask almost everything except the most crazy problems were solvable with a little bit of Googling and self exploration.
Now, I'm not saying Elixir and Phoenix is bad at all. I'm still happily using it to build my next big project (for all of the reasons that everyone else uses it for), and the Programming Phoenix book is a great learning resource for building regular web apps with Phoenix, but there's nothing like that for LiveView.
But on the bright side, Chris (the author of Phoenix) and Jose (the author of Elixir) are remarkably responsive to help the community. Lots of other folks are super helpful too. That and once things start to click, you can write some really pleasant looking code and actually understand what it does a few months later.
And also, it's easy to find help for Flask and Rails by googling because... It's been around longer. LiveView has been GA for less than a year, and even some major shifts have come around (like mount/2 suddenly becoming mount/3 in one of the last releases).
But then I look at the Live View equiv. in other frameworks. All of which came well after Live View since they are modeled after it.
For example: https://laravel-livewire.com/docs/quickstart in Laravel. I'm not a Laravel developer but their documentation is next level amazing and the author of the library even has a screencast series going that is fully up to date where he builds a real app using Live Wire to demo how it works in practice.
I don't even know Laravel at all, but I can skim their docs and get a feel for how to solve real problems like validation, authorization, pagination, and everything else is focused on practical examples and explaining things very well in the process.
But with Live View, the docs are not anything like this. It reads more like a low level guide on the API itself, not how things work in practice and when you should use certain things or how to write common web dev features. After reading the docs, you're left wondering "ok, but how do I get started using Live View in practice?", IMO at least.
There's also the Rails equiv. which is https://expo.stimulusreflex.com/. Besides the docs there's a bunch of real practical examples, with a live demo of how they all work without having to install anything. With Live View, we have an example repo but it's very out dated at the moment to the point where you can't even follow it with the docs and it seems to not be based on best practices anymore since new things were released with LV.
The struggle is mainly around the nuisances of how certain things work with LV and what the implications are of using X vs Y with practical examples. I wrote a forum post asking for help about this the other day at https://elixirforum.com/t/concrete-examples-of-when-to-use-l....
Then there's other things like wanting to do authentication. I know in theory you'll need to create a controller action to create / delete the session and there's phx-trigger-action to submit a LV form to a controller but there's no code examples anywhere on how to pull this off and get a "current_user" like you normally would with a non-LV set up. But authentication is one of the most important things in any app.
I remember thinking, "This feels messy..." but not really knowing why. And I was brand new and barely a developer!
But I think you nailed it. It's because of the Erlang continuity.
a = [foo: "bar", foo: "baz"]
Keyword.get(a, :foo) #==> gives you "bar"
a[:foo] #==> gives you "bar"
my_struct = struct(SomeStructWithFoo, a)
my_struct.foo #==> gives you "baz"
But really, this minor, minor inconsistency is near the top of my list of complaints, so... Take that into consideration.Also, if you start coding in Elixir a lot, you will write better Javascript by importing good practices in from Elixir.
They are similar but they are not the same thing and are not to be used in the same places.
That sounds useful. Do you plan to make a product out of that?
Edit: Saw the link to your blog below https://medium.com/build-ideas/
Thank you.
They hit a sweet spot. Enough rigor, enough limitation, at the language level, to force you to do things "the right way" for a broad swath of problems (there are certain problems they're poorly suited for), but enough simplicity and freedom to keep a minimal learning curve, and minimal runtime complexity to figure out issues.
The focus on fault tolerance also means once you learn to use the app structure, with supervisors, you mostly have just one concern for reliability ("how do I start this into a known, good, consistent state?"), rather than hundreds ("what happens if -this- fails? Or that? Or that other thing?").
Haskell and Scala both have a huge surface area to learn before you're productive, let alone anything approaching fault tolerant, and even after years of use, there's still a lot of hidden gotchas with them. I've seen teams take Erlang, and their very first project just...worked. None of their lessons learned caused production issues, and no weirdness; the three issues that made it to prod I can even think of were, variously, one that wasn't user facing (just a log entry indicating something wasn't handled right; supervisor took care of it), one where performance started to slow (and it was due to having written an O(N^2) algorithm accidentally, and not testing at higher loads than production), and one where a low level C driver made an unnecessary reverse DNS lookup that, when the caches flushed, caused things to hang, which became an issue when load increased, and which should have been circuit broken (but which instead caused a failover to another node).
I've never had that experience with another language. The ramp up time to productivity was longer, the production issues caused by us missing something about the language were more frequent, the production issues caused by us failing to expect or handle a failure condition were more frequent, etc.
That said, Elixir and Phoenix do raise the bar a little. Elixir has a larger surface than Erlang (only a few concepts, but a lot of ramifications when it comes to macros if you haven't used them in other languages), and Phoenix is reliant on a fair bit of magic so you need to read the docs and it may take a little bit of work to feel happy with things. But even when I knew nothing about it, picking it was easy because of the experience I had with Erlang.
Thank you for this comment. I have used Elixir for a couple of years and is a concept I have always understood but have never been able to explain coherently and succinctly.
In contrast, Elixir is super simple. I can describe the language in a single HN comment. Everything is just a function and they're inside modules. That's pretty much it. When I was picking up Elixir, I started off writing an E-Commerce platform from scratch with Elixir. When I was about to finish it, I was thrown into a 8 month long PHP project. When I was finally done with it and returned back to work on my E-Commerce project, I was able to just open up the code in a text editor and immediately understand what was happening - without having to re-learn the language. That is a the "Joy" part for me.
One more "Joy" of writing in Elixir is you don't need to worry about memory usage or performance that much. It's pretty efficient unlike Ruby or PHP even. So, I can just throw this entire application in a $5 Digital Ocean droplet and watch it handle insane amounts of traffic. And this is without optimizing anything. You will probably able to squeeze more with caching, optimizations, etc.
As for re-thinking the way you write code, it's mostly to do with pipe operations. Piping was a new concept for me jumping from Ruby. It's absolutely powerful, concise and productive. In Elixir, if you don't write good code, the compiler will warn you AND show you examples of how (and sometimes why) it should be re-written. Eg. If you write a nested case statement which many people subconsciously may do coming from OOP backgrounds, the compiler will ask you to re-think your code. And stuff like this really challenges you, but actually doesn't do anything destructive to your code - your code will still compile and function, but you will feel that tingling inside - "Hmm, maybe I should consider re-writing that?"
THAT is the balance. Before Elixir, I also used Scala. Took me 3 months to fully learn the language from the 700+ pages book on Scala. I absolutely love Scala and the JVM. It's powerful, but there's just too many ways to do the same thing in Scala to keep track of. So, it goes back to my first point on looking at the language after 6 months without the need to re-learn it. I would still try Scala at some point because JVM is very powerful. But, will it replace Elixir? Absolutely not.
This sort of absolutism does nobody any favours. Functional zealotry is every bit as bad as OO zealotry.
I've heard only good things about Elixir/Phoenix, so I'm thinking about picking it up for my next project. One thing about JS/TS is that the ecosystem is well developed, especially on the deployment side. For side projects especially, I like to minimize my dev ops configs and utilize "serverless" type stuff (i.e. Zeit/Vercel, FaaS, etc). I haven't seen much of this in Elixir-land, but hey, maybe that's an area I can help innovate in.
On a smaller scale Feedback Panda very successfully bootstrapped to a 55k MRR SaaS and was aquired in under 2 years: https://youtu.be/vaXp81OxxK0
Edit: Not to be too self-promotional, but AFIK I've created more free Elixir-learning screencasts than anyone on YouTube. I cover a pretty broad range of topics from total beginner to some fairly niche things: https://youtu.be/z1nKbzZiRtY
I'm not affiliated with them in any way. I'm just a happy customer.
Besides what was listed already, if you're looking for info on people running Elixir / Phoenix in production there's this podcast: https://runninginproduction.com/tags/phoenix
One guy was pumping over 7 billion log events a month through it.
Another one (remote.com episode) serves 100k+ daily requests.
Each podcast episode goes through how the app was built and how it's deployed at a high level, but still has enough details to apply those things back to your own projects.
With that said, if anyone is running Elixir or Phoenix in production and sees this and wants to be on the show, just head over to the above link and click "become a guest" on the top right. I'd love to have you on, both big and small sites are welcome.
https://elixir-lang.org/learning.html - Start here. It starts off kind of abstract, and then works its way into the concrete (from my experience) -- but is very easy to pick up.
> What major sites?
Last I heard, Discord was serving ~5 million concurrent users, and that was well before compelled isolation.
Bleacher Report is also using Elixir, and serving something like 100k requests per minute just to mobile clients.
Bet365 handles as much as 100k monetary transactions during a big sporting event like a Champions League final.
Including GenServers? I find writing GenServers to be needlessly painful. Their syntax is one of my least favorite things about Elixir.
You can add your own abstractions with macros on top of that if you like.
Erlang does it too.
If you want to have state you do recursion and call itself with the updated value.
fn me(iter) -> me(iter+1) end
Is it as performant as technologies like Django / Ruby?
That said, there are some ways in which Phoenix/LiveView is extremely "scalable".
For example, pushing messages to 2 million simultaneous websocket connections: https://www.phoenixframework.org/blog/the-road-to-2-million-...
... and running on the BEAM gives all sorts of exciting horizontal scaling possibilities on top of that.