Library or Framework ?
By: Senura Diwantha
Tags: Framework, Libraries, Inversion Of Control

Library or Framework ?

Why would you prefer framework over the library ? Both frameworks and libraries are reusable codes written by someone else to solve common problem. There isn’t anything magic about libraries and frameworks. Both helps you to solve your problem in easier way. Let me first give you a brief introduction about library and framework.

What is Library ?

Before the definition part, lets implement some method to “add” two integer values.

public class Simple {

public static int add(int a, int b){

return a+b;

}

}

Yeah !!! You’ve created a simple library.

Library is a collection of helping methods which you can be used for specific functionality in your code, when you need it. Now you would understand the previous example that we used to add two integers also can be used as a simple library function. In your program there is particular function that will need consistently in your program, you can write common function where you can pass in the necessary parameters to solve your common problem, that also can be a library function. Example of a Library is jQuery.

What is Framework ?

Framework is collection of inter-related libraries that work together to serve a large purpose. Framework itself an application you can plug your code into it. Example of a Library is Vue.js

Wikipedia makes it more clear:

“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”

What is the key difference between library Vs framework ?

The key difference of the framework and library lies in a term called “Inversion of Control”(IoC).

Imagine the physical library we use our day to day life. Whenever you need a book for your specific work you can borrow it from the book shelves and use it at anytime, which means you are the in charge. Consider about the foundation of a house it like a framework. You build your house on the foundation it gives you the structure when you place your components of the house. That’s the way of framework works. Framework is the in charge.

In Technical Terms

When you call a method from a library you are the in charge because you call it when you need it, so you are in control. When it comes to the framework its the opposite side. In the framework the control is inverted, framework is the in charge of the flow and it provide some space to plug your code and it calls you when plugged in as needed.

Their Relationship

To work these framework and libraries together it use an API. Think a library as a function of a application, framework as the skeleton of the application, and API as the connector to put those things together. In generally way framework calls the functions implemented in library through the API.

“Opinionated” or “Un-opinionated.”

Finally, On the idea of being opinionated, Frameworks are more opinionated it order you to put specific files in specific places in project. And framework can initialize itself. On the other hand libraries are rarely opinionated, it rarely give the instructions to where to put your files or not. You have to manually initialize your application because library cannot initialize itself. The different frameworks are opinionated at different degrees. These terms are subjective.When I personally would consider AngularJS a highly opinionated framework, and VueJS a less-opinionated framework.