Nefcairon

Nefcairon

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.

Showing Posts 1 to 10

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

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

Nefcairon OP

Thanks, this is putting me forward :slight_smile:

Nefcairon

Nefcairon OP

Hm. I thought functions should be a verb as they are doing something with the data they get (I think that goes back to university days).

Background of my functions is that another function needs a url (like “domain.com?n=1”) and some additional paramenter (so “domain.com?n=1&m=[team_id]” if some condition is met (team_id is not 0).

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

Nefcairon

Nefcairon OP

Allright. Thank you :slight_smile:

Nefcairon

Nefcairon OP

A follow up question:
I have to an request data from an foreign API for about 1 million times. So for every single team_id I have to contact an url, receive the data and insert (or update) the data as a new row in a database (if it meets certain conditions).

I know I can do this easily via for but wouldn’t that be a perfect fit for GenServer? That would give me a first practise on genserver…

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
Nefcairon

Nefcairon OP

And what about my question regarding using GenServer? Too ridiculous to comment on? :dizzy_face:

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews