Library vs Framework

AbdulMutlib
3 min readJan 6, 2021

--

The two concepts are important but sometimes confusing for developers.

Key Difference and Definition of Library and Framework

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 framework calls you.

Framework

In a framework, all the control flow is already there and there are many predefined white spots that we should fill out with our code. A framework is normally more complex. It defines a skeleton where the application defines its own features to fill out the skeleton. In this way, your code will be called by the framework appropriately. The benefit is that developers do not need to worry about if a design is good or not, but just about implementing domain-specific functions. The framework will provide you with hooks and call-backs so that you build on it; it will then call your plugged-in code whenever it wishes, a phenomenon called Inversion of Control.

A framework can contain libraries. A framework will usually include many libraries to make your work easier.

Library

A library performs specific, well-defined operations. A library is just a collection of class definitions. The reason is it simply code reuse, in other words, gets the code that has already been written by other developers. The classes and methods normally define specific operations in a domain-specific area. For example, there are some libraries of mathematics that can let developers just call the function without redoing the implementation of how an algorithm works. A library will usually focus on a single piece of functionality that you access using an API. You call a library function, it executes some code and then the control is returned to your code.

Why use a framework instead of a library

When you have a library you need to understand the functionality of each method and it is relatively hard to create complex interactions since you need to invoke many methods to get to results. Frameworks, on the other hand, contain the basic flow and since you only need to plug in your behavior it is easier to do the right thing. In the GIS example that prompted this discussion, it would be better to have a framework since there are hundreds of interfaces you can use. However, what most users want is an easy way to make a UI entity appear on a map.

Summary

(From Web developer perspective):

A library can be easily replaceable by another library, but a framework cannot.

If you don’t like the jQuery date picker library, you can replace it with another date picker such as bootstrap date picker or pickdate.

If you don’t like Angular.Js on which you built your product, you cannot just replace it with any other framework. You need to rewrite your entire code base.

Mostly, a library requires less of a learning curve compared to frameworks.

For example, underscore.js is a library, Ember.js is a framework.

Thanks for reading:)

--

--