Library Vs Framework

Library Vs Framework

When building projects as a developer, certain tools aid in simplifying the development process and enhancing functionality; Libraries and Frameworks are among these tools.

Developers aren’t required to build from scratch or ground up; Libraries and Frameworks are reusable pieces of code that assist them in solving specific problems. The advantage of using Libraries and Frameworks is that they reduce the time spent on coding, allowing developers to focus more on functionality.

I wrote this article after noticing many developers asking questions like: Is Bootstrap a Library or a framework? Is AngularJS a framework or a library? By the end of this article, you should be able to answer these questions.

What is a library?

A library is a collection of codes that a programmer uses to solve a specific problem.

Now that’s vague…

A library is where codes that are frequently used are stored so that programmers can reuse them instead of rewriting the code again. In adherence to better coding practices and the principle of DRY (Don't Repeat Yourself), rather than rewriting lines of code already written by other developers, you simply "call" a library into your code.

Here's an illustration:

Let's take a simple example in Python where we'll use the math library to calculate the square root of a number. Here's a code snippet:

# Import the math library
import math

# Input a number
num = float(input("Enter a number: "))

# Calculate the square root using the library function
square_root = math.sqrt(num)

# Display the result
print(f"The square root of {num} is: {square_root}")

In this example:

  1. We import the math library using import math.

  2. The user inputs a number.

  3. We use the math.sqrt() function to calculate the square root of the input number.

  4. The result is then displayed.

Libraries like math provide pre-written, reusable code (functions and constants, in this case) that developers can leverage to perform specific tasks without having to implement everything from scratch.

Examples of Libraries

  • Requests: A popular Python library for making HTTP requests. It simplifies communication with web services and APIs, providing an easy-to-use interface.

  • jQuery: A fast and feature-rich JavaScript library designed to simplify HTML document traversal and manipulation, as well as event handling and animation.

  • React: A JavaScript library for building user interfaces, developed and maintained by Facebook. React focuses on building reusable UI components for creating efficient and interactive applications.

Why Use Libraries?

  • Firstly, to simplify your work :

    As practical as it may sound, developers often seek the most efficient path. To save time and expedite the development process, utilizing libraries is the optimal approach.

  • Enhancing functionality:

    With the burden of writing extensive lines of code lifted, developers can now concentrate on functionality—making the code work. This not only leads to better results but also promotes increased efficiency with less time spent on coding.

  • Reusability:

    Libraries offer pre-written, tested, and debugged code that can be reused across multiple projects, saving developers time and effort.

  • Consistency:

    Libraries present a consistent and well-documented interface, enabling developers to seamlessly integrate them into their projects and facilitating comprehension and maintenance for other developers.


WHAT IS A FRAMEWORK?

Think of a framework as a structure already imbued with codes, modules and even libraries. With an existing framework, developers only need to integrate their codes.

LET'S ILLUSTRATE THAT...

Imagine you've just acquired a new office with an impressive cabinet. The cabinet has already been assembled, so all you have to do is arrange your items according to how the cabinet allows.

This is essentially what a framework accomplishes; developers can incorporate their codes to enhance functionality, but the framework has already supplied codes that perform specific tasks—much like the cabinet.

A framework serves as the foundation upon which developers construct applications designed for specific tasks.

Examples of Frameworks

Here are some web frameworks:

  • Angular: A TypeScript-based framework for building dynamic web applications. Developed and maintained by Google, Angular provides a structured and modular approach to front-end development, enabling the creation of scalable and interactive user interfaces.

  • Django: An open-source Python framework known for speed, security, and maintainability.

  • Ruby on Rails: A Ruby-based framework emphasizing convention over configuration for streamlined development.

  • Laravel: A PHP framework that aims to simplify and enhance coding with elegant and expressive syntax.

Why Use Frameworks?

There are so many reasons why developers use frameworks. Creating applications involves a lot of tasking procedures. A framework solves these coding problems by offering low-level functions such as ease of debugging, improved coding, easy code reusability, and improving development speed.

  • Frameworks reduce the complexity associated with software development

  • Developers can build and deploy applications faster using frameworks

  • Like Libraries, frameworks include reusable codes.

  • Developers can now focus on efficiency and functionality without building from scratch.

  • A framework provides resources that allow developers to create rich and dynamic content

  • Generally, frameworks are used to easily create and deploy applications.

Here's a simple diagram illustrating the concept of a framework:

In this diagram:

  • Application: Represents the specific application you're building.

  • Framework (Foundation): Serves as the foundation or structure of the application. The framework provides essential tools and predefined structures.

  • Modules (Components): Different parts or modules of the application that plug into the framework. These modules leverage the functionalities provided by the framework.

  • Libraries: Additional tools or pre-written code (similar to modules but more generic) that can be used within the application. Libraries are often integrated into the framework.

From this diagram, you can see that the application is built on top of a framework, which provides a structured environment and utilizes various modules and libraries.

What is the Key Difference Between Library and Framework?

The cover diagram at the very top of this article explains the key difference between libraries and frameworks.

The key difference between both is Inversion Of Control (IoC).

With libraries, developers control the flow.

Your code calls the library. This means that the developer can decide what point or place in the code to “call in “ the library.

That’s not the case with frameworks.

The inverse happens (hence the name Inversion Of Control). The framework calls our code and controls the program flow.

A developer can switch between libraries in the code but cannot do the same with frameworks. Once a developer is using a framework and runs into a problem, he cannot simply use another framework except restart the project. Frameworks are difficult to modify and follow a set of rules. Whereas, the developer sets his own rules when using libraries.


A Note to Take Down ...

Now, it is safe to say that both Libraries and Frameworks have their limitations and advantages. Developers get to use a lot of these Libraries and Frameworks as they build projects. Therefore, it is important to note the differences between both and decide when to use them.

I hope that you can now look at the tools you have been using and decide which one is a Library or framework.

If you appreciate this article, you can follow me up on my social media platform for more updates. X , Linkedin