Every program in the Matrix is using if statement. Let’s explore how you can write better applications using Elm if expressions. This post is part of the functional language series, and it is based on a remarkable book Programming Elm Build Safe and Maintainable Front-End Applications by Jeremy Fairbank.
Elm if statements have rules that help you caught logical errors in compile time. This is an essential feature that other languages do not have. If somebody asks you why Elm is suitable, hit them with this fact.
We will write Elm function with one if expression that will return String where the input parameter is also number.
In the image above, we defined and exposed in new
Basketball module function
trials. Input is
height number, and both branches return
String. If expression must return for both branches, the same data type and must have else branch. Why? It is proven that missing else and mixing data types in return is the common source of programming bugs.
Let’s run our trials:
- The function could have one if expression.
- Then is mandatory
- Branches must return the same type
Originally published at https://blog.tentamen.eu on June 6, 2020.