
Programming in Elixir with the Phoenix Framework - gogogarrett
http://gogogarrett.sexy/programming-in-elixir-with-the-phoenix-framework-building-a-basic-CRUD-app/
======
skrebbel
Nice writeup. I hadn't looked at Phoenix yet.

It seems that many Elixir people come from Ruby. One thing we should be wary
of is making shallow ports of Ruby concepts into Elixir, when there's much
more elegant approaches available. We, software engineers, have a tendency of
doing this: for example, there's a whole bucketload of C# libraries that have
Java roots (NHibernate, Spring.NET), and every single one of them is a bloated
piece of nonsense that doesn't make good use of C#'s strengths. We should
avoid making Elixir a Ruby-on-Erlang when it has the potential to be so much
more.

I haven't figured out yet to what extent Phoenix does this, but this blog post
highlights some things that make me wonder.

For instance, the router confuses me. Is there any point in making an explicit
router over just splitting the URL by "/" into a list and then just pattern
matching on that?

e.g.

    
    
        def get(["users", user], _params) do
            # show stuff
        end
    
        def post(["users"], _params) do
            # store stuff
        end
    
        def get([], _params) do
            # show root page
        end
    

Is Phoenix's routing somehow more powerful than that, for reasons that I'm
missing, or is it just a Rails-ism blatantly ported over to Elixir?

~~~
josevalim
We had a lot of conversations about this and it was a somewhat common topic
during ElixirConf
([http://www.confreaks.com/events/elixirconf2014](http://www.confreaks.com/events/elixirconf2014)).
On my keynote I addressed exactly your concerns: it is fine to do "X in
Elixir", which is a very helpful learning exercise, but remember to revisit it
later, otherwise you will have a project that misuses or does not fully uses
this new environment.

Dynamo ([https://github.com/dynamo/dynamo](https://github.com/dynamo/dynamo))
is such an example. It was one of the first web frameworks that showed up for
Elixir and it got a lot of things wrong. However, out of the lessons learned,
we got:

* The configuration layer that comes with Elixir. Elixir has the concept of applications (the same as Erlang/OTP applications) which you can be stopped and started as a unit. So the whole mechanism on how you define applications and configure them is defined by the language/runtime and consequently that is one area where Phoenix is considerably different than Rails.

* I absolutely hated the view layer in Dynamo (which attempted to be too Rails like and did not fit Elixir). Chris got the lessons learned from that and provided an absolute great view layer for Phoenix, where you have the concept of views (modules) and templates (embedded into those modules). Dispatching to views is done with pattern matching (similar to your example above). There aren't any global helpers and the views are easily testable in isolation.

* Plug ([https://github.com/elixir-lang/plug](https://github.com/elixir-lang/plug)) also came out of Dynamo. It has the same goals as Rack (because you can't really diverge from the goals): a specification for web servers and an API for connecting applications. However, implementation wise, it is very different than Rack. Rack applications usually build on top of a middleware stack which receives a request environment, passing through all layers, and then returns a response that once again goes through all layers. This approach is what makes Rack uncomfortable for chunking, connection upgrades and so on. In Plug, the plug stack is one-way, you only get the connection (instead of a request and response) because we assume a response can be sent through the connection at any point during the stack (disclaimer: I am both Rack and Plug committer).

The list could go on. :) Our build tool, Mix ([http://elixir-
lang.org/docs/mix](http://elixir-lang.org/docs/mix)), is inspired on Clojure's
lein. The most used database layer, Ecto ([https://github.com/elixir-
lang/ecto](https://github.com/elixir-lang/ecto)), borrows a lot from LINQ
(hard to have an ORM when you don't have objects!). There are even some other
smaller examples in Phoenix like the current attempts to remove the
singular/plural nouns that we see a lot in Rails and, in my opinion, causes a
lot of confusion.

~~~
tacticiankerala
I really appreciate your attitude. You are really inspirig people like me to
understand it is okay to make mistakes :). Also, every mistake teaches you
something. Learn from your mistakes and keep building things that matters :).

Needless to say, I am becoming a fan of yours :)

~~~
josevalim
Thanks for the nice words! I appreciate it!

------
tiffanyh
I'd love to see someone who knows Exilir/Phoenix to submit it to the Web
Framework Performance Shootout [1].

[1]
[http://www.techempower.com/benchmarks/](http://www.techempower.com/benchmarks/)

Edit: typo

~~~
chrismccord
I'd love to see Phoenix represented there. I'll see what's required to get it
added.

~~~
tiffanyh
Awesome! Really hope you submit Phoenix as well to the benchmark.

Info below on how to submit your test suite.

[http://www.techempower.com/benchmarks/#section=code&hw=peak&...](http://www.techempower.com/benchmarks/#section=code&hw=peak&test=json)

------
chrismccord
For those curious about the realtime layer in Phoenix, I have a screencast
where we live code a Phoenix chat app. We've added a View layer since the
recording, so ignore the rendering hack:

[http://chrismccord.com/blog/2014/05/06/how-to-build-an-
elixi...](http://chrismccord.com/blog/2014/05/06/how-to-build-an-elixir-chat-
app-with-phoenix/)

~~~
hackerboos
I'd love to replace Atmosphere[1] with a Elixir solution. Is there support for
a websocket fallback in Phoenix?

[1] - [http://async-io.org/](http://async-io.org/)

------
bshimmin
Has anyone used both Phoenix and Chicago Boss in anger (to at least some
extent)? It'd be great to see a comparison between the two.

I'll start by saying that Phoenix seems specifically built for Elixir, while
Chicago Boss appears to be an Erlang framework which works fine with Elixir
after a minimal amount of initial configuration upfront.

------
jbverschoor
What a coincidence. Just learnt about Elixer and Phoenix this morning, and I
see this :-)

Love it so far and the fact Jose created it.

