Idiomatic function names for Elixir/Phoenix controller helpers









up vote
1
down vote

favorite












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?










share|improve this question



























    up vote
    1
    down vote

    favorite












    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?










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      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?










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 23:54









      Sheharyar

      43.5k10106158




      43.5k10106158










      asked Nov 11 at 17:47









      purplelulu

      223




      223






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          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.






          share|improve this answer






















          • "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










          • 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










          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',
          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
          );



          );













          draft saved

          draft discarded


















          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








          up vote
          4
          down vote



          accepted










          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.






          share|improve this answer






















          • "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










          • 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














          up vote
          4
          down vote



          accepted










          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.






          share|improve this answer






















          • "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










          • 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












          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          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.






          share|improve this answer














          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 at 4:47

























          answered Nov 11 at 23:53









          Sheharyar

          43.5k10106158




          43.5k10106158











          • "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










          • 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
















          • "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










          • 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















          "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

















          draft saved

          draft discarded
















































          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.




          draft saved


          draft discarded














          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





















































          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







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3

          Museum of Modern and Contemporary Art of Trento and Rovereto