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
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
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
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
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
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
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
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Marked As Solved- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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.Also Liked
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
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
.
Last Post!
kokolegorille
It would be fun to do this with GenServer as an exercice but…