Intro to Elixir | Lesson 26: Using `with` to avoid nested conditionals

Elixir
Transcript

English (Auto-generated)

Hello friends today. I'm gonna show you using with to avoid having nested conditions. Um It doesn't make sense to show it to you before because we weren't dealing with very complicated conditional, but let's say here, I want to implement a function that says who is dollar and then you give it basically name one and name. Mhm. Okay. So what you can do what we want to accomplish in this case here. So let's actually use maybe doc as as a reminder from our documentation. This function takes two names and queries the star Wars ap I copy and tells you what character Okay, which character is color than the other. The idea is that you give it to character names and then it will tell you well, you know, character one is dollar than character two or so on. The way you go about this. Well, first of all, because we're getting names, we need to fetch it by names. Okay? And then what we're doing here, um let's actually do this a little bit differently by fetch, person by name. Let's have a dis return. Okay? And then the and then the second triple. The first element is okay. And the second one is the actual persons map and in the case of the air, similarly we do error and then we do something like that. We put the error message as the second argument by the way, you know, I I know you see me doing a lot of io dot puts or io dot inspect. Usually those are used for debugging or used for um you know, you displaying information or teaching, did you don't see them as much in in production applications. So this is why again, if you see me haven't left any of these there, you know, just keep in mind that those are meant to be primarily for for these type of things. By the way, one thing I want to remind you about is using mixed format. You can, you should run this every now and then in case you get the wrong formatting, so not just hear how before this I was formatting it this way, which isn't really ideal. And then when you run mixed format it switches it to be more nicely formatted. So it's good to get into the habit of running mixed format and you could actually use, if you're familiar with gifting, get help, you could use a hook that every time you commit to get it will automatically run mixed format for you, just to make sure that things are formatted on that awesome. So what we need to do here is I need first to kind of like, I would say like person one and then we need to fetch a person by their name and then I need to say, well, you know, similarly person too, we need to fix that by name and then there's a little problem here that maybe person one is misspelled, maybe person to is misspelled right? So it's really the ideal situation is to do something like this where I do maybe case Fetch the person one and then here if it's okay and then person info or I should call it like maybe person one info, then do another case and now fetch the person to info and then if it is okay and then person to info and then we should handle the error cases in both scenarios. As you can tell here the code is getting really, really I'd say deeply nested which is really mixed, hard to read and hard to modify later on. Um one solution is to actually use pattern matching, so you could use pattern matching and just pattern match on different functions. Um the other approach I want to highlight here, which I think is nicer in this particular case is using with. So the way with works is as follows, so you could say something like this, I think as as direct this example it will become clear to you. So I say person one info and then I use this kind of, you can think of it as a backward arrow which person by name. Good morning. And then you put a comma, you could have as many of those as possible. The idea is you put condition one like you could put condition one condition to and so on and what you usually do, you put the happy path, I'm a special person, person, person, you put the happy path at the very top, the one that you expect to happen most of the times. Okay and then here name too and then you open the do block what's difference thing here is whatever is in here this but only execute when everything I mean uh with clauses matches correctly so when I get to do here That means this has matched correctly and this is matched correctly. You could have actually 20 different other conditions and then they could match correctly now. Otherwise you could just have an else and the else could be a catch all where like if an error happens in any of them just return a string saying something like uh names are misspelled or some other error happened. I mean we could go more granular and then uh match for different areas. So we could say something like you know error or we could have some other ones right here but at least we could have a catch all. You should have a catch all the words to his names are misspelled or some other error happened connecting to the api Okay and then here now that we have person one in for one person to info then we could say something like this. Um We could actually have it even here um we could do something like um um if it's true I shouldn't say this this is actually ideal putting uh put in a separate function so I could do something like this. I could do something like person one is dollar question mark because we expected to return to our false and then I could say person one info. Person to info. Now we know here that when we make the api request it will come let me actually do it. I X dash X dash s mix. I'm getting some warnings here because I have unused variables right here so we can ignore them for now but I can do Star Wars dot fetch info by name and let's say I put doors later. Okay, well I got here okay. NeLL because I misspelled got dark Vader didn't exactly match. So what I should do here probably before I move forward, let's actually do something like string. A damn case should match string but down case today. So basically we do kind of a case insensitive matching in this particular case. So let's try to do our Star Wars. Oh I'm made a typo somewhere here. Unicode characters. Oh sorry, down case should be down cased. Let's recompile this and let me now find by Darth Vader. So I do find Darth Vader right here, so which is okay and then the person one info so notice here how the height comes in as a string and it comes in with a key that is height as a string. So what I need to do in this info here. Well I should say height and then here I could say person one, right? And then the same thing here. For the second one I do pattern matching as well. But then I call it person too. Sorry for the typos again. Remember that those come as strings. Um how do we convert the string to in teacher while we could do in teacher dot uh sorry. We could do spring but to the enclosure and convert it that way. So we could say something like this string dot to find treasure Britain one height. It's greater than um string dot to be sure person to hide. So what I can do right now here, I can do something like this Case which in $1 to an info and passive person to info and then we do a block if we get through, we say that um actually this one maybe an easier one to do considering it's very simple matching. We could just use if and then you can stay here and one is taller than you too. Otherwise we say Name to his dollars and name one. All right. So it's not just here how we have really like we've really simplified this otherwise, we would have had to triple or even more nesting zones to handle that same case. So let me actually go and recompile Star Wars. Now notice all the warnings are gone because we've actually used these variables and now I can say well who is taller luke? Skywalker or Darth Vader. Let's find out who is taller. Sorry, I forgot to call Star Wars with dollar luke. Skywalker, Darth Vader, ss Darth Vader is um taller than sky luke. Skywalker. Who's taller? More three C. Threepio luke walker is dollar than C. Threepio. So as you can tell here, um we've used this with to make multiple requests and again here, it's not uncommon to see 345 here, we're all checking and then you do matching at the very end right here. That's it for today's lesson. I hope you've enjoyed it and we'll see you next time.
56 Views 0 Likes 0 Comments

We learn how to avoid having nested conditionals by using `with`.

Comment
Leave a comment (supports markdown format)