Nefcairon

Nefcairon

To improve a function

Hello,

I am still an Elixir newbie. To learn I keep on asking questions like this…
Can I improve this two functions? It feels like but I do not know how.

  defp get_url(team_id) when team_id > 0 do
    "a_url_removed_because_not_public" <>
      Kernel.inspect(team_id)
  end

  defp get_url(team_id) when team_id == 0 do
    "a_url_removed_because_not_public" 
  end

besides doing them as one-liners:

  defp get_url(team_id) when team_id > 0; do:  "a_url_removed_because_not_public" <> Kernel.inspect(team_id)

  defp get_url(team_id) when team_id == 0; do:  "a_url_removed_because_not_public" 
 

It still looks as it contains too much redundant code.

Marked As Solved

Qqwy

Qqwy

TypeCheck Core Team

Quite possibly!
Here are a couple of questions for you, that might help you:

  • Are the URLs in the two function clauses the same? If they are, you could extract this out to either another function, or to a module attribute.
  • Instead of using Kernel.inspect (which is meant to only be used for logging/introspection), you probably want to use to_string, or string interpolation. Also, there is no reason to use Kernel. in front of a function, because the Kernel module is always (unless explicitly overridden) imported into a module.
  • Why have the get_ in the name of the function? Depending on the context, this might matter for the meaning of the function name but maybe it is fluff that does not add extra meaning.

Also Liked

NobbZ

NobbZ

I’d add a guard is_integer(team_id) as any non numeric type is greater than 0.

Also, I’d use string interpolation rather than inspect.

Inspection in general is something I’d only use for logging, never to create a user facing string.

kokolegorille

kokolegorille

You might use

defp get_url(0) do ...
Qqwy

Qqwy

TypeCheck Core Team

This is not required. And if you really want to put a verb in there, get is about as vague as you can be, since it means so many different things :sweat_smile:. Using a verb like get or fetch as part of a function name would make more sense if you retrieve the URL value from somewhere else. In this case, where it is something the function itself calculates, I would just leave it out:

defmodule Team do
  def url(team_id) do
    ...
  end
end

Here, it is clear that Team.url(foo) will return an URL based on foo that has to do with a ‘Team’.


Of course, naming things quickly enters the realms of personal preference. It is very easy to spend all afternoon bikeshedding about it. It’s possibly the least important suggestion of all of the suggestions that people have given in this topic :wink: .

Where Next?

Popular in Questions Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement