Debugging

Contents
IO.inspect with label
Require warnings
IEx.pry concept
Halve the code

IO.inspect with label

When a value looks wrong, print it with a label at the exact spot of suspicion. The label tells which print produced which line when several prints are active at once.

user = fetch_user(1) IO.inspect(user, label: "user")

Remove every inspect call once the bug is found. Prints left in committed code add noise for the next reader.

Require warnings

Compiler warnings point at real problems: unused variables, missing clauses, and stale calls. Treat them as errors so no warning survives unnoticed.

mix compile --warnings-as-errors

Run the command before committing, and keep the output clean at all times. A project with zero warnings makes each new warning loud and easy to catch.

IEx.pry concept

For a live look inside running code, pause execution with a breakpoint call. The session stops at that line and hands over an interactive prompt with local bindings in scope.

require IEx IEx.pry()

Breakpoints need an interactive session started from the project. They are a development tool only and must never reach committed code.

Halve the code

When nothing points at the cause, narrow the area by halves. Disable one half of the suspect code and check whether the bug stays or goes.

Repeat on the half that still fails. A few rounds move the fault from a whole module to a few lines, where one labeled inspect usually closes the case.

Next: Configs

Article author: Arthur Isaev

Related articles
Elixir
Elixir course: zero to hero in 50 lessons
What is Elixir and where it runs
Install Elixir
iex: the interactive shell
Your first script
Variables
Numbers
Strings
Atoms
Truthiness: only false and nil
Conditions
Recursion replaces loops
Lists
Tuples
Maps
Keyword lists
Functions
Defaults and clauses
Pattern matching
case
with chains
Modules in Elixir
import, alias, require, use in Elixir
Structs in Elixir
Protocols in Elixir
Behaviours in Elixir
The pipe operator in Elixir
Enum in Elixir
Stream in Elixir
Comprehensions in Elixir
Advanced strings in Elixir
Sigils
Binaries and charlists
Exceptions
ok and error tuples
Files and IO
Processes
GenServer
Supervisors
Tasks and Agents
OTP applications
Mix projects
Dependencies
Testing with ExUnit
Doctests
Debugging
Configs
Releases
Capstone: todo CLI
Capstone: HTTP server
Hero roadmap: the whole course on one page
Install Elixir on Windows 11
Install Elixir on Ubuntu
Install Elixir on Rocky Linux
Install Elixir on macOS
Install Elixir on FreeBSD
Elixir in Docker
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: