DEV Community

Michael Tharrington for CodeNewbie

Posted on

What's the difference between a library and a framework?

Just like the title says, I'd like help understanding the difference between a library and a framework.

I'm a non-techie, so simple explanations and analogies are most definitely welcome. #explainlikeimfive 🙌

Bonus points if you can provide examples of each!

Top comments (21)

Collapse
 
giulio profile image
Giulio "Joshi"

Oh, I love this question.

I'd attempt to work on it with a metaphor

A library is like a tool: offers you a specific set of functions, more or less like an amplifier, a drum trigger or a mixer.
Libraries tend to offer you API, that would be the tools user interface: functions on API are more or less like jacks and sockets, you can tune a library function with parameters like you can adjust settings with knobs and switches.

Since you need an example, I'd play the Log4J card here, a Java logging library quite known to be a security issue, theater of recent drama around the security world.

A framework feels more like a preset recording studio: it has its tool preferences (a set of libraries already ingrained), usually is already tuned for a set of known music genres (frameworks tend to offer their most-used case scenario as tutorials to reduce learning time) and stuff like that.

I mentioned already tuned because framework tend to be pretty bundled up with specific use case, and this time I mention Laravel.

Yes, it is PHP. It has improved a lot and deserves more love.

Like that old recording studio that never let you down, then a bad series of events drove you away from it ...and now it is growing back from its own ashes, happy to welcome you again.

Collapse
 
gmarie profile image
Gnintedem Marie

Thanks a lot for the explanation, it's easy to picture the example and understand the principle.

Collapse
 
peterwitham profile image
Peter Witham

Such a great way to explain. I like it.

Collapse
 
michaeltharrington profile image
Michael Tharrington

Oooo this is awesome! Love the analogies to music production. Thanks so much for offering this thorough (and thoroughly enjoable) explanation, Giulio!

Collapse
 
montyharper profile image
Monty Harper

Loving the more succinct descriptions. How about this:

A library provides.
A framework guides.

Collapse
 
rachelfazio profile image
Rachel Fazio

This was awesome.

Collapse
 
montyharper profile image
Monty Harper

This is all new to me as well, but I asked the exact same question of the internet just the other day, so I'll share my understanding. I'm learning iOS development, so my examples will come from there.

A library is a set of functions that can be called by you, the developer, from within your code. You are in charge and you pick and choose what you need to use. Say you need to calculate the sine value of a given number. Somebody else probably already figured that out and put the algorithm into a library. Indeed, Apple has a library called Math which includes all kinds of mathematical functions you can call, including sine and cosine, so you don't have to work that stuff out yourself from scratch.

A framework is a set of functions that the system uses, which you respond to, so it kind of works the other way around. For example, UIKit is a framework for creating user interfaces. If I want to draw a table of data on the screen, the code I set up will need to respond to the framework.

For example, if a user scrolls down my table, UIKit decides when it's time to draw a new table cell at the bottom. It queries my code with a message that basically says, "hey, what should I put in this new table cell, cell number 29?" The code I have set up responds by saying, "Let's see, that would be Joe Schmo. Here's his information, and you can format it using this layout here."

The library seems much simpler in my mind - using a library is basically like picking out some specialized Leggo blocks from a bin - blocks you need to build your project.

A framework is similar to a library in that it will contain a long list of functions I might want to know about, in order to understand all the things my app could do. But a framework seems more complex than a library.

I think that's because a framework is an interface between your code and something else. For UIKit, the something else is the iPad or iPhone hardware. I don't have to know how to track the user's finger gestures in order to scroll a table. (Thank goodness!) My code can be completely unaware of what's happening out there in the real world because the framework handles that part for me.

I hope that helps. Let me know what you think! Does this match with what you're finding out?

Collapse
 
michaeltharrington profile image
Michael Tharrington

Wow! This is ideed helpful, Monty. Appreciate you hopping in here with this explanation and cool to hear that you were just asking the same thing. I really like how you explained the scale of the two — that a framework is really much bigger than a library.

Going back to your LEGO analogy where a library is like picking out some specialized blocks from a bin, would the framework be akin to having the instuctions at all? It almost sounds like it'd be more like having a bunch of instructions (instructions to more than one set) and having to sort them all out to create something new.

Collapse
 
montyharper profile image
Monty Harper

Good question. I almost didn't use the LEGO analogy because I couldn't think of a way to apply it to a framework. Is it like having the instructions? Maybe kind of. Again, my understanding is still developing. But I can take a whack at this.

I'm struggling with the fact that an app is a very flexible thing, designed to respond to user input, while a LEGO creation is a static object once it's done. So it might not be the greatest analogy to use for a framework.

But let's picture a small factory that makes things to order out of LEGO blocks. You are the app that makes this factory run. You're sitting inside a big vending machine, which you can't see out of.

All of a sudden, a plastic ball rolls down a chute and bounces off your forehead. Inside the ball is a note that says, "Customer pressed the spaceship button."

You consult your little notebook and get busy collecting LEGOs from bins, including a few specialized parts from your Spaceship Library. You assemble some pieces yourself and when you've got enough different chunks to make a spaceship, you load those into a little cart on a track, pull a lever, and watch the cart disappear through the opening of a dark tunnel.

You basically spend your day responding in this way; sometimes the plastic ball wants a spaceship, sometimes a race car, sometimes a castle. You always assemble the pieces as instructed by your notebook and send them off in the cart never to be seen again.

So, the customer is the user. The vending machine itself is the hardware. You are the app. The plastic ball and cart-on-a-track message system is the framework. You never meet the customers, but the framework takes their orders and returns finished products. This can be configured different ways, for example in a different vending machine the spaceship button might be used to order a unicorn instead. Your bins of specialized LEGO parts are the libraries from which you can select pieces. Your notebook is your code; it tells you how to respond to the framework's requests.

I don't know how accurate that is but it reflects my current understanding well enough. I'll be interested to see if anyone wants to tweak it to make it more accurate!!

Thread Thread
 
michaeltharrington profile image
Michael Tharrington

Nice! This def makes sense and is helpful. Also, just loving the narrative you came up with here, haha!

But also, understood that you're still learning as well, so things may not track perfectly. That's all good! I really appreciate ya sharing your current understanding!

Collapse
 
lexlohr profile image
Alex Lohr • Edited

A framework outlines the structure that shapes the code running within it, whereas a library is a collection of related functionality that can be applied without imposing rules beyond its interface.

Collapse
 
melli79 profile image
M. Gr.

And what does tht imply for React?

Collapse
 
lexlohr profile image
Alex Lohr

That their claim that it was a library is wrong.

Collapse
 
almostconverge profile image
Peter Ellis • Edited

In my book (and this is definitely not the book everyone else has) something is a library if your code invokes it and a framework if your code is invoked by it. (And "you" in this situation are an application developer.)

This is not a great definition in that you could easily find exceptions, so it's more like a rule of thumb I use whenever I want to quickly decide what to call something.

In the real world this difference would translate to something like bus vs. car. If you travel by car, you operate the controls of the car. It's a lot of work (though still less than walking), but you can get pretty much anywhere. Whereas if you travel by bus, you just check the routes, timetables and connections, and that's how you get to your destination but you do not operate the bus. (This analogy doesn't work if you're a bus driver. Or so rich that you've got a chauffeur.)

Collapse
 
lupomontero profile image
Lupo Montero • Edited

A library plugs into your code.

Your code plugs into a framework.

Collapse
 
gulshan profile image
Gulshan

A framework usually provides some kind of tooling (CLI/UI) alongside their library(s).

Collapse
 
marodev profile image
Atchoutchou Virgil

Pour moi un freamwor est un outil puissant qui nous permet de sauvegarder tout notre code qu'on aura besoin de modifier après sans problème

Collapse
 
acode123 profile image
acode123

Thanks for commenting!

Collapse
 
dbrower profile image
(((David Brower)))

In general, with a library you own the main() and call it, and with a framework it owns the main() and calls you.

Collapse
 
gutopro profile image
Gutopro

since I'm bent on being more active here I guess this is the best place to start. so in this case a library is a library lol and a framework is a school. libraries contain enormous amount of knowledge on various fields, so if I was looking for names of elements (which are functions ) I'll head over to the chemistry section of the library and that eases the stress for me since someone was kind enough to list them there already for me. now frameworks are like school systems in that should I want to develope a great student I'll need all the components of a functional school, like admin, libraries,gym and obviously the canteen. all these summed up into a single school make the development of students easier, with each section contributing to the overall development. lol I hope this helps

Collapse
 
easytdd profile image
Kazys

A concise explanation I've encountered before is: your code utilizes libraries, and your code dictates the program's flow, whereas the framework leverages your code, and the framework dictates the flow. For instance, as a C# developer working in the .NET ecosystem, ASP.NET is a framework. When I create a controller, I might not even know how it's invoked when HTTP requests arrive; the framework handles the call to the controller. On the other hand, I use the RestSharp library to make HTTP calls to external APIs. My code configures it, constructs requests, invokes the class to dispatch the request, and handles the response.