Use "only" instead of "except" when generating api routes?

Hello.
Currently, this is what phoenix generates for JSON routes:

scope "/api", MyAppWeb do
    pipe_through :api
    resources "/users", UserController, except: [:new, :edit]
end

Going with the “explicit is better than implicit” methodology, I would suggest that the generated code uses “only” instead:

scope "/api", MyAppWeb do
    pipe_through :api
    resources "/users", UserController, only: [:index, :create, :show, :update, :delete]
end

It’s a bit longer, but it’s much clearer, especially to someone who didn’t come from other frameworks which has similar options.

Thoughts?

7 Likes

Definitely, :+1:.

3 Likes

I’m 50/50 on this. I do like “explicit is better than implicit” for some things, but also “convention over configuration” in some points. I think there is a thin line between them which is where we should be comfortable. If we should respect “explicit is better than implicit” as a unbreakable rule, the resources/2 function shouldn’t even exist at all.

I agree with you, but for this particular change, I don’t believe that it affects or violates the convention over configuration philosophy.
As you said, there are some stuff that would greatly benefit from being somewhat implicit, but there are also stuff that would make phoenix easier to understand, especially for newcomers.
Actually, now that I think about it, it’s more of a “whitelisting vs blacklisting” issue rather than “explicit vs implicit”.
I would much rather know what routes I’m getting than what routes I’m not getting.
Again, I’m interested to see what the community thinks of this change. I’m glad and honored that Jose thinks it’s a good idea.
@chrismccord @josevalim I’m more than happy to submit a PR if you and the community think it’s a “go”.

2 Likes

As I said, I’m 50/50… I really don’t have a strong opinion for any side…

I always use only, because it is explicit, and does not expect prior knowledge of the reader.

1 Like

The issue with except is that it requires me to do the math in my head of what is actually being used… while I could just read it out. :slight_smile:

1 Like

Well, that’s a good argument. But I particularly only use except when I have a simple case, which is easier explained like “a resource that cannot be deleted” than “a resource that can be listed, shown, created or updated”.

Anyways, for the JSON routes, I’m comfortable with any of the choices, only or except. I particularly use to save this options on a var to keep my router a little DRYer:

scope "/" do
  pipe_through :api 

  opts = [except: [:new, :edit]]

  resources :orgs, opts
  resources :users, opts
end