DEV Community

Daniel Kukula
Daniel Kukula

Posted on

1 1 1

Enhancements to dbg in elixir 1.18

Elixir 1.18 added some interesting, features but one that went under the radar was extended support for dbg. In 1.17 when you had this code

a = 1
b = 2

if a + b == 3 do
  :equal
else
  :non_equal
end
|> dbg
Enter fullscreen mode Exit fullscreen mode

It evaluated to:

if a + b == 3 do
  :equal
else
  :non_equal
end #=> :equal
Enter fullscreen mode Exit fullscreen mode

In 1.18 this was aligned with what case and cond does:

Case argument:
1 + 2 #=> 3

Case expression (clause #1 matched):
case 1 + 2 do
  3 -> :equal
  _ -> :non_equal
end #=> :equal
Enter fullscreen mode Exit fullscreen mode

if result in 1.18:

If condition:
a + b == 3 #=> true

If expression:
if a + b == 3 do
  :equal
else
  :non_equal
end #=> :equal
Enter fullscreen mode Exit fullscreen mode

if this is not enough you can wrap your code in brackets and pass it to dbg

(
  input = %{a: 1, b: 2}
  c = Map.get(input, :c, 20)

  if input[:a] + c == 3 do
    :equal
  else
    :non_equal
  end
)
|> dbg
Enter fullscreen mode Exit fullscreen mode

results in displaying results for every expression

Code block:
(
  input = %{a: 1, b: 2} #=> %{a: 1, b: 2}
  c = Map.get(input, :c, 20) #=> 20
  if input[:a] + c == 3 do
  :equal
else
  :non_equal
end #=> :non_equal
)
Enter fullscreen mode Exit fullscreen mode

Last missing piece is the with macro which is sometimes a nightmare to debug, in elixir 1.18 we have support for that:

with input = %{a: 1, b: 2},
     {:ok, c} <- Map.fetch(input, :c),
     3 <- input[:a] + c do
  :equal
else
  _ -> :non_equal
end
|> dbg
Enter fullscreen mode Exit fullscreen mode

The result here is:

With clauses:
%{a: 1, b: 2} #=> %{a: 1, b: 2}
Map.fetch(input, :c) #=> :error

With expression:
with input = %{a: 1, b: 2},
     {:ok, c} <- Map.fetch(input, :c),
     3 <- input[:a] + c do
  :equal
else
  _ -> :non_equal
end #=> :non_equal
Enter fullscreen mode Exit fullscreen mode

This should make your debugging journey easier.
If you are stuck in an older elixir versions you can install a package that backports this functionality dbg_mate
Thanks for your attention.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (1)

Collapse
 
abreujp profile image
João Paulo Abreu

Excellent.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay