Difference Between Library and Framework

Why would you prefer one over the other?

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.


Figure 1 Library, Framework and your Code image representation

Both of them define an API that for programmers to use. To put those together, we can think of a library as a certain function of an application, a framework as the skeleton of the application and an API as a connector to put those together. A typical development process normally starts with a framework and fills out functions defined in libraries using the API.

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.

Wikipedia

The following is from the current definition of "Software framework" in the Wikipedia:

"In computer programming, a software framework is an abstraction in which software providing generic functionality can be selectively changed by additional user-written code, thus providing application-specific software. A software framework is a universal, reusable software environment that provides particular functionality as part of a larger software platform to facilitate development of software applications, products and solutions."

Why to 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 behaviour 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.

Advantage

Advantages of frameworks over libraries is flexibility and extensibility. By definition, a framework provides you with the necessary means to extend its behaviour. You often can even subclass the framework classes and provide a complete new functionality.

Disadvantage

The disadvantage of frameworks is that the temptation to add more and more functionality creates many bloated frameworks that results in immobility and needless complexity.

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 AngularJs 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.

Up Next
    Ebook Download
    View all
    Learn
    View all