Idiomatic function names for Elixir/Phoenix controller helpers
I'm writing some get
helpers in my Phoenix web app. In rails, you would generally name (or have method missing) helpers like find_account_by_email(email)
, etc.
With pattern matching seeming so core to Elixir/Erlang, I'm wondering if I'm better of writing my helpers like:
def get_account(email: email) do
# ...
end
Phoenix stubs out a get_account(id)
method, so it feels to me that reusing the name with pattern matching is more idiomatic?
elixir phoenix-framework
add a comment |
I'm writing some get
helpers in my Phoenix web app. In rails, you would generally name (or have method missing) helpers like find_account_by_email(email)
, etc.
With pattern matching seeming so core to Elixir/Erlang, I'm wondering if I'm better of writing my helpers like:
def get_account(email: email) do
# ...
end
Phoenix stubs out a get_account(id)
method, so it feels to me that reusing the name with pattern matching is more idiomatic?
elixir phoenix-framework
add a comment |
I'm writing some get
helpers in my Phoenix web app. In rails, you would generally name (or have method missing) helpers like find_account_by_email(email)
, etc.
With pattern matching seeming so core to Elixir/Erlang, I'm wondering if I'm better of writing my helpers like:
def get_account(email: email) do
# ...
end
Phoenix stubs out a get_account(id)
method, so it feels to me that reusing the name with pattern matching is more idiomatic?
elixir phoenix-framework
I'm writing some get
helpers in my Phoenix web app. In rails, you would generally name (or have method missing) helpers like find_account_by_email(email)
, etc.
With pattern matching seeming so core to Elixir/Erlang, I'm wondering if I'm better of writing my helpers like:
def get_account(email: email) do
# ...
end
Phoenix stubs out a get_account(id)
method, so it feels to me that reusing the name with pattern matching is more idiomatic?
elixir phoenix-framework
elixir phoenix-framework
edited Nov 11 at 23:54
Sheharyar
44k10106158
44k10106158
asked Nov 11 at 17:47
purplelulu
2713
2713
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Welcome to Stack Overflow!
While phoenix-framework might be called the "Rails for Elixir", the design patterns and the architecture considerations are very different. Exposing random functions that "might be used for a bunch of things" isn't really part of the philosophy.
But the good part is, that if your use-case does call for something like this, it's very easy to extend existing functionality using Macros, Behaviours or Protocols. For your simple use-case, you can indeed create a generic method (or a set of methods), but I would lose the tuple
:
defmodule Account do
def get(clauses) do
Repo.get_by(Account, clauses)
end
end
You can call it using:
Account.get(email: "user@example.com")
But I would argue if replacing one one-liner with another, truly added enough value to your codebase to warrant it.
Side Note: I actually created a library to add Rails-style model helpers to Ecto schemas in Elixir apps to ease-in Rails developers to Phoenix, exposing methods similar to what active-record does. Also see the note about complex queries.
"But I would argue to see if it truly added enough value to your codebase by replacing one one-liner with another.", by this do you imply I should be using Repo directly in my other code? It actually getting called from a background genserver and including Repo everywhere seems like it's breaking the encapsulation laid out by phoenix's "contexts".
– purplelulu
Nov 12 at 3:17
I agree that repeatingRepo
calls again and again can be tiring and prone to bugs, so ideally you should have a module encapsulating important and repeated queries. But for one-off calls encapsulating the logic in another method does not make sense. You should just use existing library methods.
– Sheharyar
Nov 12 at 3:45
AlsoYourApp.Repo
actually does behave like a separate context in Phoenix applications by abstracting out how your application interacts with a database or another schema. Unless your queries are getting very complicated, you shouldn't abstract them out further in another context.
– Sheharyar
Nov 12 at 3:48
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251494%2fidiomatic-function-names-for-elixir-phoenix-controller-helpers%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Welcome to Stack Overflow!
While phoenix-framework might be called the "Rails for Elixir", the design patterns and the architecture considerations are very different. Exposing random functions that "might be used for a bunch of things" isn't really part of the philosophy.
But the good part is, that if your use-case does call for something like this, it's very easy to extend existing functionality using Macros, Behaviours or Protocols. For your simple use-case, you can indeed create a generic method (or a set of methods), but I would lose the tuple
:
defmodule Account do
def get(clauses) do
Repo.get_by(Account, clauses)
end
end
You can call it using:
Account.get(email: "user@example.com")
But I would argue if replacing one one-liner with another, truly added enough value to your codebase to warrant it.
Side Note: I actually created a library to add Rails-style model helpers to Ecto schemas in Elixir apps to ease-in Rails developers to Phoenix, exposing methods similar to what active-record does. Also see the note about complex queries.
"But I would argue to see if it truly added enough value to your codebase by replacing one one-liner with another.", by this do you imply I should be using Repo directly in my other code? It actually getting called from a background genserver and including Repo everywhere seems like it's breaking the encapsulation laid out by phoenix's "contexts".
– purplelulu
Nov 12 at 3:17
I agree that repeatingRepo
calls again and again can be tiring and prone to bugs, so ideally you should have a module encapsulating important and repeated queries. But for one-off calls encapsulating the logic in another method does not make sense. You should just use existing library methods.
– Sheharyar
Nov 12 at 3:45
AlsoYourApp.Repo
actually does behave like a separate context in Phoenix applications by abstracting out how your application interacts with a database or another schema. Unless your queries are getting very complicated, you shouldn't abstract them out further in another context.
– Sheharyar
Nov 12 at 3:48
add a comment |
Welcome to Stack Overflow!
While phoenix-framework might be called the "Rails for Elixir", the design patterns and the architecture considerations are very different. Exposing random functions that "might be used for a bunch of things" isn't really part of the philosophy.
But the good part is, that if your use-case does call for something like this, it's very easy to extend existing functionality using Macros, Behaviours or Protocols. For your simple use-case, you can indeed create a generic method (or a set of methods), but I would lose the tuple
:
defmodule Account do
def get(clauses) do
Repo.get_by(Account, clauses)
end
end
You can call it using:
Account.get(email: "user@example.com")
But I would argue if replacing one one-liner with another, truly added enough value to your codebase to warrant it.
Side Note: I actually created a library to add Rails-style model helpers to Ecto schemas in Elixir apps to ease-in Rails developers to Phoenix, exposing methods similar to what active-record does. Also see the note about complex queries.
"But I would argue to see if it truly added enough value to your codebase by replacing one one-liner with another.", by this do you imply I should be using Repo directly in my other code? It actually getting called from a background genserver and including Repo everywhere seems like it's breaking the encapsulation laid out by phoenix's "contexts".
– purplelulu
Nov 12 at 3:17
I agree that repeatingRepo
calls again and again can be tiring and prone to bugs, so ideally you should have a module encapsulating important and repeated queries. But for one-off calls encapsulating the logic in another method does not make sense. You should just use existing library methods.
– Sheharyar
Nov 12 at 3:45
AlsoYourApp.Repo
actually does behave like a separate context in Phoenix applications by abstracting out how your application interacts with a database or another schema. Unless your queries are getting very complicated, you shouldn't abstract them out further in another context.
– Sheharyar
Nov 12 at 3:48
add a comment |
Welcome to Stack Overflow!
While phoenix-framework might be called the "Rails for Elixir", the design patterns and the architecture considerations are very different. Exposing random functions that "might be used for a bunch of things" isn't really part of the philosophy.
But the good part is, that if your use-case does call for something like this, it's very easy to extend existing functionality using Macros, Behaviours or Protocols. For your simple use-case, you can indeed create a generic method (or a set of methods), but I would lose the tuple
:
defmodule Account do
def get(clauses) do
Repo.get_by(Account, clauses)
end
end
You can call it using:
Account.get(email: "user@example.com")
But I would argue if replacing one one-liner with another, truly added enough value to your codebase to warrant it.
Side Note: I actually created a library to add Rails-style model helpers to Ecto schemas in Elixir apps to ease-in Rails developers to Phoenix, exposing methods similar to what active-record does. Also see the note about complex queries.
Welcome to Stack Overflow!
While phoenix-framework might be called the "Rails for Elixir", the design patterns and the architecture considerations are very different. Exposing random functions that "might be used for a bunch of things" isn't really part of the philosophy.
But the good part is, that if your use-case does call for something like this, it's very easy to extend existing functionality using Macros, Behaviours or Protocols. For your simple use-case, you can indeed create a generic method (or a set of methods), but I would lose the tuple
:
defmodule Account do
def get(clauses) do
Repo.get_by(Account, clauses)
end
end
You can call it using:
Account.get(email: "user@example.com")
But I would argue if replacing one one-liner with another, truly added enough value to your codebase to warrant it.
Side Note: I actually created a library to add Rails-style model helpers to Ecto schemas in Elixir apps to ease-in Rails developers to Phoenix, exposing methods similar to what active-record does. Also see the note about complex queries.
edited Nov 12 at 4:47
answered Nov 11 at 23:53
Sheharyar
44k10106158
44k10106158
"But I would argue to see if it truly added enough value to your codebase by replacing one one-liner with another.", by this do you imply I should be using Repo directly in my other code? It actually getting called from a background genserver and including Repo everywhere seems like it's breaking the encapsulation laid out by phoenix's "contexts".
– purplelulu
Nov 12 at 3:17
I agree that repeatingRepo
calls again and again can be tiring and prone to bugs, so ideally you should have a module encapsulating important and repeated queries. But for one-off calls encapsulating the logic in another method does not make sense. You should just use existing library methods.
– Sheharyar
Nov 12 at 3:45
AlsoYourApp.Repo
actually does behave like a separate context in Phoenix applications by abstracting out how your application interacts with a database or another schema. Unless your queries are getting very complicated, you shouldn't abstract them out further in another context.
– Sheharyar
Nov 12 at 3:48
add a comment |
"But I would argue to see if it truly added enough value to your codebase by replacing one one-liner with another.", by this do you imply I should be using Repo directly in my other code? It actually getting called from a background genserver and including Repo everywhere seems like it's breaking the encapsulation laid out by phoenix's "contexts".
– purplelulu
Nov 12 at 3:17
I agree that repeatingRepo
calls again and again can be tiring and prone to bugs, so ideally you should have a module encapsulating important and repeated queries. But for one-off calls encapsulating the logic in another method does not make sense. You should just use existing library methods.
– Sheharyar
Nov 12 at 3:45
AlsoYourApp.Repo
actually does behave like a separate context in Phoenix applications by abstracting out how your application interacts with a database or another schema. Unless your queries are getting very complicated, you shouldn't abstract them out further in another context.
– Sheharyar
Nov 12 at 3:48
"But I would argue to see if it truly added enough value to your codebase by replacing one one-liner with another.", by this do you imply I should be using Repo directly in my other code? It actually getting called from a background genserver and including Repo everywhere seems like it's breaking the encapsulation laid out by phoenix's "contexts".
– purplelulu
Nov 12 at 3:17
"But I would argue to see if it truly added enough value to your codebase by replacing one one-liner with another.", by this do you imply I should be using Repo directly in my other code? It actually getting called from a background genserver and including Repo everywhere seems like it's breaking the encapsulation laid out by phoenix's "contexts".
– purplelulu
Nov 12 at 3:17
I agree that repeating
Repo
calls again and again can be tiring and prone to bugs, so ideally you should have a module encapsulating important and repeated queries. But for one-off calls encapsulating the logic in another method does not make sense. You should just use existing library methods.– Sheharyar
Nov 12 at 3:45
I agree that repeating
Repo
calls again and again can be tiring and prone to bugs, so ideally you should have a module encapsulating important and repeated queries. But for one-off calls encapsulating the logic in another method does not make sense. You should just use existing library methods.– Sheharyar
Nov 12 at 3:45
Also
YourApp.Repo
actually does behave like a separate context in Phoenix applications by abstracting out how your application interacts with a database or another schema. Unless your queries are getting very complicated, you shouldn't abstract them out further in another context.– Sheharyar
Nov 12 at 3:48
Also
YourApp.Repo
actually does behave like a separate context in Phoenix applications by abstracting out how your application interacts with a database or another schema. Unless your queries are getting very complicated, you shouldn't abstract them out further in another context.– Sheharyar
Nov 12 at 3:48
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251494%2fidiomatic-function-names-for-elixir-phoenix-controller-helpers%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown