Comparisons Conditionals ∞
(and coding style / conventions)
if ∞
a = 1; b = 1; if ( a == b ) printf "a is equal to b"; fi; # => # a is equal to b
Adding additional spaces is a Mythryl style convention which greatly improves readability. The following is a simple example, but keep in mind that the more complex the code, the more important it becomes to take the time to make it readable.
It doesn't matter that you understand it and don't need to "pretty it up". Will you understand it at a glance in the far future? Will someone else?
The time you spend improving readability now is time you save trying to figure it all out again later. Even if you spend just one minute now, that could save you several minutes of time (and attention) later.
a = 1; b = 2; if ( a == b ) printf "a is equal to b"; fi;
fi;
is on its own line- three spaces after
if
-
two spaces after
)
Lining up multiple statements:
a = 1; b = 1; if ( a == b ) printf "a is equal to b"; printf "\nthis is another line"; fi; # => # a is equal to b # this is another line
Other comparisons ∞
The example in if
, above, shows ==
(is equal to). These are the common comparisons:
==
- is equal to!=
- is not equal to<
- is less than<=
- is less than or equal to>
- is greater than-
>=
- is greater than or equal to
These are sometimes called "connectives" in Mythryl.
if, else ∞
a = 1; b = 2; if ( a == b ) printf "a is equal to b"; else printf "a is not equal to b"; fi; # => # a is not equal to b
If you notice how the text has been lined up, you learn why there are three spaces after an if
.
if, elif, else ∞
elif
is short for "else, if".
a = 1; b = 2; c = 3; if ( a == b ) printf "a is equal to b"; elif ( a == c ) printf "a is equal to c"; else printf "a is not equal to either b or c"; fi; # => # a is not equal to either b or c
The three spaces after if
keep an empty column next to the if
, elif
, else
and fi
. This is easy on the eyes. Notice how the printf
statements are lined up, even after else
.
The line which has if
on it has two spaces between the comparison and the printf
. Those two spaces form a column of whitespace which helps separate the logic on the left from the commands on the right.
Comparisons and Conditionals in Functions ∞
Comparisons and conditionals work the way you'd expect in a function. Pass your data into the function, and that function can have conditionals work with the data and do anything or return anything you want.
fun is_greater( number ) = if ( number > 10 ) printf "%d is greater than 10." number; elif ( number == 10 ) printf "%d is 10." number; else printf "%d is less than 10." number; fi; is_greater( 100 ); is_greater( 10 ); is_greater( 1 ); # => # 100 is greater than 10. # 10 is 10. # 1 is less than 10.
- Notice how there is an alignment of the number 10.
-
Notice how the far-right number; lines up.
That alignment helps make the code resilient against typos and easier to scan.
This can also be programmed in this way:
fun is_greater( number ) = if ( number > 10 ) "is greater than 10."; elif ( number == 10 ) "is 10."; else "is less than 10."; fi; printf "100 %s" ( is_greater( 100 ) ); printf "10 %s" ( is_greater( 10 ) ); printf "1 %s" ( is_greater( 1 ) ); # => # 100 is greater than 10. # 10 is 10. # 1 is less than 10.
Last updated 2017-11-19 at 02:51:32