What are libraries and frameworks?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the turns framework and library are two terms that are used a lot in the tech industry many people use them interchangeably and don't really understand what they mean so if you're on that boat hopefully this video will help you out so let's go ahead and get started so today we're gonna cover what is a library what exactly is a framework and then we're gonna compare both of them and see what the actual difference is so what exactly is a library well Anna Geist a library is a set of code that was previously written that can be called upon when building your own code so it's essentially a collection of code packed together that can be used over and over again and it's a very good idea to put frequently used functions together and build yourself a library one of those reasons is handling reusability so reusability is one of the most important factors in software development so whenever some method is used you can simply make a method invocation using the library just imported so we can look at an example of how we import libraries and JavaScript right over here we're importing three different libraries one called promise body parser and lodash and from there we can simply make calls to those libraries whenever actually needed so let's go ahead and take a look at an example where we actually use a library now let's say you're building an application where you need to register and sign up a bunch of users in order to do that the users must actually submit a valid email address when signing up now there are many ways to do this but most of the ways will actually involve using regular expressions so I actually wrote a function that validates email earlier so we're gonna write a function called validate email that will take a parameter called email and from there we're gonna write our regular expression and you can see it's quite lengthy and tedious and it's hard to understand if you're not familiar with regular expressions they basically match a string with specific characters and this is basically a pattern that actually looks for the specific email that matches the pattern so near the end of the function we actually going to return a boolean that will check whether the email has passed a read regular expression test and it will tell us whether the email was false which was in which means invalid or true which means that it was valid so let's actually go ahead and test this out before we start let's go ahead and validate email that has the wrong character so I'll passing a string that simply says name calm save that and then we're gonna run the validator and you can see that's false I put a little quote there accidentally run that again and you can see that's false so if I type in an ad gmail.com and save that that's returned true so the regular expression is essentially looking that there's characters and it's split by an @ sign and it has another domain name here followed by a period and then another 3 string character or a specific number of characters here now all those this works totally fine and it's acceptable code there's nothing wrong with it there's a faster way to do this by using a library called email validator now email validator essentially has this function kind of similar to this but it goes in a lot more depth as to what are valid emails or not here it's probably not testing all the cases for specific emails but the specific library email validator can check for a bunch of different emails with different domain names different lengths different symbols and many other ways around meaning we'll so I'm gonna go ahead and move this down and import that library and you'll see how much more succinct and condensed our code looks our code looks using this library so I'll import the library at the top here and I'm going to call it validator and we're gonna require it into this file now the library name is called email validator and now what we do now that we have validator the library itself we can call a lot of their methods that are actually written in that library using a period so we can simply type in of console lock this as well validator dot validate and then here we type in the name the email that we want to check so I'll do the same one that we did earlier now I'm actually going to comment this out so we know we don't get confused here I'm gonna save this and clear this console out again and we can see that it's actually true so let's say let's remove the @ sign to make sure that it's invalid and we see now we get a false so you can see now how a lot more condensed our validator is simply importing the library allows us to reuse this function into this file and really condense it and make it really readable and easy to to use so let's say we have this validator when setting up a user but we also want to validate users when logging in so we can simply import this library on the login page and do the exact same thing we've done here instead of actually running a long tedious function using regular expressions like the one below so we can actually save ourselves time by reusing this handy function from the library instead of writing this function over over again in different files now in theory you could actually write this function in a utilities file of your own project and save it there and import it in other files but you're essentially running a library on your own and since there's already an library for email validation you might as well just use the one that's available now let's take a look at what is a framework and a gist a framework is a supporting structure where your own code defines the meat of the operations by filling out the structure it essentially provides a scaffold on which to build software and allows a programmer to avoid writing any boilerplate code and merely fill in the blanks leaving the framework to decide when it's appropriate to execute any business logic so let's go ahead and take a look at an example of how we would use a framework we're gonna be working with one called Express now Express is a framework that provides a robust set of features for web and mobile applications now I'm not gonna go too in depth as to what Express is and how it works since this isn't really up tutorial on it but I do have some boilerplate code here where I'm actually importing the framework at the top I'm making an instance of the application itself and sending it to a variable called app and I have a variable called port set to the number 3000 which we're going to be using in a second now we use an Express and similar to other frameworks it's structured in this specific way for it's gonna be looking for specific method calls and specific parameters so when you're actually using Express in framework so you have to be able to name them the methods correctly you can just name them whatever you want so let's say we want to make an actual get request on our route path and we already have an actual Express server instantiated and stored in our variable called app now app will have a bunch of methods that is expecting and one of them is actually get so dot get is basically saying that when it gets to that route it should give the response that is specified in the function and it will take two arguments the first one will be the URL which will be our route path which is simply a forward slash and then it will take a callback function that tells express what to send back to the person making the request now that callback function will take in two parameters as well request and response and these are essentially objects that express knows how to handle and manage and it's essentially going to be looking for them when this route is hit so what we want to do is simply send a message to the route and let's just send back something that says hello from code caddy just like our app instantiation our response object has also a bunch of functions and methods that is are available and we can invoke them whenever we want so before we actually run this we actually have to instantiate our server as well and we can do this by simply calling don't listen and passing in our port close that I'm gonna restart my server here and then we could simply let me save this and then let's go ahead and run our server so I have a new tab open and I have set up my port on 3000th so we can go directly to our localhost 3000 and whatever we have sent through the dot send method call the string that we passed in there and the response object should display to the client here so let's go ahead and go to our localhost 3000 and you can see that the string that we passed into that function call is sent back to the to the user to the client so that's what was written right over here so it's okay if you don't completely understand what the code means in here like I said this isn't really an Express tutorial but what I do want you to get out of this is mostly how we're actually working with a framework as opposed to a library where we simply import it and call a method or a different method with different parameters in this case there is a specific structure that we have to follow so over making a get request or a poster it's gonna be expecting two parameters at least one of them being the route and the other one being a call that function so if one is missing or the certain pattern here is like and not a string it's gonna give you errors so you have to follow the pattern the structure that the framework provides so in this case were more of filling in the functions themselves instead of making simply method invocations like we're doing with the library in this case the framework is calling on your code you're not calling on the frameworks code so let's take a look at an analogy in order to drive this home let's pretend you're a construction worker and you're assigned to build a house now in theory you could build a house without using any tools but if you want to make your life easier you'd go ahead and use a hammer use a nail gun use a handsaw whatever you need so in a sense all these tools are your library you're gonna be using these tools to build a house and not only can you build a house with them you can y'all go ahead and build a table a chair or anything you like with these tools now if we look at a framework well the supporting structure is gonna be the framework itself so the framework dictates how your project will be structured where as libraries are more of the building blocks that can be used anywhere so the skeleton or foundation of the house is essentially your framework you're going to be adding to it and we're gonna fill out the structure until it's completed and it comes together now one key point to note here is that a framework may use libraries but it doesn't really need libraries in order to operate so now we have an idea of what a framework in a library is but one of the biggest factors that differentiate a library and a framework is something called inversion control when we use frameworks and libraries our code flows a little bit different so let's say you have your own code written and you import a library when you import a library you have access to all these methods that are collected pieces of functionality from that library so you're making calls directly to the library itself so when we use a library we are in charge of the flow of the application we are the ones choosing when and where to call the library on the other hand when we use a framework the framework itself is making calls towards our code so the framework writer writes the application and leaves out the interesting details which we fill in so when using a framework the framework is in charge of the flow it provides some places you to plug in your code but it calls a code you plugged in as needed now a lot of frameworks can get quite hefty and large like rails for example so for that reason many frameworks actually end up even using libraries but it's important to note like I said in the previous slide it's not a necessity for a framework to contain libraries okay so let's go ahead and take a look at some actual popular frameworks and libraries that are used around one of the main most popular frameworks which I just mentioned is rails which is a very popular beginner friendly framework that uses Ruby and makes it very easy to get started and set up a basic skeleton if you worked a lot with front-end development then you're probably familiar with angular which is a front-end framework that specializes in building rich single page applications there's also django which is a framework that uses python for web development and it's used on by some of the big names such as google youtube and instagram and we also have Express which is a back-end development framework that prides itself in being very minimal and fast so let's go ahead and take a look at some of the libraries now what are the most popular libraries which you've probably heard of is react and react is a JavaScript library by Facebook that's used for building user interfaces and single page applications there's also jQuery which is a very old cycle library designed to help in manipulating HTML elements as well as the event handling and CSS animation another library called lodash is essentially a Productivity kit that provides a bunch of utility functions that you can use in your program right off the bat and last we have redux which is another open source JavaScript library for managing applications state and it's commonly used with react so hopefully this video gave you a good understanding of what is a framework and a library and how they're actually different anyway thank you all for watching during the conversation by subscribing to this channel or dropping a comment below and if you want to take your skills to the next level start learning on code Academy today
Info
Channel: Codecademy
Views: 43,236
Rating: 4.9412589 out of 5
Keywords: coding, codecademy, learn to code, programming, frameworks, libraries, code foundations, coding basics, libraries definition, frameworks definition
Id: LimOOe6I4eo
Channel Id: undefined
Length: 12min 59sec (779 seconds)
Published: Wed Jun 05 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.