Experiment in the REPL

February 19, 2019

A technique I’ve picked up for learning new tools is experimenting with them in the REPL.

A read–eval–print loop (REPL), is a simple, interactive computer programming environment that takes single user inputs, evaluates them, and returns the result to the user. *

Elixir (iex), Ruby (irb), and Node.js (node) all have interactive shells that allow you to evaluate expressions easily and quickly.

This works great for small things like remembering how division works or grokking a tricky enumerable method.

iex(1)> 1 / 2
0.5
iex(2)> div 1, 2
0
iex(3)> Enum.reject([1, 2, 3], fn n -> rem(n, 2) == 0 end)
[1, 3]

It’s also a convenient way to try out new libraries.

iex(4)> Slack.Web.Chat.post_message("C68FV6MDH", nil, %{attachments: [%{color: "good", text: "Hello world!"}]})
** (ArgumentError) argument error
    :erlang.list_to_binary([%{color: "good", text: "Hello world!"}])
    (hackney) /Users/mitchell/Development/my_app/deps/hackney/src/hackney_bstr.erl:36: :hackney_bstr.to_binary/1
    (hackney) /Users/mitchell/Development/my_app/deps/hackney/src/hackney_url.erl:300: :hackney_url.urlencode/2
    (hackney) /Users/mitchell/Development/my_app/deps/hackney/src/hackney_url.erl:360: :hackney_url.qs/3
    (hackney) /Users/mitchell/Development/my_app/deps/hackney/src/hackney_request.erl:310: :hackney_request.encode_form/1
    (hackney) /Users/mitchell/Development/my_app/deps/hackney/src/hackney_request.erl:318: :hackney_request.handle_body/4
    (hackney) /Users/mitchell/Development/my_app/deps/hackney/src/hackney_request.erl:81: :hackney_request.perform/2
    (hackney) /Users/mitchell/Development/my_app/deps/hackney/src/hackney.erl:373: :hackney.send_request/2
    (httpoison) lib/httpoison/base.ex:787: HTTPoison.Base.request/6
    (httpoison) lib/httpoison.ex:128: HTTPoison.request!/5
    (slack) lib/slack/web/web.ex:51: Slack.Web.Chat.post_message/3

# Turns out we need to JSON encode the attachments list
iex(5)>Slack.Web.Chat.post_message("C68FV6MDH", nil, %{attachments: Jason.encode!([%{color: "good", text: "Hello world!"}])})
%{
  "channel" => "C68FV6MDH",
  "ok" => true,
} 

I find experimenting in the REPL to be useful when I need a short feedback loop that isn’t tightly coupled to the full stack of my application.

Avoid Analysis Paralysis

Analysis paralysis is when the fear of potential error outweighs the realistic expectation or potential value of success, and this imbalance results in suppressed decision-making in an unconscious effort to preserve existing options. *

Idle hands build nothing. I fell into this trap recently, wasting hours before I remembered the power of the REPL.

I knew I needed to use either a Supervisor or a Dynamic Supervisor, but I wasn’t sure which one was right for the job.

Unclear on how to proceed, I spent a few hours reading documentation, searching for blog posts, and asking people for advice. Did I learn enough to make a decision? No.

Then I spent 20 minutes experimenting with them in iex and I knew which one to use.


Enjoy this article?

Subscribe to have them delivered straight to your inbox!