lkuty

lkuty

How to get rid of "this check/guard will always yield the same result"?

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.

The cond expression looks like:

cond do
  @dev && <dev condition>   -> <dev expression>
  @prod && <prod condition> -> <prod expression>
  <condition>               -> <expression>
  true                      -> <true expression>
end

Marked As Solved

NobbZ

NobbZ

You have to push as much of this to compile time.

in this case, you shouldn’t even generate the @prod clause when in dev and vice versa.

At the same time, it is probably already a smell to have this distinction based on the build environment.

Also Liked

NobbZ

NobbZ

There is a certain kind of warnings that you can silence by using macros and set generated: true or so in the returned quote.

Kernel.SpecialForms — Elixir v1.16.3.

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.

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New

Other popular topics Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement