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.
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 10 of 11 Posts
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
You might use
Qqwy
Quite possibly!
Here are a couple of questions for you, that might help you:
Kernel.inspect(which is meant to only be used for logging/introspection), you probably want to useto_string, or string interpolation. Also, there is no reason to useKernel.in front of a function, because the Kernel module is always (unless explicitly overridden) imported into a module.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
Thanks, this is putting me forward
Nefcairon
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
This is not required. And if you really want to put a verb in there,
. Using a verb like
getis about as vague as you can be, since it means so many different thingsgetorfetchas 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:Here, it is clear that
Team.url(foo)will return an URL based onfoothat 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
.
Nefcairon
Allright. Thank you
Nefcairon
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
forbut wouldn’t that be a perfect fit for GenServer? That would give me a first practise on genserver…OvermindDL1
I’d probably just make:
Or more readably as (since I like to pull out arg calculations onto their own lines:
Nefcairon
And what about my question regarding using GenServer? Too ridiculous to comment on?