unaware8150
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in different scenarios:
def start_race(user_1, user_2, "horse"), do: ...
def measurement_unit(%{vehicle: "horse"}), do: "m/s"
def is_animal?(vehicle) when vehicle == "horse", do: ....
def track(vehicle) do
case vehicle do
"horse" -> %Track{name: "horse_track"}
_ -> %Track{name: "highway"}
end
end
def default_vehicle(_), do: "horse"
if all those function examples are within the same module, then we could extract them to a module attribute as follows:
defmodule Game do
@horse "horse"
def start_race(user_1, user_2, @horse), do: ...
def measurement_unit(%{vehicle: @horse}), do: "m/s"
def is_animal?(vehicle) when vehicle == @horse, do: ....
def track(vehicle) do
case vehicle do
@horse -> %Track{name: "horse_track"}
_ -> %Track{name: "highway"}
end
end
def default_vehicle(_), do: "horse"
end
but things get tricky when we have to use `horse` across different modules, because we ended up with lots of `@horse “horse”` definitions and later on if needed to update, it might get tricky.
In order to kind of solve this, the team started to write macros and use them as follows:
defmodule Vehicles do
defmacro horse, do: "horse"
end
defmodule Game do
require Vehicles
def start_race(user_1, user_2, Vehicles.horse()), do: ...
def measurement_unit(%{vehicle: Vehicles.horse()}), do: "m/s"
def is_animal?(vehicle) when vehicle == Vehicles.horse(), do: ....
end
defmodule Track do
defstruct [:name]
require Vehicles
def default_vehicle(_), do: Vehicles.horse()
end
which does not sit well with me
but I’m not sure if it’s just me or that pattern is a code smell on its own and there is a better way to handle it. I hope you got the gist of the problem and my pet peeve.
So, anyway, is there a way to handle those “fixed” or constant strings across the codebase in a clean way without losing pattern matching or guard usage ?
Thanks in advance!
Trending in Questions
Other Trending Topics
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
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #elixirconf-us
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #security
- #elixirconf-eu
- #processes
- #api
- #forms
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
rhcarvalho
At a glance it looks like those “strings” really wanted to be atoms?
unaware8150
sometimes, yeah, but not always. Due to the toy example, we could replace them all with atoms but since our code deals with JSON serialization/parsing, sometimes we need to keep the fixed term as a binary string.
Zurga
You could create a module that contains all the globals and the inject them as module attributes with
use. So:And then you can have them as compile time env vars as well. Just replace “horse” with Application.compile_env calls
frerich
Perhaps you can consider not referencing the same string everywhere. Instead of having N functions each having a clause for
horseit might be viable to group all the horse-functions in a single module and then select the module just once. E.g.I.e. you pass a module around which has all the horse-specific functions.
derek-zhou
It is not just you; I find using macro in function head hard to read.
gregvaughn
Perhaps this is an XY problem. Instead of focusing on constants, maybe what you want is a Vehicle protocol, with a Horse struct as one implementation.
LostKobrakai
Can you elaborate on why you need that?
nikfp
Why not use functions? You can execute them into module attributes during compilation and use that value as the match or in guards.
You can define a module that holds the constants as return values of functions:
Then assign the constants to module attributes in the consumer modules:
You need to change the string to something else? Change it in the constants module and recompile and you are done.
This looks close to what you are already doing, but doesn’t need a macro to do it. It’s more explicit and reads better to me, and if you have a string that needs to be consistent on what it’s matching but the meaning behind it might need to change for the module, you can name the module attribute anything you want.
windexoriginal
To elaborate on Greg’s suggestion:
Now when you add a new vehicle you create a module that implements the Vehicle protocol and add it’s module name to the vehicles attribute in the Vehicle module. If you add more required functions the compiler will warn you if you haven’t implemented them in your modules.
unaware8150
interesting approach! thanks for the suggestion. I might give it a try.