Intro to Elixir | Lessons 21: Project organization, alias and import


English (Auto-generated)

Hello Friends. So we have initiated our Star Wars project. We haven't done much with it so far. So we have this module that is default, that's built in auto generated, let's call it. And it has just this one function called Hello that returns an atom, that is world. So it doesn't do any useful things for now. It's just meant to be an initial kind of scaffolding type of a um you know that the situation, I would say here, we will delete it later. But for now, what I want to show you here is how we can use functions in other modules. Well, first here um a lot of your code will live in this lib folder so it's best to kind of learn how to organize it in the best way that you can. So for instance, here let's say I wanna use, I wanna bring some of the functions we've already written um about strings in our previous lessons. Let's say I want to make them available in this project because they'll come in handy as we learn how to do api integrations later. So what I'll do here, I'll start by creating a folder helpers. And let's say I'll make um a file called strings dot e X. Remember E X. Are all the files that will get compiled. E X S R script files that will ex execute on on demand but they will not be compiled with your project. So when you put a folder structure like this, it's common that you do name spacing for your modules. So what we do here, we have these helpers as a folder and then inside it we have strings. So we do helpers that strings do. And then now we have our modules like this. Now. How do you create a folder And how deeply unesco's folders? Really depends on how big your project is. And it also depends on the conventions that you're working with, especially if you're working with the company that probably have their own conventions. If you're working on your own. Honestly, it depends on how you organize your project. There isn't some fixed rules but generally um known is that it's not going to put a lot of your code in a single module. It's good to split it into different modules that are relatively um easy to kind of navigate through and make sense of and so on. So what I'll do here, I'll bring this function here. If you recall we've written this function called vitalized in a previous lesson where we take a string and then we return the string. An alternative string. Tight allies. So if we give it Hello World, we get a string. That is the same one with each word capitalized, which would be Hello World like this. I'm gonna remove this comment for now as we don't need it. Okay, so I've put in this tight allies in this helpers. Strings and we have this title is that takes in a string and then it tight allies, is it? How do I use use it in this Star Wars? There are multiple ways to do this. So I will show you two techniques today. And yeah, those are I would say probably the most common ones right here. The first one is to use import. So I will put your import um helpers, Art Friends. Okay, so in this case it says no documentation available because one second here. So let me just move this documentation up there. Um which is an interesting thing to pay attention to here. So it's telling me uh no documentation available notice here when we started when the Star Wars was generated. It came with this at module dog at the top right here. And this is what I recommend you do with all the modules. So module dog. And then we can see here these modules provide handy spring helpers. It's very important, especially bigger projects because your project starts to get big and then new people reading your code may not know what these modules are for while this module docked at the top here may give you a bit of a hint for what's happening over there. Alright, so I did hear import helper string. So import mix all the functions of the important module available as if they are written in this module. So now, basically what happens you think of it. Like you take this title? Eyes and you inject it as if you've done like this. Okay, so that's kind of you could think of it as the equivalent. Read them. So let's give this a test. So for example, I can let's make a new function for now deaf greeting name you and then let's say we return hello and then we Vitalize the name. We title eyes the name. So remember this title Eyes is coming from helper strings module right here. And the only reason I have it available is because I have this import helpers strength by the way you notice here I have a plug in that's suggesting something called specs. Not worry about it for now. I will show you later how we could write specs and how we can make best use of specs. All right. So how do we actually test this um that this is actually working as intended? Well, we could use I E X. So I'm inside the folder here where you know um the those files all are here. So what I can do right now is I can do I E X. But what I'm gonna do here, I'm gonna pass the dash X option and then mix let me actually put this in your notes so you'll remember it later. Alright, uh knows have gone into another file. So let me just bring it here. Thank you. Mhm. Mhm. All right. So with this notes right here. So I'll say you can uh load up I E X. With all of your modules and all the libraries imported, which we'll see later using this command, I shouldn't say important here to avoid just name, collision added using this command which is my ex dash capital X X. So now I have tv star Wars and then I used my X dash capital S mix so this loads not only my files but all the dependencies. So if you recall I mentioned in the earlier lessons, we can add dependencies by adding them here and running mixed um some mixed commands. So what's gonna happen here is if you, if you have any here, you basically will have them loaded when you run i X. Which is handy because that's usually what you want to test with. Let's test this out. Star wars dot greeting, john smith And the notice here it says hello john Capital J smith. Capital s so it did title is the name that I passed to it. Okay, so that's a really good way for us to kind of have functions split into different modules and using them wherever we want. Awesome. So the second approach is to use an alias, in my opinion, this is generally the preferred approach. Again, this really depends on how you write your code and it depends on the standards of the project that you're working with but generally speaking I would prefer that you use alias as much as possible and then use import for very few cases um, alias. So I do the same thing, helpers that strings. The difference in alias. Well actually before I do this, if I don't want to use alias or import one thing you could do is you could use the fully qualified name of the modules. Okay. So you could write helpers dot strings that title. Eyes. Okay. So what I can do here, by the way, this doesn't automatically get reloaded in i X. So for this to work, you have two ways to either exit and enter again or you could recompile the module um respectively. Alright. So to redefine the module to recompile it, you just type R and then you put the name of it and now we basically have it again. So let me run the functions again. It still works um star wars dot greeting john smith still return the same thing. But you know, um in this case it's probably not too bad. It's a very small um so it's a very small path but it is not uncommon to have like strings that helpers that abc that you know, so you have basically multiple nested folders in your application. So that becomes cumbersome. And it's also good to just be able to just say strings instead of saying helpers of strings. And this is done basically by using help birds and strings using alias. Wicked. Access is a module without its fully qualified name. So instead of accessing the module Well, a couple of strings with alias. We could simply use strings. Mm hmm. Alright. So let me recompile this and then call it again and it still works. So as we saw here, we could use import and then we have the function available directly as if it's certain here or we have alias which allows us to have a simpler name instead of the fully qualified name of our module. Um again, favorite alias in most situations because you know, it's less magical, I would say. So if you see here title ized, you wouldn't know right away if this function is defined here or it's if it's important you'd have to search and dig and if it's important you have to go look at all the imported modules to see where it came from. Well, if it says strings here, you could simply look up there and see what's been alias here and see which one is the strings. And then basically go to that module and see the definition of the title lies. One thing as as you know, I can think of it as our hook for next lesson. Notice how every time I made a change I had to uh to redefine this module and I had to re run the method which as you can tell, can become cumbersome. If you have a big project, that's why in the next lesson we will learn how to automate this by actually writing tests to do what we expected to do. Thank you for tuning in and see you in the next lesson.
42 Views 0 Likes 0 Comments

We learn how to use functions in different modules with `import` and `alias`.

Leave a comment (supports markdown format)