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: .

OvermindDL1

OvermindDL1

I’d probably just make:

defp get_url(id) when is_integer(id) and id >=0 do
  "a_url_removed_because_not_public#{if(id > 0, do: id)}"
end

Or more readably as (since I like to pull out arg calculations onto their own lines:

defp get_url(id) when is_integer(id) and id >=0 do
  sid = if(id > 0, do: id, else: nil)
  "a_url_removed_because_not_public#{sid}"
end

Where Next?

Popular in Questions 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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
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
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
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

We're in Beta

About us Mission Statement