Libraries and frameworks: What are they and why use them

A few days ago, someone asked me on Twitter “What’s the difference between libraries and frameworks” and “Why people use them”.

That’s a question I asked myself when I started; one that most of us ask ourselves when we were starting in programming. Let’s have a look at what they are and why to use them.

Libraries
What it is a library
Why should I use libraries?
What are the benefits of using libraries?
How to use libraries
Frameworks
What it is a framework
Why should I use frameworks?
What are the benefits of using frameworks?
How to use frameworks
Conclusion

What it is a library

It is easy: A library is just a collection of files with a functionality.

Well, maybe not so easy….

Imagine you want to create a calendar for a website where people will ask for an appointment. You should use HTML, CSS, Javascript to create a page with the current month, displaying the days along the week accordingly, create some CSS classes, etc etc. Not extremely difficult, but you’ll spend too much time doing it.

But you are not the first person that needs this functionality, right? Maybe someone else has coded this before. Even that person may have uploaded it to the public so someone else can use it.

jsCalendar, a JS library to create calendars

So when we say that a library is a “collection of files with a functionality” we mean that a library is a re-usable code that you can use on your own projects.

Do you want to add a Javascript video player to play your own video? Ok, you can code your own or you can use a library.

Libraries are prewritten code, ready to use, that implements a functionality.

Why should I use libraries?

Well, why you shouldn’t?

Imagine you want to cook a veal soup you never tried before. You can try different ingredients, different cooking times…or you can just use a recipe.

You don’t need to do the trial-and-error thing experimenting until you find and edible recipe, you can use the one you found on the Internet, maybe changing some ingredients if you want to, but cooking a good, tested recipe.

In the programming world you don’t want to reinvent the wheel over and over for each project. You don’t need to create a calendar application from scratch every time you want to use a calendar on a website of yours.

Once you write the same piece of code over and over, you will see the need for using libraries.

Libraries offer a solution tested, maintained and perfected over time. And that saves you time, trouble and a lot of headaches.

What are the benefits of using libraries?

  • Speed production. You don’t need to create code that would take hours or days (or even more). Just read the docs, pop the library in your code, and use it. As simple as that.
  • Bugs. This one is hard but you have to realise it. Your implementation will have bugs. No matter how good you are. In some cases, it might break your code. Libraries creators keep improving their library patching it up (keep an eye of the updates. If isn’t updated recently, run from this library).
  • Maintained. Normally libraries have more than one person working on them. They keep expanding the library, adding new functionalities, patching bugs, making them more attractive.
  • Documentation. Ok, let’s say you don’t follow my recommendations on using libraries (shame on you!) and you code your solution. 500 lines of code of pure gold. Then you come back one year later. You won’t remember half of it. Programmers document their libraries so it’s easy to see what it does. And most of them are open source so you can see what the code does at a low level.
  • Separation of concerns. Having your functionality separated in different files make it’s easy to locate bugs and errors.

How to use libraries

Resultat d'imatges de read the fucking manual
Don’t forget to “Read The Fucking Manual”. Or docs.

Any good library has its docs, normally hosted at ReadTheDocs.org.

At them, you’ll find how to install the library, how to add it to your code with a basic example, the API (a description of the code, explaining its functions and more), and even more.

Not every library has all of those, but the good ones have them.

Your standard approach when using a library should be to read the documentation. See how to install it, how to add it to your program and in general follow the path laid down to you.

If you don’t want to mess your code, you can create a testing project to see how the library works, before implementing it in your code.

Sometimes documentation it is not as good as I wanted and I cannot make head or tails of it, so I have to resort to Youtube videos or blogposts. If documentation is not enough, google “How to use LIBRARY_NAME” and you’ll be fine.

If everything else fails, you can always seek help on the internet

What it is a framework

Let’s say I tell you to cook and cut a cookie in a star shape. You’ll measure and cut one. Now I tell you to make 500 cookies with the same shape. You’ll measure and cut 500 cookies? Or you will use a cookie cutter?

Frameworks are like that.

Think of a library on steroids.

A framework is a platform for developing applications with a set of rules, a structure (o ‘Scaffold’), protocols and a series of standards already set, so you can forget about the nitty-gritty and work on the solution instead.

Wood framed house
The ‘frame’ of a wood house.

For example, let’s say I want to create a website in Python with the Framework Django. With only 4-6 lines I can create a project and a CRUD-like admin system. Because every backend of a website needs an administration system, Django developers have considered it and add it as a basic part to their framework, so you don’t need to create one each time you create a project.

Resultat d'imatges de django admin
Cool, right?

What about the login? And the logout? Registration? Authentication? And sending an email in case the user forgot the password where that person changes the password and encrypts the password? Do you really want to code that over and over again with each project?

Or do you want to use a framework?

Why should I use frameworks?

Of course you do!

If you saw the benefit of using libraries, regarding frameworks this multiplies by 10!

Not only that, they provide “Best practices” (a set of rules on how you should do things that have been tested for thousands of programmers), makes it easy to scale, security and overall helps your productivity A LOT.

What are the benefits of using frameworks?

  • Efficiency. With frameworks, you don’t need to write repetitive code.
  • Good documentation. Frameworks are well documented by multiple programmers, so you view what everything does, examples of the functions, etc
  • Secure. As lots of people use them, security is a top concern of every good framework. Programmers ensure good practices are used to increase the security of our web applications.
  • Cost. As the production increases, thanks to removing repetitive code, the process of creating web applications is faster. This means fewer hours for programmers. And that means less cost.

How to use frameworks

Each framework is huge compared to libraries, so in order to use them properly isn’t enough with reading one or two pages of the docs.

Learning a framework good enough it will take time. But worry not: It pays.

My recommendation is to go to the framework’s website, where there is a “Getting Started“, “FRAMEWORK_NAME Tutorial” or something along the lines where you learn how to create a small project.

From there, as you have the basics, create a small pet project. Then start adding functionalities. Start small, grow big.

Conclusion

library vs framework
Image from StackOverflow

We had a lot to read, let’s synthesize everything in a few lines:

  • Libraries are prewritten code, ready to use, that implements a functionality.
  • Libraries let you add some functionality (a clock, the styling of forms, a pop up)
  • Speed up your production, as libraries are (relatively) bug-free, maintained and documented pieces of code.
  • A Framework is a skeleton (or a frame) of a project with a series of functionality, protocols to follow, so you can work in the solution.
  • A Framework is the base of the project. While you add libraries to your project, in a framework you add your code to the framework.
  • Frameworks are a secure, easy to implement and scale solution that boost your productivity.

Well, you know the differences and what libraries and frameworks do. Now it is time to learn them.

keep learning Stock Photo - 45516980