Unit introduction
Welcome to unit 2 of the Functions course!
In this unit, you will explore conditional functions. By the end of this unit you should have an understanding of:
- what is a conditional function
- how to use the
if(),ifempty(),switch()andcontains()functions - how functions can help make your scenarios be more maintainable and save credits
Here, you’ll start to dip your toes in more advanced concepts, marking the official end of the Make Intermediate learning journey.
What you’ll see in this unit
By now, you’ve come across a few functions and you might remember how they enabled you to change your data in ways it wasn’t possible before, like the upper() and formatDate() functions.
In this unit, you’ll start with something a bit different. Instead of transforming data, this unit will focus on conditional functions: if(), ifempty(), switch(), and also show you the contains() function.
Conditional functions refer to functions that include conditional statements. These conditional statements are used to make decisions based on certain conditions. The most common type of conditional statement is the if statement (if this, then that). Think of this like a filter. For example: if you are 18 or older, you can vote.
One of the main advantages of these functions is streamlining your routes and avoiding repetition. This can make your scenarios much cleaner and easier to maintain and also save credits!
if() …
Feel free to follow along by copying the functions you see on the images, or download a blueprint of this scenario. Blueprint / zip. Go through every step before you continue.
Do you remember what Boolean values are? These are values that are always either true or false, and they are the basis for any kind of conditional check. This is what you use in filters, even if you don’t realize it at times. Whenever you have any comparison, this will always end up being a Boolean value.
Take the examples in the image. Note that the variable Dog has the value Dog. What do you think the value of Animal_1 will be? What about Animal_2? Are these comparisons true or false?
If you said true and false, respectively, you got it right! In the Animal_1 case, the Dog is a variable mapped from the first module. The variable contains the value Dog. So the first comparison is actually Dog = Dog, which makes the first statement true. In the Animal_2 statement, Dog does not equal Cat, which means that the second comparison is false: Dog ≠ Cat.
Okay, that makes sense, but you might ask what’s the use of this? Well, with the if() function, you can use these comparisons to decide whether to output one value or another, instead of having true or false.
The basic structure of the if() function is:
if(this is true; do this; otherwise do this)
You always start with a condition, followed by 2 values. For example, if you want to output “Animal” only if your condition is true, you could have this: if(condition; Animal;Not an animal).
With that in mind, what do you think example3 and example4 outputs will be? Try to think about the solution on your own.
If you got example3 = Animal and example4 = Not_an_animal, then good job! For example3, Dog equals Dog, which is true. In this case, the if() function outputs the first option, which is Animal. For example4, Dog does not equal Tree so this condition is false. Therefore, the if() function outputs Not_an_animal, the otherwise/else value.
To recap the basic structure of the if() function is:
if(this is true; do this; otherwise do this)
You can also see the definition of the function, together with examples, by hovering over the specific function.
contains()
Let’s look at the contains() function next. You can use the same scenario blueprint as before.
The contains() function can be used with strings or arrays, and will always output a Boolean value (true or false).
It receives 2 values and is true only if the second value is contained within the first. Consider these examples:
contains("Dog", "Do")contains("Dog", "d,o,g")if(contains("Dog", "O"); "Match"; "No_Match")
Here’s how these examples would output:
- Contains1 is true: ‘Dog’ does contain ‘Do’.
- Contains2 is false because even if the string ‘Dog’ contains ‘d’, ‘o’, and ‘g’, it doesn’t contain them separate but as one word.
- The last one, If and Contains, would output
No_Match. The contains() function outputs false because ‘Dog’ does not contain ‘O’, so the if() function outputs the second option (No_Match).
ifempty()
Before we move on to some slightly more complex examples, here’s a quick look at ifempty().
This function is a simplification of if() for a specific case.
ifempty() will output a “replacement” in case the value is empty. If the value is not empty, it leaves the value as it is. This can be quite useful when dealing with required fields in a module when you can’t be sure if the value will indeed exist.
An example: you’re migrating a list of tasks from one platform to another, but some of the people are no longer in your team, so they don’t have a matching ID in the new platform. You could use ifempty() to assign any of the tasks without a corresponding assignee to yourself, and manually handle or reassign them later: ifempty(assignee; me).
The example on the left shows a variable named Task_migration with a lifetime of One cycle and a variable value defined as ifempty( assignee ; me ).
Nested functions
Nested functions are functions defined inside other functions. For example, in the image, you can see the if() function with two nested functions inside it – contains() and another if().
Here is an example of a nested function:
if( contains( 30.Dog ; Tree ) ; if( 30.Dog = Tree ; Animal ; Not_an_animal ) ; Not_a_dog )
Here are some tips on how to analyze nested functions to help you in the future.
Outer if() Statement
The result of the outer if() statement is determined as follows:
- If
contains(Dog; Tree)is true, it returns the result of the innerif()statement. - If
contains(Dog; Tree)is false, it returns'Not_a_dog'.
contains() Function
This checks if the value in the field variable Dog contains the string 'Tree'.
If the condition is true, the inner if() statement is executed; otherwise, it goes to the else part of the function.
The Dog variable has the value Dog and therefore does not contain the value of 'Tree'.
On its own, this contains() function will return a false output.
Inner if() Function
The inner if() function checks if the value in the field variable Dog is equal to the string 'Tree'.
If this condition is true, it returns 'Animal'; otherwise, it returns 'Not_an_animal'.
This will fail because Dog is not equal to 'Tree'.
Since the condition is false, the final output of the function is 'Not_an_animal'.
Tips for Analyzing Complex Functions
Two good tips that can help you when analyzing a complex function:
- Resize the module form to make it easier to read it, having each part of the function in a different line.
- Hover your mouse over the function names, semicolons, or parentheses to highlight the other separators of the function.
Switch use case
Let’s switch things up for a second with a use case.
Mands is working on project for their college’s library, where they are supposed to update the old data to a new system.
All the old books were identified by an ID, and Mands has a list of ID → title.
For this specific part, they only need to convert the IDs to the titles. 7123 for instance becomes “The Brothers Karamazov” and so on.
Let’s help them with that!
Element - Selectable image 3 – modal
There are multiple ways to do certain things in Make, but some are better than others. Here you can see 3 alternatives to create a “switch”.
You can find a blueprint with all these use cases – blueprint / zip.
Nested functions
You can use nested if() functions to create an if, else if, …, else if, else function that goes through all the IDs and links them to the corresponding title.
if( 6.bookId = 7123 ; The Brothers Karamazov ;
if( 6.bookId = 5623 ; The Count of Make Cristo ;
if( 6.bookId = 7865 ; Les Makérables ;
if( 6.bookId = 4562 ; The Makeamorphosis ;
unknown code ) ) ) )
This works perfectly, but it’s not so clean and can be a bit confusing. One wrong parenthesis could mess it up and make it hard to find the issue too. On the plus side, this doesn’t consume extra credits.
Switch module
Another option is the Switch module. It makes the data much more organized than the nested if() functions.
It’s a good option, but if you have an Iterator and an Aggregator configuration and you need the Switch module in the middle, this would consume a lot of credits.
switch() function
Lastly, there is the best of both worlds: the switch() function!
It’s configured similarly to the Switch module, but doesn’t cost extra credits! The format is: switch(value; v1; o1; v2; o2; ..., else) where v and o are pairs of values and their corresponding output.
Other potential use cases include any kind of connection between systems where IDs are different, be them product IDs, user IDs or anything else. The switch() function allows you to create a “translation table” to convert these values and “link” different IDs together.
switch( 6.bookId ;
7123 ; The Brothers Karamazov ;
5623 ; The Count of Make Cristo ;
7865 ; Les Makérables ;
4562 ; The Makeamorphosis ;
unknown code )
Switch use case wrap up
Now, if Mands has only 1 ID to convert, any approach is fine. However, if they’re converting a bunch of values, or if the scenario runs frequently, those extra credits from the Switch module would quickly add up.
The switch() function is the cleanest and most optimal solution in this case! Check this comparison between the module and the function when they have 4 IDs to convert in an array. Do you notice the difference in the number of credits?
You can test this yourself too! Download the blueprint / zip.
Real life example
You can combine functions with as much or as little complexity as you like. Cramming everything together can certainly save you some credits, especially when dealing with iterators and aggregators, or on scenarios that you run all the time.
However, it makes them harder and harder to read, and it can be a nightmare if you ever need to make any changes.
Here in the image you can see a real life example using some of the functions you’ve seen today. See how long these can get?
The last 2, while hard to understand, save hundreds to thousands of credits every time the scenario runs!
Optimization tips
While the examples you’ve just seen might be intimidating, you can definitely achieve similar results as well! It takes some time and patience, but here are some tips on how to optimize your scenarios with function nesting:
- Start small and don’t optimize too early: spread your functions over multiple Set variable modules at first and make sure the scenario works! Once you’re there, you can start copying and pasting the functions to their proper places within each other, replacing the variables.
- Test often: don’t try writing the whole thing at once and expect it to work on the first try. Test each step, each change, to see how your function reacts and catch potential errors before they become more complex.
- Be playful: get on it knowing you’ll spend some time and make mistakes along the way. Try things, explore, think outside the box.
Unit completed
We hope this was a smooth introduction to conditional functions and that you have started to realize how useful and powerful they can be!
With this intro, by now you should be familiar with:
- How to use
if(),ifempty(),switch()andcontains()functions, alone or combined - How functions can help make your scenarios be more maintainable and save credits
- A few tips on how to analyze and create more complex nested functions - the rest is experience, so go and play around with them!
This concludes this unit. You can take a break now or continue to learn more in unit 3, where you will look at string and array conversion functions.