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