I have predicates that rely on the @dev and @prod module attributes to know in which environment we’re in.
In config/dev.exs, I have the following configuration among other things:
config :myapp,
dev: true,
prod: false
My @dev module attribute is defined like that:
@dev Application.compile_env!(:myapp, :dev)
When I write a predicate like:
cond do
@dev && ... -> ...
Elixir warns me that this check/guard will always yield the same result which is true but I do not want to be warned because the produced code is ok even if the translation gives false && ... (because we’re in production). Is there a way to disable the warning or to refactor the usage? Is it considered bad style?
A priori without pattern matching on function arguments unless there is no other way.
Though instead of working around the warnings and silencing them, you should take care to not make them happen.
Why spend cycles/reductions on a clause if you know in advance that it can never become true? Just omit that clause.
And still, changing behavior based on the build environment should be done with care. It would be much more idiomatic to instead introduce an option, that you configure differently between build and prod, that describes the difference in behavior much better.
This was really useful for another bunch of warnings coming from generated code with a few macros. Solved it! And reached 0 warnings after a few days of refactoring. Afraid to put credo in the build pipeline now
If you really want to check environment I would just check the name as opposed to a boolean. There are a few ways to get it… like setting it in each config: config :env, <:dev|:test|:prod>. You can also do @env Mix.env() which is safe but sort of sets bad precedent AFAIC.