What is the difference between a library and a framework?


What is the difference between a library and a framework?

In: Technology

A library is an external collection of functions loaded into the main binary to fill in execution; a framework is an external piece of loaded software the main binary can call to perform tasks via API — but they reside separately from each other and just pass messages back and forth.

But in reality, there’s a lot of overlap.

Hmmm… thinking in ELI5 terms, trying to come up with a simple analogy.

A library could be a big book of car part specifications that you could use to build a car from scratch. A framework is more like an actual car that you can start up and drive right away, and then you learn how to fiddle with things like power seat settings, how to roll the windows up and down, paint color, bumper stickers, and whatnot.

Both of these things are powerful and useful – but you have to be more careful and aware when jumping into a new framework, in my experience. Sometimes, default behaviors that are baked into frameworks are not things that you actually want, and lack of understanding of framework basic assumptions can have spectacular unintended consequences if you’re not willing or able to dive deep into how the framework was built and what it’s trying to accomplish.

A framework is a basic working model of a solution to some problem. For example, when you go out to eat, the basic framework for a meal is: drinks, appetizers, main course, and dessert. Now, you can modify this all you want. Maybe you want dessert wines at the end, or maybe you want to throw in a salad course. The point being is that a framework gives you a starting point from where you can jump from.

A library is a working set of tools to solve a specific problem. To keep the restaurant analogy going, when you go to a restaurant, it is likely that they do not physically make their own silverware, instead they go to the store and buy it as a restaurant’s job is to focus on the food, not the utensils. In this case, the library would be utensils as the specific problem they solve is aiding in eating food. At the end of the day, libraries aren’t needed (e.g the restaurant can make their own utensils if they really wanted), but they greatly help increase efficiency.

Generally, you use frameworks and libraries together. Say you were to open a restaurant, the basic meal framework would be as outlined above and you would use “libraries” of furniture, silverware, etc to aid in the operations of the restaurant. You could even consider the chef to be a library as he/she solves the problem of making the food if the owner themself isn’t cooking.

The key difference between a library and a framework is “Inversion of Control”. When you call a method from a library, you are in control. But with a framework, the control is inverted: the frameworkcalls you. Both of them define an API that forprogrammers to use