Elixir basics · Lesson 15 (open contents)
01 · Let the code speak02 · Meet six kinds of values03 · Put values together04 · Build a new list05 · Make the shapes match06 · Choose with case07 · Parse a number safely08 · Unpack &1 and the pipe09 · Put code in a project10 · Ask precise true-or-false questions11 · Read and update nested data12 · Count visible text and bytes13 · Let function clauses choose14 · Solve a list twice15 · Choose the smallest clear control form16 · Give domain data a shape17 · Put one tested module in Mix18 · Make success and failure predictable19 · Read and write one real file20 · Take only the data you need21 · Give different data one shared action22 · Write a callback contract23 · Write down the data contract24 · Test behavior and boundaries25 · Turn a project into a command26 · Write the project promise27 · Parse one real line28 · Keep the public API small29 · Make the promise executable30 · Package the edge and leave clues31 · Prove the project is done
ELIXIR · FOUNDATION · LESSON 1525 minutes

Choose the smallest clear control form

Use `if`, `cond`, and `case` for different shapes of decisions.

01 · Start with the whole example

Run this first

Run it once, change one input, and compare the new result.

Elixir
# Use each form for the question it fits
score = 76
label = cond do score >= 90 -> :great; score >= 60 -> :pass; true -> :retry end
parsed = case Integer.parse("42") do {n, ""} -> {:ok, n}; _ -> {:error, :bad_text} end
allowed = if score >= 60, do: :yes, else: :no
{label, parsed, allowed}
Check the result first
  • The result is {:pass, {:ok, 42}, :yes}.
02 · Take the code apart

Read from the first line down

  1. Read if ... do ... else: Choose between two Boolean paths.
  2. Read cond do: Try conditions in order.
  3. Read case value do: Match one value against clauses.
03 · Meet the new symbols

Symbols are not secret signs

if ... do ... else

Choose between two Boolean paths.

cond do

Try conditions in order.

case value do

Match one value against clauses.

04 · Ideas inside the code

Match each name to its meaning

01

if

Use if for one Boolean question with at most two outcomes.

02

cond

Use cond for several Boolean conditions checked from top to bottom.

03

case

Use case when patterns should inspect one value.

05 · Make the idea clear

Why these forms are useful

Run the example first. Predict one result, then change one input and run it again.

Use if for one Boolean question with at most two outcomes.

Use cond for several Boolean conditions checked from top to bottom.

06 · Change it yourself

Close the answer and try

Use cond to label 5 as :positive, 0 as :zero, and smaller values as :negative.

Practice starting point
number = 5
cond do
  number > 0 -> ____
  number == 0 -> ____
  true -> ____
end

Target result: With 5, return :positive.

Stuck? Read one hint

Fill the branches with three atoms.

After you run it, see one answer
One answer
number = 5
cond do
  number > 0 -> :positive
  number == 0 -> :zero
  true -> :negative
end
Think about it: When is `case` clearer than `cond`?

Use case when several patterns inspect the shape of one value.

Take with you

Remember these three lines

  1. 1

    Use if for one Boolean split.

  2. 2

    Use cond for ordered conditions.

  3. 3

    Use case for pattern-driven choices.

Lesson completeRun the code once, then mark this lesson as done.