braindeaf

braindeaf

Do you rate the Programming Phoenix book?

By day I’m a Ruby dev, I’ve taken a few days off work to work through all the stacked up Elixir books I have and I’m really just hoping to deep dive into Elixir and to knock out an application. I’ve been working through the Programming Phoenix book and I’m wondering if the code quality is what you’d expect in your average Phoenix project rather than a play project like the one you build in here.

I’m seconds guessing if the code is dumbed down for new users and overly explicit. If the Context(s) are too much, I’ve got so many files open and getting muddled just trying to work on with a single controller. There are lots of different ways to say authenticate a user on a controller, and it’s really interesting to see but am I getting more options than I need?

There’s a case where the select options for a Category select tag are loaded in a plug defined in a controller, and then the options are extracted in a Context, rather say in a View which seems more appropriate?! Or is the view avoiding touching the database at all.

Then when it comes to testing a controller, the examples seem way too much. e.g. VideoControllerTest where every action is tested to suggest that the user should be logged out. I get it a bad actor could try every route. But is this just for completeness and to prove what you can do?

To some degree I’m expecting to find all the familiarity and equivalents that I use with Rails in just one book, which is probably insane. So far I’m creating objects with basic scaffolds and I’m reaching for the equivalent of nested attributes already because I know my interface isn’t going to be quite so simplified.

Anyway…I’m not really sure what my point is. Is this book a good book to build a project alongside or just to fire out the possibilities and off you go to kill a bear in the woods with your new weapons?

RobL

Most Liked

Exadra37

Exadra37

I extended the app built on the book and this is the result https://video-hub.exadra37.com/

The code is the way it is encouraged by the creator of the framework, that is present in all the mix Phoenix generators.

Personally I dislike the approach and by far I prefer @PragDave approach that resembles my old Resource Action Pattern approach to structure code in a clean and efficient way, but this affirmations are always relative, and many may disagree.

I would recommend you to take the Elixir for Programmer 2 video course, and see by yourself what it looks like to organize and segment your code in a much clean way and with the Single Responsibility Principle in mind.

tfwright

tfwright

Haven’t read that book, but if you are coming from Ruby/Rails as I did, you will probably find the “blessed path” in Elixir/Phoenix a bit heavy at first. Rails by default is a real lean MVC pattern but Phoenix encourages something more like a S(chema) C(context) C(controller) V(iew) T(emplate), with similar separation conventions as you would expect in Rails to see between V/M (formatting in a model? db calls in a view? no).

But as others have pointed out compared to Rails, Phoenix is far lighter when it comes to imposing this convention. It is fairly easy to break convention and institute your own patterns. In Rails if you move a file or even just name it slightly wrong, it can act as if it doesn’t even exist. Phoenix doesn’t care where your modules are or what they are called. It exposes way more in terms of request processing and templating logic so you can easily make it behave however you want.

Personally, I don’t see a need for views/templates in my RESTful API only application. And I use contexts as namespaces, I don’t hesitate to put business logic specific to a single schema in the schema module (or child modules)–even if I need to query the db there. I have found this to be a bit more of a challenge with teams, since Rails strong conventions left little room for contention, you had to have a very compelling reason to follow a different pattern and you needed to identify it very early on in the dev process. In Phoenix people tend to approach everything slightly differently, not as crazy as the JS world, but definitely more variation between Phoenix projects than Rails in my experience.

gpopides

gpopides

I want to mention that i don’t have a ruby/rails background

Separation of View from DB is something I would normally avoid.

Is this common in rails? Because in other languages/framework it’s very common to seperate your view from your database.

many ways the achieve the same goal

Isn’t this one of the biggest problems when writing ruby?

Of course you can have contexts that abstract things, but if you don’t need contexts don’t use them

You can do Repo.all(Users) in your view if you would like, but this creates problems as the codebase grows. That what contexts solve essentially.

Contexts are similar to Controller -> Service -> Repository pattern in my view which is used a lot.

As i mentioned previously, if your intentions are to learn phoenix and elixir, don’t focus on contexts. You will use them when you will meet the problems that they are intented to solve, but if you still don’t like them you can just ignore them. It’s not enforced.

Last Post!

Exadra37

Exadra37

I loved PragDave way because I was already doing this type of approach in PHP, where I would have an entrypoint to the code core (application in the Elixir course) that could then be used from Controllers, CLI, Websockets, or whatever interface we would decide to use, and they were packaged as vendor dependencies, instead of being stuffed inside the Laravel Framework. This approach had the advantage of being a lot easier to upgrade Laravel versions, because the framewrok was just being used as a placeholder to bootstratp and run our code from the vendor folder.

Where Next?

Popular in Discussions Top

sergio
There’s a new TIOBE index report that came out that shows Elixir is still not in the top 50 used languages. It also goes on to call Elix...
New
MarioFlach
Hello, I want to share a project I’ve been working on for a while: https://github.com/almightycouch/gitgud Background Some time ago I ...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
New
nburkley
AWS re:Invent is on at the moment with some interesting announcements. One new feature in particular is the Lambda Runtime API for AWS La...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40042 209
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement