Clean code
Search…
⌃K

Library vs Framework

While the terms Library and Framework may sound similar and developers often use the terms “library” and “framework” interchangeably, they both work differently.
Both libraries and frameworks are code written by some developer to solve a common problem efficiently. They both give you an excellent approach to write DRY (don't repeat yourself) code. Their purpose was to increase the reusability of the code so that you can use the same piece of code or functions again in your various project.
Before we dive into the details of how libraries and frameworks difference, let’s take a look at both terms in more detail.

What is Library?

For example, let’s say you have a program where you plan on working with strings. You decide to keep your code DRY (don’t repeat yourself) and write some reusable functions like these:
public static String[] getWords(String str) {
return str.split(' ');
}
public static String createSentence(String[] words) {
return words.join(' ');
}
Congratulations. You’ve created a library.
As you can see, a library is a collection of prewritten code that can be used to simplify tasks. Reusability is one of the main reasons to use libraries.
It is essentially a set of pre-defined functions and classes that programmers can use to simplify their work and speed up the development process. So, developers do not have to write code to accomplish specific functionality because the library already includes code for those functionalities. Standard libraries are available with most programming languages, but programmers can also make their own custom libraries.
Usually, most libraries do not have a large size. Generally, libraries are focused on narrow scopes such as strings, sockets, and IO, etc., so their APIs are also small and require fewer dependencies. Common lang, Gson, etc. are examples of libraries which can let developer just call the function without redo the implementation of how an algorithm works.

What is Framework?

A framework embodies some abstract design, with more behavior built in. In order to use it you need to insert your behavior into various places in the framework either by subclassing or by plugging in your own classes. The framework's code then calls your code at these points.
In framework, all the control flow is already there, and there are a bunch of 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 when 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.
One thing to remember here is that frameworks sometimes get quite large, so they may also use the Library. But the Framework doesn't necessarily have to use Library.
Advantages:
  • Software frameworks are highly versatile, robust, and efficient due to the fact that they are often built, tested, and optimized by multiple software developers.
  • Facilitates better programming practices and proper implementation of design patterns
  • Avoiding duplicates and redundant code results in fewer bugs and a more consistent development process
  • It would be possible to create one’s own software framework or contribute to an open-source framework. As a result, the functionality of software applications will continue to improve.
  • Numerous code segments and functionalities have been built in framework and tested in advance, which makes applications more reliable.
  • Even developers who do not own the code can test and debug it
  • Developing an application takes significantly less time as it provides code to perform common tasks and uses code provided by a developer for custom functionality.
  • As a result of using a software framework, you can focus on high-level functionalities, while the low-level functions are handled by the framework itself.

The key difference between Library vs 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. Refer to Hollywood Principle
It means when you use a library, you are in charge of the flow of the application. You are choosing when and where to call the library. But, when you use a framework, the framework is in charge of the flow. It provides some places for you to plug in your code, but it calls the code you plugged in as needed.
The following are some other fundamental differences between Libraries and Frameworks:
Parameters
Library
Framework
Definition
Libraries provide developers with predefined functions and classes to make their work easier and boost the development process.
Framework, on the other hand, is like the foundation upon which developers build applications for specific platforms.
Inversion of Control
By using a library, you can control the flow of the application and call the library.
In contrast, when you use a framework, the control is inverted, i.e., the framework controls the flow and calls your code.
Collection
Generally, libraries are a collection of helper modules, objects, classes, functions, message templates, pre-written code, etc.
Frameworks consist of a lot of APIs, compilers, toolsets, support programs, libraries, etc.
Code Modification
Codes in libraries are geared toward a particular program or to solve a specific development problem. Therefore, developers must modify library code to meet their needs.
Despite the fact that frameworks generate new codes for developers. These codes cannot be altered or modified later. Unlike libraries, frameworks do not allow users to modify their pre-written codes, so you don’t have to worry about deleting or changing them.
Scope
It is possible to call a library out of context. You may use the library wherever you see fit in your code.
On the other hand, you can only call and use what belongs to a Framework within the same Framework.
Function
In the program linking and binding process, they play an important role.
Using them, you can build and deploy applications in a standard way as the framework already provides code to perform common tasks and uses code provided by a developer for custom functionality.
Complexity
Having a library means understanding the functionality of each method, and it isn’t easy to create complex interactions since you need to call many methods to get the desired results.
Frameworks, on the other hand, embody the basic flow, and since plugins need to be added to code, it is easier to do the right modification.
Extensibility
Generally, libraries aren’t designed for extensibility; they are designed to accomplish a specific purpose.
Frameworks provide general functionality. Because of this, they are built to be extensible, which allows developers to incorporate app-specific features without modifying the framework’s source code.
Replaceable
It is easy to replace a library with another library. For instance, if you do not like the Gson library, you can use another json library like Jackson or Simplest json.
Frameworks are difficult to replace. If, for instance, you were using Spring framework to build your product, you cannot simply swap it out for another framework. It requires rewriting the entire codebase.
Performance
Less code is required to build libraries, which leads to faster loading times and better performance.
Developing a framework requires a lot of coding, which increases loading times and decreases performance.
Usage
The purpose of libraries is to perform a defined and specific task. Eg: Image manipulation, network protocols, math operations, etc.
Frameworks can be used for performing a wide range of tasks. Among these are Web application systems, plug-in managers, GUI systems, and so on.
Existing Projects
You can integrate libraries seamlessly into existing projects to add functionality.
Incorporating frameworks seamlessly into an existing project is impossible. Instead, frameworks should be used when starting a new project.
Benefits
Good code quality, reusability, and control, enhanced speed and performance of the program, etc.
Faster programming, support from the community, great support for MVC (Model View Controller) pattern, etc.
Examples
Gson, Common Lang, etc.
Spring, Struts, Quarkus, etc.

Summary

In general, both libraries and frameworks are code written by some developer to solve a common problem efficiently. A framework inverts the control of the program, it tells the developer what they need. A library doesn’t, the developer calls the library where and when they need it. Frameworks are better than libraries, or vice versa; however, it is ultimately a matter of use cases and situations rather than the tool itself.
A framework can relieve you of the headache of dependency trees, what to use and what not to use, how to scaffold your application, and how to design it, but it doesn’t provide you with complete control over your application. It’s very useful if you need to develop something quickly for a client.
Using libraries, on the other hand, allows you to design your own application tailored specifically to your needs; however, maintaining dependencies, updating individual libraries, and identifying if one is broken due to another, can be extremely time-consuming.
Programming is a complex world, and while neither pattern is inherently better, you need to know which pattern is appropriate for the problem at hand.

References